TExcelTAD
Home › CI/CD Pipelines & Pipeline Security › SonarQube & Quality Gates

SonarQube & Quality Gates

SonarQube is the other big name in static analysis, and it works differently from Semgrep. Instead of a CLI that prints findings, it is a server with a database: a scanner in the pipeline sends the analysis to the server, which tracks bugs, vulnerabilities, "code smells", duplication and test coverage over time, per branch and per pull request, and decides whether the change passes a quality gate.

Product What it is
SonarQube Server the self-hosted server (Community Build: free, main branch only; commercial editions add branch and PR analysis, more languages and security rules)
SonarQube Cloud the SaaS version (free for public projects)
SonarQube for IDE the editor plugin (formerly SonarLint), giving the same findings while you type
SonarScanner the CLI and build plugins (Maven, Gradle, .NET) that run the analysis in CI

Because it needs a running server and a database, SonarQube can't run in this course's sessions; everything below is sample configuration and output. Check the edition matrix on Sonar's site before relying on a feature: branch and PR analysis in particular depend on the edition.

Quality gates

A quality gate is a set of conditions on new code (code changed in the PR, or since a "new code period" such as the previous version). The built-in "Sonar way" gate fails when new code has, for example:

  • any new issue of a high-severity category (reliability or security rating worse than A),
  • security hotspots that haven't been reviewed,
  • coverage on new code below 80 %,
  • more than 3 % duplicated lines in new code.

Focusing on new code is the same idea as Semgrep's baseline: old debt is visible on the dashboard, but it doesn't block a change that didn't create it.

The scanner in a pipeline

Project settings live in sonar-project.properties at the repository root:

ini
sonar.projectKey=acme_shop
sonar.projectName=shop
sonar.sources=app
sonar.tests=tests
sonar.python.version=3.13
sonar.python.coverage.reportPaths=coverage.xml
sonar.qualitygate.wait=true

sonar.qualitygate.wait=true makes the scanner wait for the server to compute the gate and fail the CI job if it's red; without it, the job succeeds as soon as the report is uploaded.

yaml
# GitHub Actions (non-run). SONAR_TOKEN is a project analysis token stored as a secret.
sonar:
  runs-on: ubuntu-24.04
  needs: test                                   # coverage.xml comes from the test job
  steps:
    - uses: actions/checkout@v7
      with:
        fetch-depth: 0                          # full history: blame data and new-code detection
    - uses: actions/download-artifact@v8
      with: { name: coverage }
    - uses: SonarSource/sonarqube-scan-action@<commit-sha>   # pin by SHA
      env:
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        SONAR_HOST_URL: ${{ vars.SONAR_HOST_URL }}
yaml
# GitLab CI (non-run)
sonarqube-check:
  image: sonarsource/sonar-scanner-cli:latest   # pin a version or digest in real pipelines
  variables:
    GIT_DEPTH: "0"
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"
  cache:
    key: sonar
    paths: [.sonar/cache]
  script:
    - sonar-scanner
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

A run whose gate fails ends like this (sample output, shortened):

bash
sonar-scanner -Dsonar.host.url="$SONAR_HOST_URL" -Dsonar.token="$SONAR_TOKEN"
INFO  Scanner configuration file: /opt/sonar-scanner/conf/sonar-scanner.properties
INFO  Project root configuration file: /builds/acme/shop/sonar-project.properties
INFO  Analyzing on SonarQube server 2025.4.2
INFO  Load quality profiles
INFO  Sensor Python Sensor [python]
INFO  1 source file to be analyzed
INFO  Sensor Cobertura Sensor for Python coverage [python]
INFO  Analysis report uploaded
INFO  ------------- Check Quality Gate status
INFO  Waiting for the analysis report to be processed (max 300s)
ERROR QUALITY GATE STATUS: FAILED - View details on https://sonar.example.com/dashboard?id=acme_shop&pullRequest=42
ERROR Error during SonarScanner Engine execution

Semgrep or SonarQube?

Semgrep (OSS CLI) SonarQube
Runs a CLI, no server scanner + server + database
Strength custom rules in minutes; security patterns; fast broad quality tracking (bugs, smells, coverage, duplication) over time, dashboards, PR decoration
Gate exit code (--error), your own policy quality gate on new code, configured on the server
Cost free CLI; paid platform for cross-file analysis and management free Community Build; paid editions for branches/PRs and deeper security analysis

Many teams run both: SonarQube for code quality and coverage trends, Semgrep for fast, custom security rules, and both feed the PR.

Test yourself

Why can't SonarQube analysis run as a single offline CLI command like Semgrep?

  1. It only supports Java
  2. The scanner sends results to a SonarQube server, which stores history and computes the quality gate
  3. It needs GPUs
  4. It requires Docker

What does sonar.qualitygate.wait=true change in CI?

  1. The scan runs faster
  2. The scanner waits for the gate result and fails the job if the gate fails
  3. It skips the upload
  4. It disables PR analysis

Why do quality gates focus on new code?

  1. Old code can't be analysed
  2. So existing debt doesn't block changes that didn't create it, while new code must meet the bar
  3. To save server resources
  4. Licensing rules