Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
sonar.projectKey=shop-api
sonar.projectName=Shop API
sonar.sources=src
sonar.tests=tests
sonar.python.coverage.reportPaths=coverage.xml
sonar.qualitygate.wait=trueexport SONAR_HOST_URL=https://sonar.example.com
export SONAR_TOKEN="$(cat ~/.sonar-token)"
sonar-scannerStep by step
- Create the project in SonarQube (Projects → Create → manually, or import from GitHub/GitLab) and a project analysis token (My Account → Security). Store the token as a CI secret.
- Project settings in the repo (quick answer) — key, source and test folders, the coverage report.
sonar.qualitygate.wait=truemakes the scanner wait for the result and fail if the gate fails. - Coverage — SonarQube does not run tests; your build creates the report first (for Python
pytest --cov=src --cov-report=xml; Java: JaCoCo; JS: lcov). - Run the scanner — locally with the CLI, or the scanner container
sonarsource/sonar-scanner-cli; Maven/Gradle projects use their plugin (mvn sonar:sonar). - In CI — after the tests, before the deploy:
- run: pytest --cov=src --cov-report=xml
- uses: SonarSource/sonarqube-scan-action@v5
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ vars.SONAR_HOST_URL }}Pin actions to a commit SHA in real pipelines.
- The quality gate on new code — the default "Sonar way" checks new code only: no new bugs/vulnerabilities, reviewed hotspots, ≥ 80 % coverage on new lines, little duplication. Old problems do not block you; new code must be clean.
- Pull request decoration — connect SonarQube to GitHub/GitLab (DevOps platform integration), then results appear as a check and comment on the PR.
- Security hotspots — code that needs a human decision (a regex, a crypto call). Review them in the UI: "safe" or "fix".
Check that it worked
The scanner ends with ANALYSIS SUCCESSFUL and QUALITY GATE STATUS: PASSED; the project page shows bugs, vulnerabilities, coverage and duplication; a test PR with a hard-coded password fails the gate and the CI job.
Common problems
| Problem | Cause | Fix |
|---|---|---|
Not authorized | Token missing or wrong scope. | Project analysis token in SONAR_TOKEN. |
| Coverage 0 % | Report not created or wrong path. | Run tests with coverage first; fix reportPaths. |
| Gate fails on old code | Gate set on overall code. | Use the "new code" conditions; set the new-code period. |
| PR analysis missing | Community Edition limits, or no platform integration. | Check your edition's features; configure the integration. |
| Scanner very slow | Analyses node_modules, build output, generated code. | sonar.exclusions=**/node_modules/**,**/dist/**. |
| Pipeline passes although the gate failed | Gate not awaited. | sonar.qualitygate.wait=true. |
Go further
- Command pages: sonar-scanner.
- Next how-tos: Find bugs in code with Semgrep, Find vulnerable dependencies with Grype, Create a CI pipeline in GitHub Actions, Use secrets safely in GitHub Actions.
- Learn it: Supply chain services — SonarQube, GitHub Actions — SAST in CI. Full guide: SonarQube.
1Why does the default quality gate look at new code only?
So old problems do not block every change, while all new code must meet the standard.
2Does SonarQube run the tests to measure coverage?
No — the build runs the tests and writes a coverage report; SonarQube reads that report.