Setup SonarQube with Jenkins Declarative Pipeline
2 min readAug 22, 2018
Before start, you should have done…
- Jenkins Multibranch Pipeline project setup with declarative pipeline Jenkinsfile. If not, please refer to this tutorial
sonar-project.properties
file at your project root
Prerequisite
- SonarQube Server
Installation Guide: Link
However, it’s easier to run a docker version, as always: Link - Jenskins Plugin: SonarQube Scanner for Jenkins
Setup
- Create SonarQube User Token at SonarQube Server
User > My Account > Security > Generate Tokens
- Configurate SonarQube webhook for quality gate
Administration > Configuration > Webhooks > Create
The URL should point to your Jenkins serverhttp://{JENKINS_HOST}/sonarqube-webhook/
- Setup SonarQube server at Jenkins
Manage Jenkins > Configure System > SonarQube servers
Enter you SonarQube server host and the token generated at previous step
- Setup SonarQube scanner at Jenkins
Manage Jenkins > Global Tool Configuration > SonarQube Scanner
Remember to give it a name, let’s take SonarQubeScanner as example here
- Setup SonarQube scan stage in declarative pipeline Jenkinsfile
stage('Sonarqube') {
environment {
scannerHome = tool 'SonarQubeScanner'
} steps {
withSonarQubeEnv('sonarqube') {
sh "${scannerHome}/bin/sonar-scanner"
} timeout(time: 10, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}
Noted that the tool name is the one you named for your SonarQube scanner at previous step
waitForQualityGate
will halt the pipeline until SonarQube notifies Jenkins whether quality gate is passed through webhook setup earlier
Now you should see SonarQube in your pipeline, happy coding 🐤
This post is tested with SonarQube 7.1 and Jenkins 2.121.3