SonarQube Scan Workflow
Run SonarQube or SonarCloud code quality analysis.
Overviewโ
This workflow performs comprehensive code quality analysis using SonarQube or SonarCloud, checking for bugs, vulnerabilities, code smells, and code coverage.
When to Useโ
- โ You need code quality analysis
- โ You want to track technical debt
- โ You need security vulnerability scanning
- โ You want code coverage tracking
Inputsโ
| Input | Type | Default | Description |
|---|---|---|---|
sonar_platform | string | 'sonarcloud' | Platform (sonarcloud, sonarqube) |
sonar_organization | string | '' | SonarCloud organization |
sonar_project_key | string | '' | Project key |
coverage_paths | string | 'coverage.xml' | Coverage report paths |
sources | string | 'src' | Source code paths |
Secretsโ
| Secret | Required | Description |
|---|---|---|
sonar_token | Yes | SonarCloud/SonarQube token |
Outputsโ
| Output | Description |
|---|---|
quality_gate_status | Quality gate pass/fail status |
analysis_url | URL to analysis results |
Usage Examplesโ
SonarCloud Analysisโ
jobs:
test:
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_run_test.yaml@master
with:
python_version: '3.11'
test_type: unit-test
all_test_items_paths: test/unit_test/
sonar:
needs: test
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_sonarqube_scan.yaml@master
secrets:
sonar_token: ${{ secrets.SONAR_TOKEN }}
with:
sonar_platform: sonarcloud
sonar_organization: my-org
sonar_project_key: my-project
SonarQube Analysisโ
jobs:
sonar:
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_sonarqube_scan.yaml@master
secrets:
sonar_token: ${{ secrets.SONARQUBE_TOKEN }}
with:
sonar_platform: sonarqube
sonar_project_key: my-project
sources: 'src,lib'
With Coverageโ
jobs:
test:
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_run_test.yaml@master
with:
python_version: '3.11'
test_type: unit-test
all_test_items_paths: test/
organize-coverage:
needs: test
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_organize_test_cov_reports.yaml@master
with:
test_type: all-tests
sonar:
needs: organize-coverage
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_sonarqube_scan.yaml@master
secrets:
sonar_token: ${{ secrets.SONAR_TOKEN }}
with:
sonar_platform: sonarcloud
sonar_organization: my-org
sonar_project_key: my-project
coverage_paths: 'coverage.xml'
How It Worksโ
Step 1: Download Coverageโ
Downloads coverage reports from previous jobs:
- uses: actions/download-artifact@v4
with:
name: coverage-reports
Step 2: Run SonarScannerโ
Executes SonarScanner analysis:
sonar-scanner \
-Dsonar.organization=$SONAR_ORG \
-Dsonar.projectKey=$PROJECT_KEY \
-Dsonar.sources=$SOURCES \
-Dsonar.python.coverage.reportPaths=$COVERAGE_PATHS \
-Dsonar.host.url=https://sonarcloud.io
Step 3: Quality Gate Checkโ
Checks quality gate status:
curl -u $SONAR_TOKEN: \
"https://sonarcloud.io/api/qualitygates/project_status?projectKey=$PROJECT_KEY"
Configurationโ
sonar-project.propertiesโ
Create configuration file:
sonar.projectKey=my-project
sonar.organization=my-org
sonar.sources=src
sonar.tests=test
sonar.python.coverage.reportPaths=coverage.xml
sonar.python.version=3.11
# Exclusions
sonar.exclusions=**/migrations/**,**/tests/**
sonar.coverage.exclusions=**/tests/**,**/__init__.py
Quality Gateโ
Configure quality gate rules:
- Coverage: Minimum 80%
- Duplications: Maximum 3%
- Maintainability: A rating
- Reliability: A rating
- Security: A rating
Metrics Trackedโ
Code Qualityโ
- Bugs: Potential bugs
- Vulnerabilities: Security issues
- Code Smells: Maintainability issues
- Technical Debt: Estimated fix time
Coverageโ
- Line Coverage: % of lines covered
- Branch Coverage: % of branches covered
- Condition Coverage: % of conditions covered
Complexityโ
- Cyclomatic Complexity: Code complexity
- Cognitive Complexity: Understanding difficulty
Duplicationsโ
- Duplicated Lines: % of duplicated code
- Duplicated Blocks: Number of duplications
Best Practicesโ
1. Configure Exclusionsโ
Exclude non-production code:
sonar.exclusions=**/tests/**,**/migrations/**,**/__pycache__/**
sonar.coverage.exclusions=**/tests/**
2. Set Quality Gatesโ
Define minimum standards:
sonar.qualitygate.wait=true
sonar.qualitygate.timeout=300
3. Track Coverageโ
Include coverage reports:
sonar.python.coverage.reportPaths=coverage.xml,coverage-*.xml
4. Regular Scansโ
Run on every PR and merge:
on:
pull_request:
push:
branches: [main]
Troubleshootingโ
Analysis Failsโ
Symptoms:
- SonarScanner errors
- Authentication failures
Solutions:
-
Verify token:
secrets:
sonar_token: ${{ secrets.SONAR_TOKEN }} -
Check project key
-
Verify organization name
Coverage Not Showingโ
Symptoms:
- 0% coverage in SonarCloud
- Coverage report not found
Solutions:
-
Verify coverage path:
coverage_paths: 'coverage.xml' -
Check coverage format (must be XML)
-
Ensure coverage artifact uploaded
Quality Gate Failsโ
Symptoms:
- Quality gate status: FAILED
- Metrics below threshold
Solutions:
- Review analysis results
- Fix identified issues
- Adjust quality gate settings
- Improve test coverage
Related Workflowsโ
- rw_run_test - Run tests
- rw_organize_test_cov_reports - Organize coverage
- rw_upload_test_cov_report - Upload coverage