Triển Khai GitHub Actions Cho Flutter Mobile Kết Hợp Phân Tích Mã SonarQube Chuyên mục Devops 2025-04-22 0 Lượt xem 0 Lượt thích 0 Bình luận
Trong bài viết này, mình sẽ hướng dẫn bạn tạo một pipeline CI đơn giản dùng GitHub Actions cho một dự án Flutter Mobile. Pipeline này sẽ tự động:
-
Checkout mã nguồn
-
Thiết lập Flutter
-
Cài đặt dependencies
-
Build code sinh bởi build_runner (freezed/json_serializable...)
-
Phân tích mã với dart analyze
-
Gửi report lên SonarQube
-
Kiểm tra Quality Gate
Yêu Cầu
-
Repository Flutter Mobile hoạt động bình thường
-
Đang dùng package chung có truy cập bằng git private repo (cần token)
-
Server SonarQube đã được cấu hình trước (Sonar Scanner và project key/name)
-
Đã tạo secrets trong GitHub:
-
SECRET_GITHUB_PAT: token cá nhân truy cập git repo private
-
SONAR_TOKEN: token truy cập SonarQube server
-
SONAR_URL: URL server SonarQube (ví dụ: http://your-sonarqube.com)
-
Cấu Hình Workflow
Tạo file .github/workflows/flutter-sonarqube.yml với nội dung sau:
name: Flutter + SonarQube CI
on:
push:
branches: [ develop ]
pull_request:
jobs:
analyze:
name: Analyze Flutter Project with SonarQube
runs-on: ubuntu-latest
env:
SONAR_HOST_URL: ${{ secrets.SONAR_URL }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
steps:
- name: ⬇️ Checkout code
uses: actions/checkout@v4
- name: 🔧 Setup Flutter
uses: subosito/flutter-action@v2
with:
flutter-version: '3.27.4'
- name: 🔧 Override pubspec.yaml to use private git dependency
run: |
sed -i '/liveapp_common_mobile:/,/path:/c\ liveapp_common_mobile:\n git:\n url: https://${{ secrets.SECRET_GITHUB_PAT }}@github.com/We-Rikkei/liveapp_common_mobile.git\n ref: develop' pubspec.yaml
- name: 📆 Install dependencies
run: flutter pub get
- name: 🔨 Run build_runner
run: flutter pub run build_runner build --delete-conflicting-outputs
- name: 📊 Run analyzer (with tee)
run: dart analyze --format=machine | tee analysis.txt || true
- name: 📥 Install SonarScanner
run: |
wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-5.0.1.3006-linux.zip
unzip sonar-scanner-cli-*.zip
sudo mkdir -p /opt/sonar-scanner
sudo mv sonar-scanner-*/* /opt/sonar-scanner
echo "SONAR_SCANNER_HOME=/opt/sonar-scanner" >> $GITHUB_ENV
echo "/opt/sonar-scanner/bin" >> $GITHUB_PATH
- name: 🚀 Run SonarQube Scanner
run: |
sonar-scanner \
-Dsonar.projectKey=LiveAppUser \
-Dsonar.projectName="LiveAppUser" \
-Dsonar.sources=lib,pubspec.yaml \
-Dsonar.sourceEncoding=UTF-8 \
-Dsonar.dart.analyzer.mode=MANUAL \
-Dsonar.dart.analyzer.report.mode=MACHINE \
-Dsonar.dart.analyzer.report.path=analysis.txt \
-Dsonar.host.url=$SONAR_HOST_URL \
-Dsonar.login=$SONAR_TOKEN
- name: 🥪 Debug Check report-task.txt existence
run: find . -name "report-task.txt"
- name: 🚩 Check Quality Gate
uses: SonarSource/sonarqube-quality-gate-action@master
with:
scanMetadataReportFile: '.scannerwork/report-task.txt'
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
Giải Thích Một Số Step Quan Trọng
Override pubspec.yaml
Thay đường dẫn local package bằng link git private repo có token để pipeline Flutter pub get không bị lỗi.
Analyzer
Dùng dart analyze --format=machine xuất file analysis.txt, Sonar Scanner sẽ đọc file này. Dù phân tích bị lỗi (exit code != 0), ta dùng || true hoặc tee để không làm pipeline fail.
SonarScanner CLI
Tải và cài đặt SonarScanner trong CI runner, sau đó gời lệnh scan với projectKey tương ứng trong SonarQube.
Check Quality Gate
Kiểm tra kết quả sau khi scan có pass Quality Gate không. File report-task.txt thường được sinh tại .scannerwork/ tại root.
Kết Luận
CI pipeline cho Flutter kết hợp SonarQube giúp bạn:
-
Kiểm soát chất lượng mã
-
Tự động phân tích trước khi merge
-
Bám sát các nguy cơ bug, code smell, duplication, test coverage (nếu có)
Chỉ cần vài bước cấu hình, dự án Flutter của bạn đã có một workflow CI chắc chắn và chào chuẩn!
Nếu bạn thích bài viết này, hãy chia sẻ với đồng đội và theo dõi blog để nhận thêm nhiều tips hay ho về DevOps và Flutter nha!
Bình luận (0)