Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 37 ยท CI Integration

SonarQube in GitLab CI, Jenkins and Azure DevOps Pipelines

Configure SonarQube analysis in GitLab CI, Jenkins (withSonarQubeEnv and waitForQualityGate) and Azure DevOps (Prepare, Analyze, Publish), with the settings each one needs.

Every CI system has its own way to store secrets, share caches and describe jobs, but the SonarQube parts are the same: provide the server URL and a token, make sure the git history is complete, run the scanner after tests, and check the quality gate. This lesson gives a working skeleton for the three other big platforms, GitLab CI, Jenkins and Azure DevOps, and ends with a stand-in that checks pipeline settings.

None of these tools run on this site; the files are real configuration for your own environment, and version numbers (images, task versions) should be checked against the vendor's current documentation.

GitLab CI

  1. In the GitLab project open Settings > CI/CD > Variables and add SONAR_TOKEN (masked, protected if you only analyse protected branches) and SONAR_HOST_URL.
  2. Add a job to .gitlab-ci.yml:
YAML
sonarqube-check:
  stage: test
  image:
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
  variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"
    GIT_DEPTH: "0"
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script:
    - sonar-scanner -Dsonar.qualitygate.wait=true
  allow_failure: false
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
    - if: $CI_COMMIT_BRANCH == 'main'
  • GIT_DEPTH: "0" disables shallow cloning.
  • The scanner picks up SONAR_TOKEN and SONAR_HOST_URL from the environment, and the project settings from sonar-project.properties.
  • Coverage reports from an earlier job must be passed with artifacts: and dependencies: or needs: so the scan job finds them.
  • In merge request pipelines, SonarQube Developer Edition and above can analyse the merge request and decorate it when the GitLab integration is configured (see Pull request analysis).

Jenkins

Install the SonarQube Scanner for Jenkins plugin. Then:

  1. Manage Jenkins > System > SonarQube servers: add a server with a name (for example sonar-main), the URL, and a Secret text credential holding the token.
  2. In a Pipeline job, wrap the scanner in withSonarQubeEnv, and use waitForQualityGate afterwards:
GROOVY
pipeline {
  agent any
  stages {
    stage('Build and test') {
      steps { sh 'npm ci && npm test -- --coverage' }
    }
    stage('SonarQube analysis') {
      steps {
        withSonarQubeEnv('sonar-main') {
          sh 'sonar-scanner'
        }
      }
    }
    stage('Quality gate') {
      steps {
        timeout(time: 10, unit: 'MINUTES') {
          waitForQualityGate abortPipeline: true
        }
      }
    }
  }
}

withSonarQubeEnv injects the server URL and token as environment variables. waitForQualityGate needs a webhook from SonarQube to Jenkins: in SonarQube choose Administration > Configuration > Webhooks and add https://jenkins.example.com/sonarqube-webhook/. When the server finishes processing the report, it calls that URL and the waiting stage resumes. Without the webhook, the stage waits until the timeout.

For the scanner tool itself, either install it on the agent, or configure a "SonarQube Scanner" tool in Jenkins and use tool 'name'.

Azure DevOps

Install the SonarQube extension in your Azure DevOps organisation. Then:

  1. Project settings > Service connections: create a SonarQube service connection with the server URL and token.
  2. In the pipeline, add three tasks around your build:
YAML
steps:
  - checkout: self
    fetchDepth: 0
  - task: SonarQubePrepare@7
    inputs:
      SonarQube: 'sonar-main'          # service connection name
      scannerMode: 'cli'
      configMode: 'manual'
      cliProjectKey: 'shop-frontend'
      cliSources: 'src'
  - script: npm ci && npm test -- --coverage
  - task: SonarQubeAnalyze@7
  - task: SonarQubePublish@7
    inputs:
      pollingTimeoutSec: '300'

Prepare configures the scanner, Analyze runs it, and Publish waits for the server's result and publishes the gate status to the build summary. For Maven and Gradle use scannerMode: 'Other' or 'MSBuild' for .NET, with the prepare task before the build. Azure Repos pull requests also need a build validation branch policy and the SonarQube branch policy settings, plus a token with permission to comment, to see decoration in the PR.

Same ideas, different words

Concept GitLab CI Jenkins Azure DevOps
Secret CI/CD variable Secret text credential Service connection
Full clone GIT_DEPTH: "0" Checkout option "advanced" depth 0 fetchDepth: 0
Run the scan sonar-scanner in a job withSonarQubeEnv SonarQubeAnalyze
Wait for gate sonar.qualitygate.wait=true waitForQualityGate + webhook SonarQubePublish
Cache .sonar/cache agent workspace Cache@2 task

Stand-in: settings checker

This stand-in takes a normalised description of a pipeline on any platform and reports the missing basics. The rules are the common denominators from the table above.

JS Stand-in: pipeline settings checklist

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Stand-in: webhook-driven wait

Jenkins' waitForQualityGate does not poll; the server pushes the result. The simulation below shows the two sides: the waiting stage and the webhook that resolves it.

JS Stand-in: waiting for a webhook

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What must be configured in SonarQube for Jenkins' waitForQualityGate to work?

    The server calls Jenkins when the report is processed; without it the stage waits until it times out.
  2. 2. Which GitLab variable disables shallow cloning?

    GIT_DEPTH: 0 fetches the full history, which SonarQube needs for blame and new code.
  3. 3. In Azure DevOps, which task waits for the analysis result?

    Publish polls the server and publishes the quality gate status.
  4. 4. Where does a Jenkins job get the SonarQube token?

    withSonarQubeEnv injects the server URL and the token stored as a Jenkins credential.

Summary

  • All platforms need URL, token, full history, tests before scan and a gate check.
  • GitLab uses CI variables and sonar-scanner; Jenkins uses withSonarQubeEnv plus a webhook; Azure DevOps uses Prepare/Analyze/Publish.
  • Check the version numbers of images and tasks in the vendor docs.
  • Keep the token out of the repository.

Next: Pull request analysis and waiting for the gate