Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

sonar-scanner

Send code to SonarQube / SonarCloud for analysis: bugs, vulnerabilities, security hotspots, code smells, coverage and a quality gate.

Niveau
DevSecOps-service
Groep
Beveiligingsscans en hardening
Root nodig
Nee
Pakket
sonar-scanner-cli

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

Wat het doet

SonarQube (self-hosted, port 9000) and SonarQube Cloud analyze code in many languages. The scanner runs in CI, reads sonar-project.properties, and uploads the results. The server applies a quality gate (for example: no new bugs, no new vulnerabilities, coverage on new code ≥ 80 %) and can block the merge. Java/Maven, Gradle and .NET have their own scanners; sonar-scanner is for everything else.

Zo schrijf je het

sonar-scanner [-Dkey=value ...]

Belangrijkste delen

Key properties

sonar.projectKey
Unique project ID.
sonar.sources
Source folders.
sonar.tests
Test folders.
sonar.host.url
Server URL.
sonar.token
Analysis token (from env SONAR_TOKEN).
sonar.exclusions
Files to skip.
sonar.python.coverage.reportPaths / sonar.javascript.lcov.reportPaths
Coverage reports.
sonar.qualitygate.wait=true
Wait for the gate and fail the job if red.

Opties 4

Meest gebruikt

Flags

-D key=value
Set any property.
-X
Debug output.
-h
Help.
SONAR_TOKEN / SONAR_HOST_URL
Env vars for token and URL.

Voorbeelden 10

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

  1. 1Run SonarQube for a lab

    podman run -d --name sonarqube -p 9000:9000 docker.io/sonarqube:lts-community
    curl -s http://localhost:9000/api/system/status
  2. 2A project file

    cat sonar-project.properties
    sonar.projectKey=s0sta_shop
    sonar.sources=app
    sonar.tests=tests
    sonar.exclusions=**/migrations/**
    sonar.python.coverage.reportPaths=coverage.xml
  3. 3Run the scanner and wait for the gate

    export SONAR_TOKEN=squ_exampletokenonly0000000000000000000000
    sonar-scanner -Dsonar.host.url=http://localhost:9000 -Dsonar.qualitygate.wait=true
  4. 4Run the scanner in a container

    docker run --rm -e SONAR_HOST_URL=http://sonar.lab.local:9000 -e SONAR_TOKEN -v "$PWD:/usr/src" sonarsource/sonar-scanner-cli
  5. 5Make a coverage report first (Python)

    pytest --cov=app --cov-report=xml
    sonar-scanner
  6. 6GitHub Actions

    cat .github/workflows/sonar.yml
          - uses: actions/checkout@v4
            with: { fetch-depth: 0 }
          - uses: SonarSource/sonarqube-scan-action@v4
            env:
              SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
              SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
  7. 7Analyze a pull request (decoration)

    sonar-scanner -Dsonar.pullrequest.key=42 -Dsonar.pullrequest.branch=feat/health -Dsonar.pullrequest.base=main
  8. 8Read the gate status with the API

    curl -s -u "$SONAR_TOKEN:" "http://localhost:9000/api/qualitygates/project_status?projectKey=s0sta_shop" | jq '.projectStatus.status'
  9. 9List open vulnerabilities

    curl -s -u "$SONAR_TOKEN:" "http://localhost:9000/api/issues/search?componentKeys=s0sta_shop&types=VULNERABILITY&resolved=false" | jq '.issues[].message'
  10. 10Debug a failed analysis

    sonar-scanner -X 2>&1 | tail -40

Let op

  • Use a project analysis token, not your user token, in CI.
  • fetch-depth: 0 is needed so Sonar knows "new code" from Git blame.
  • The SonarQube container needs vm.max_map_count=524288 on the host (Elasticsearch inside).

Bestanden

sonar-project.properties
Project settings.

Tips om te onthouden

  • Make the gate strict on new code only, so old projects can start without fixing everything first.

Exitcodes

0Success (gate passed if waited).
1Error or quality gate failed (with qualitygate.wait).

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man sonar-scanner