SonarQube Scanning in CI/CD¶
SonarQube analysis runs automatically in every pipeline during the test stage.
How It Works¶
- Pipeline triggers — on every push to any branch
- SonarQube scanner runs in the
teststage - Results uploaded to the SonarQube server
- Quality Gate evaluated — pass or fail
Pipeline Job¶
The sonarqube job in .gitlab-ci.yml:
sonarqube:
stage: test
image:
name: sonarsource/sonar-scanner-cli:latest
entrypoint: [""]
script:
- sonar-scanner
-Dsonar.projectKey=$SONAR_PROJECT_KEY
-Dsonar.host.url=$SONAR_HOST_URL
-Dsonar.token=$SONAR_TOKEN
tags:
- small
Configuration¶
Each project has a sonar-project.properties file in its root:
sonar.projectKey=orchestrator-server
sonar.sources=src
sonar.tests=tests
sonar.python.coverage.reportPaths=coverage.xml
Viewing Results¶
- After the pipeline completes, open SonarQube
- Navigate to your project
- The latest analysis shows new issues introduced by your branch
Quality Gate Failures¶
If the quality gate fails:
- Check the pipeline job log for a summary
- Open SonarQube and look at the New Code tab
- Fix the reported issues (bugs, vulnerabilities, code smells)
- Push a new commit — the pipeline will re-analyze
Branch Analysis¶
mainbranch: Full analysis, quality gate enforced- Feature branches: Analyzes only new/changed code compared to
main - MR decoration: SonarQube posts findings as comments on the MR (if configured)