- Install SonarQube Scanner plugin in jenkins
- Sonar Server > Administration > Security > Users > Token > Update Tokens > Generate
- Manage Jenkins > Configure System > SonarQube Servers
- Check Environment variables injection
- Add Sonarqube installations with Server authentication
- Kind: Secret text, Secret: token generated in step 2
- Configure Jenkinsfile with
withSonarQubeEnv
step
node {
stage('SCM') {
git 'https://github.com/foo/bar.git'
}
stage('SonarQube analysis') {
withSonarQubeEnv() { // Will pick the global server connection you have configured
sh './gradlew sonarqube'
}
}
}
- Sonar Server > Administration > Configuration > Webhook
- URL:
http://jenkins:8080/sonarqube-webhook
Secret to be empty
- Update Jenkinsfile with following
pipeline {
agent { label 'linux' }
options {
buildDiscarder(logRotator(numToKeepStr: '5'))
}
stages {
stage('SonarQube analysis') {
withSonarQubeEnv() { // Will pick the global server connection you have configured
sh './gradlew sonarqube'
}
}
stage("Quality Gate") {
steps {
timeout(time: 2, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}
}
}
Reference
- How to Integrate SonarQube With Jenkins - YouTube
- java-web-app/Jenkinsfile-2 at sonar ยท darinpope/java-web-app
- SonarScanner for Jenkins | SonarQube Docs