Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 238CI/CD · Code quality

How to check code quality with SonarQube

Code reviews catch logic, but people miss the hard-coded password, the SQL built from user input, the 300-line function and the code without tests. SonarQube analyses every branch and pull request for bugs, vulnerabilities, security hotspots, duplication and coverage, and a quality gate decides pass or fail. This how-to creates a project, runs the scanner locally and in CI, and makes the pipeline wait for the gate.

  • 5 Lessen
  • 3 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

238.1

The quick answer

sonar-project.properties
sonar.projectKey=shop-api
sonar.projectName=Shop API
sonar.sources=src
sonar.tests=tests
sonar.python.coverage.reportPaths=coverage.xml
sonar.qualitygate.wait=true
export SONAR_HOST_URL=https://sonar.example.com
export SONAR_TOKEN="$(cat ~/.sonar-token)"
sonar-scanner

238.2

Step by step

  1. 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.
  2. Project settings in the repo (quick answer) — key, source and test folders, the coverage report. sonar.qualitygate.wait=true makes the scanner wait for the result and fail if the gate fails.
  3. Coverage — SonarQube does not run tests; your build creates the report first (for Python pytest --cov=src --cov-report=xml; Java: JaCoCo; JS: lcov).
  4. Run the scanner — locally with the CLI, or the scanner container sonarsource/sonar-scanner-cli; Maven/Gradle projects use their plugin (mvn sonar:sonar).
  5. In CI — after the tests, before the deploy:
.github/workflows/ci.yml (part)
      - 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.

  1. 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.
  2. Pull request decoration — connect SonarQube to GitHub/GitLab (DevOps platform integration), then results appear as a check and comment on the PR.
  3. Security hotspots — code that needs a human decision (a regex, a crypto call). Review them in the UI: "safe" or "fix".

Commando's in deze les

238.3

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.

238.4

Common problems

ProblemCauseFix
Not authorizedToken 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 codeGate set on overall code.Use the "new code" conditions; set the new-code period.
PR analysis missingCommunity Edition limits, or no platform integration.Check your edition's features; configure the integration.
Scanner very slowAnalyses node_modules, build output, generated code.sonar.exclusions=**/node_modules/**,**/dist/**.
Pipeline passes although the gate failedGate not awaited.sonar.qualitygate.wait=true.

238.5

Go further

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.

Alle commando's in dit hoofdstuk