Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
In modern software development, code quality plays a vital role in maintaining reliable and maintainable applications. As part of a DevSecOps or CI/CD workflow, integrating SonarQube with Jenkins helps automate static code analysis, detect vulnerabilities early, and enforce quality gates before deployment.
In this article, you’ll learn how to integrate SonarQube with a Jenkins Groovy pipeline step by step for seamless code quality analysis.
SonarQube is an open-source platform used to continuously inspect code quality. It analyzes your source code for:
It supports multiple languages such as Java, JavaScript, Python, Go, C#, and more.
Jenkins automates your build, test, and deployment workflows. When integrated with SonarQube, it adds automated code scanning to your CI/CD pipeline, ensuring every commit is tested for quality before deployment.
Key benefits:
Before you begin, ensure the following:
http://sonarqube.example.com).SonarQubehttp://sonarqube.example.comSonarScanner) and choose “Install automatically.”Here’s a sample Groovy pipeline integrating SonarQube:
stage('SonarQube Analysis') {
steps {
withSonarQubeEnv("${SONARQUBE}") {
script{
def scannerHome = tool 'SonarScanner'
sh """
echo "Running SonarQube Analysis..."
${scannerHome}/bin/sonar-scanner \
-Dsonar.projectKey=pdp-audit-be \
-Dsonar.sources=. \
-Dsonar.sourceEncoding=UTF-8
"""
}
}
}
}Or
Here’s a sample Groovy pipeline Direct integrating SonarQube:
For this you have to store the SonarQube Token into Jenkins Credentials and pass the Credentials ID as per the below example
stage('SonarQube Analysis') {
environment {
scannerHome = tool 'SonarScanner' // Tool name which you have set
}
steps {
withSonarQubeEnv('SonarQube') {
withCredentials([
usernamePassword(
credentialsId: 'sonarqube', // SonarQube Credintials ID
usernameVariable: 'SONAR_USER',
passwordVariable: 'SONAR_TOKEN'
)
]) {
sh """
${scannerHome}/bin/sonar-scanner \
-Dsonar.projectKey=PROJECT_NAME \ # replace it with your project name
-Dsonar.sources=. \
-Dsonar.host.url=SONARQUBE_URL \ # replace it with your sonarqube url
-Dsonar.token=${SONAR_TOKEN}
"""
}
}
}
}withSonarQubeEnv('SonarQube') injects SonarQube environment variables.waitForQualityGate ensures the build waits for SonarQube analysis results.You’ll see metrics such as:
You can also define Quality Gates in SonarQube (e.g., coverage > 80%, no critical issues).
Integrating SonarQube with Jenkins Groovy pipelines ensures continuous code quality checks as part of your CI/CD workflow. It not only maintains high standards for your codebase but also prevents vulnerabilities and technical debt from reaching production.
By automating code quality analysis, you enable developers to deliver cleaner, more secure, and maintainable software—faster.