How to Use SonarQube with Jenkins for Code Quality Analysis
To use
SonarQube with Jenkins, install the SonarQube plugin in Jenkins, configure the SonarQube server in Jenkins settings, and add a SonarQube analysis step in your Jenkins pipeline or freestyle job. This setup lets Jenkins automatically scan your code for quality issues during builds.Syntax
Here is the basic syntax to add SonarQube analysis in a Jenkins pipeline using the withSonarQubeEnv and sonarScanner steps.
withSonarQubeEnv('SonarQubeServerName'): Sets up environment variables for SonarQube analysis.sh 'sonar-scanner': Runs the SonarQube scanner command to analyze the code.
groovy
pipeline {
agent any
stages {
stage('SonarQube Analysis') {
steps {
withSonarQubeEnv('MySonarQubeServer') {
sh 'sonar-scanner'
}
}
}
}
}Example
This example shows a complete Jenkins pipeline that checks out code, builds it, and runs SonarQube analysis using the SonarQube plugin.
groovy
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git 'https://github.com/example/repo.git'
}
}
stage('Build') {
steps {
sh './gradlew build'
}
}
stage('SonarQube Analysis') {
steps {
withSonarQubeEnv('MySonarQubeServer') {
sh 'sonar-scanner'
}
}
}
}
}Output
[Pipeline] stage\n[Pipeline] { (SonarQube Analysis)\n[Pipeline] withSonarQubeEnv\nSonarQube Scanner 4.6.2.2472\nINFO: Scanner configuration file: /var/jenkins_home/.sonar/cache/sonar-scanner.properties\nINFO: Project root configuration file: /workspace/sonar-project.properties\nINFO: SonarQube analysis started\nINFO: Analysis report generated in 123ms, dir size=456 KB\nINFO: Analysis report compressed in 78ms\nINFO: Analysis report uploaded in 234ms\nINFO: ANALYSIS SUCCESSFUL\n[Pipeline] }\n[Pipeline] // withSonarQubeEnv\n[Pipeline] }
Common Pitfalls
- Not installing the SonarQube plugin in Jenkins before configuring the job.
- Forgetting to configure the SonarQube server URL and authentication token in Jenkins global settings.
- Running
sonar-scannerwithout proper environment variables set bywithSonarQubeEnv. - Not having a
sonar-project.propertiesfile or proper scanner configuration in the project.
groovy
pipeline {
agent any
stages {
stage('SonarQube Analysis') {
steps {
// Wrong: Missing withSonarQubeEnv causes scanner to fail
sh 'sonar-scanner'
}
}
}
}
// Correct way:
pipeline {
agent any
stages {
stage('SonarQube Analysis') {
steps {
withSonarQubeEnv('MySonarQubeServer') {
sh 'sonar-scanner'
}
}
}
}
}Quick Reference
Summary tips for integrating SonarQube with Jenkins:
- Install the SonarQube plugin in Jenkins from Manage Plugins.
- Configure SonarQube server in Manage Jenkins > Configure System with URL and token.
- Use
withSonarQubeEnv('ServerName')in pipeline to set environment. - Run
sonar-scannercommand inside that environment. - Ensure your project has
sonar-project.propertiesor scanner config.
Key Takeaways
Install and configure the SonarQube plugin and server in Jenkins before use.
Use withSonarQubeEnv to set environment variables for the scanner in pipelines.
Run sonar-scanner command inside the withSonarQubeEnv block for analysis.
Ensure your project has proper SonarQube configuration files.
Common errors come from missing plugin, server config, or environment setup.