Jenkins extension for SonarQube
This plugin lets you centralize the configuration of SonarQube server connection details in Jenkins global configuration.
Then you can trigger SonarQube analysis from Jenkins using standard Jenkins Build Steps or Jenkins Pipeline DSL to trigger analysis with:
Once the job is complete, the plugin will detect that a SonarQube analysis was made during the build and display a badge and a widget on the job page with a link to the SonarQube dashboard as well as quality gate status.
Installation
- Install the Jenkins Extension for SonarQube via the Jenkins Update Center.
- Configure your SonarQube server(s):
- Log into Jenkins as an administrator and go to Manage Jenkins > Configure System.
- Scroll down to the SonarQube configuration section, click Add SonarQube, and add the values you're prompted for.
- The server authentication token should be created as a Secret Text credential.
Analyzing a .NET solution
In version 5.0 of the SonarScanner, we changed the name of the SonarScanner for MSBuild to SonarScanner for .NET.
The documentation is updated with the new name and we will call the scanner SonarScanner for .NET moving forward.
Global configuration
This step is mandatory if you want to trigger any of your analyses with the SonarScanner for .NET. You can define as many scanner instances as you wish. Then for each Jenkins job, you will be able to choose which launcher to use to run the SonarQube analysis.
- Log into Jenkins as an administrator and go to Manage Jenkins > Global Tool Configuration
- Click on Add SonarScanner for MSBuild
- Add an installation of the latest available version. Check Install automatically to have the SonarScanner for MSBuild automatically provisioned on your Jenkins executors
If you do not see any available version under Install from GitHub, first go to Manage Jenkins > Manage Plugins > Advanced and click on Check now
Job configuration
- Configure the project, and go to the Build section.
- Add the SonarQube for MSBuild - Begin Analysis to your build
- Configure the SonarQube Project Key, Name, and Version in the SonarScanner for MSBuild - Begin Analysis build step
- Add the compatible MSBuild build step or the Execute Windows batch command to execute the build.
- Add the SonarQube for MSBuild - End Analysis build steps to your build
Analyzing a Java project with Maven or Gradle
Global configuration
- Log into Jenkins as an administrator and go to Manage Jenkins > Configure System
- Scroll to the SonarQube servers section and check Enable injection of SonarQube server configuration as build environment variables
Job configuration
- Configure the project, and go to the Build Environment section.
- Enable Prepare SonarScanner environment to allow the injection of SonarQube server values into this particular job. If multiple SonarQube instances are configured, you will be able to choose which one to use. Once the environment variables are available, use them in a standard Maven build step (Invoke top-level Maven targets) by setting the Goals to include, or a standard Gradle build step (Invoke Gradle script) by setting the Tasks to execute.
Maven goal:
Gradle task:
In both cases, launching your analysis may require authentication. In that case, make sure that the global configuration defines a valid SonarQube token.
Analyzing other project types
Global configuration
This step is mandatory if you want to trigger any of your SonarQube analyses with the SonarScanner CLI. You can define as many scanner instances as you wish. Then for each Jenkins job, you will be able to choose which launcher to use to run the SonarQube analysis.
- Log into Jenkins as an administrator and go to Manage Jenkins > Global Tool Configuration
- Scroll down to the SonarScanner configuration section and click on Add SonarScanner. It is based on the typical Jenkins tool auto-installation. You can either choose to point to an already installed version of SonarScanner CLI (uncheck 'Install automatically') or tell Jenkins to grab the installer from a remote location (check 'Install automatically')
If you don't see a drop-down list with all available SonarScanner CLI versions but instead see an empty text field then this is because Jenkins still hasn't downloaded the required update center file (default period is 1 day). You may force this refresh by clicking the 'Check Now' button in Manage Plugins > Advanced tab.
Job configuration
- Configure the project, and go to the Build section.
- Add the SonarScanner CLI build step to your build.
- Configure the SonarQube analysis properties. You can either point to an existing sonar-project.properties file or set the analysis properties directly in the Analysis properties field.
Using a Jenkins pipeline
We provide a withSonarQubeEnv
block that allows you to select the SonarQube server you want to interact with. Connection details you have configured in Jenkins global configuration will be automatically passed to the scanner.
If needed you can override the credentialsId
if you don't want to use the one defined in global configuration (for example if you define credentials at the folder level).
If you only need the SonarQube environment variables to be expanded in the build context then you can override the envOnly
flag.
Here are some examples for every scanner, assuming you run on Unix slaves and you have configured a server named "My SonarQube Server" as well as the required tools. If you run on Windows slaves, just replace sh
with bat
.
SonarScanner CLI:
SonarScanner for Gradle:
SonarScanner for Maven:
SonarScanner for .NET:
Pause pipeline until the quality gate is computed
The waitForQualityGate
step will pause the pipeline until SonarQube analysis is completed and returns Quality Gate status.
Prerequisites:
- Configure a webhook in your SonarQube server pointing to
<your Jenkins instance>/sonarqube-webhook/
- Use
withSonarQubeEnv
step in your pipeline (so that SonarQube taskId is correctly attached to the pipeline context).
Scripted pipeline example:
Thanks to the webhook, the step is implemented in a very lightweight way: no need to occupy a node doing polling, and it doesn't prevent Jenkins to restart (the step will be restored after restart). Note that to prevent race conditions, when the step starts (or is restarted) a direct call is made to the server to check if the task is already completed.
Declarative pipeline example:
If you want to run multiple analyses in the same pipeline and use waitForQualityGate
you have to do everything in order:
Configuring a webhook secret
If you want to verify the webhook payload that is sent to Jenkins, you can add a secret to your webhook on SonarQube.
To set the secret:
- In Jenkins, navigate to Manage Jenkins > Configure System > SonarQube Server > Advanced > Webhook Secret and click the Add button.
- Select Secret text and give the secret an ID.
- Select the secret from the dropdown menu.
If you want to override the webhook secret on a project level, you can add the secret to Jenkins and then reference the secret ID when calling waitForQualityGate
.
if your pipeline is declarative or
if your pipeline is scripted.
Was this page helpful?