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 to trigger analysis with:
- SonarQube Scanner
- SonarQube Scanner for Maven
- SonarQube Scanner for Mave
- SonarScanner for MSBuild
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.
SINCE 2.5 : you can also use Jenkins Pipeline DSL (some features require SonarQube >= 6.2).
SonarQube Scanner for Jenkins
|2.0||2.1||2.2.x||2.3 - 2.4.x||2.5||2.6||2.7 - 2.8.1|
SINCE 2.5 Analysis must run with a JRE8
- Install the SonarQube Scanner for Jenkins 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 on Add SonarQube, and add the values you're prompted for.
Analyzing with the SonarQube Scanner
This step is mandatory if you want to trigger any of your SonarQube analyses with the SonarQube Scanner. You can define as many scanner instances as you wish. Then for each Jenkins job, you will be able to choose with 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 SonarQube Scanner configuration section and click on Add SonarQube Scanner. It is based on the typical Jenkins tool auto-installation. You can either choose to point to an already installed version of SonarQube Scanner (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 SonarQube Scanner 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 'Check Now' button in Manage Plugins > Advanced tab.
- Configure the project, and scroll down to the Build section.
- Add the SonarQube Scanner 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
Analyzing with SonarQube Scanner for MSBuild
This step is mandatory if you want to trigger any of your analyses with the SonarQube Scanner for MSBuild. You can define as many scanner instances as you wish. Then for each Jenkins job, you will be able to choose with 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 SonarQube Scanner for MSBuild
Add an installation of the latest available version. Check Install automatically to have the SonarQube Scanner 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
- Configure the project, and scroll down to the Build section.
- Add both the SonarQube for MSBuild - Begin Analysis and SonarQube for MSBuild - End Analysis build steps to your build
- Configure the SonarQube Project Key, Name and Version in the SonarQube Scanner for MSBuild - Begin Analysis build step
Use the MSBuild build step or the Execute Windows batch command to execute the build with MSBuild 14 (see compatibility) between the Begin Analysis and End Analysis steps.
Analyzing with SonarQube Scanner for Maven or Gradle
- 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
- Configure the project, and scroll down to the Build Environment section.
Enable Prepare SonarQube Scanner 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.
Press the help button to learn which variables you can use in your build. Some values may be blank, depending on what was defined for the server.
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:
In both cases, launching your analysis may require authentication. In that case, make sure that the Global Configuration defines a valid SonarQube token, and add it to the Maven goal or Gradle task with the following argument and value:
Analyzing in a Jenkins pipeline
Since version 2.5 of the SonarQube Scanner for Jenkins, there is an official support of Jenkins pipeline. We provide a '
withSonarQubeEnv' block that allow 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.
Support of pipeline only works with SonarQube >= 5.2.
Here are a some examples for every scanner, assuming you run on Unix slaves and you have configured a server named 'My SonarQube Server' as well as required tools. If you run on Windows slaves, just replace 'sh' by 'bat'.
Pause pipeline until quality gate is computed
waitForQualityGate step will pause the pipeline until SonarQube analysis is completed and returns quality gate status.
- SonarQube server 6.2+ (need webhook feature)
- Configure a webhook in your SonarQube server pointing to
<your Jenkins instance>/sonarqube-webhook/The trailing slash is mandatory with SonarQube 6.2 and 6.3!
withSonarQubeEnvstep in your pipeline (so that SonarQube taskId is correctly attached to the pipeline context).
Example (scripted pipeline):
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 (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.
Example (declarative pipeline):
- If you want to run multiple analysis in the same pipeline and use waitForQualityGate, it works starting from version 2.8, but you have to do everything in order: