Table of contents
- π οΈ Prerequisites
- π What is Community Branch Analysis?
- π Step 1: Install Docker & Create a Container for SonarQube with the Plugin
- π§ Step 2: Install Jenkins Plugins (SonarQube Scanner & Eclipse Temurin Installer)
- βοΈ Step 3: Configure Tools in Jenkins
- π Step 4: Create a Token in SonarQube
- π Step 5: Configure SonarQube Server in Jenkins
- π Step 6: Write a Jenkins Pipeline for the Job
- ποΈ Step 7: Build the Job & Analyze the Report
- π― Conclusion
SonarQube is a powerful tool for code quality and security. With the Community Branch Plugin, you can extend its functionality to include branch analysis, even in the Community Edition. Let's walk through setting up this tool with Jenkins in a Dockerized environment and analyze code like a pro! π
π οΈ Prerequisites
Before diving into the setup, ensure you have:
Docker installed π³
Jenkins set up π»
Basic knowledge of Maven and CI/CD workflows π
π What is Community Branch Analysis?
Branch analysis lets you review code quality across different branches, ensuring consistency and standards. The Community Branch Plugin brings this feature to SonarQube's Community Edition, giving you premium capabilities for free! π
π Step 1: Install Docker & Create a Container for SonarQube with the Plugin
Install Docker:
sudo apt update sudo apt install docker.io
Create a Docker Container with the Plugin:
docker run -d -p 9000:9000 mc1arke/sonarqube-with-community-branch-plugin
π§ Step 2: Install Jenkins Plugins (SonarQube Scanner & Eclipse Temurin Installer)
Log into your Jenkins instance.
Go to Manage Jenkins > Plugin Manager.
Search for:
SonarQube Scanner π
Eclipse Temurin Installer β
Install both plugins and restart Jenkins.
βοΈ Step 3: Configure Tools in Jenkins
JDK Configuration:
Go to Manage Jenkins > Global Tool Configuration.
Under JDK, add a new JDK and select "Install Automatically."
SonarQube Scanner Configuration:
Add SonarQube Scanner and configure its installation.
Maven Configuration:
Add Maven and configure its installation.
π Step 4: Create a Token in SonarQube
Log in to SonarQube (localhost:9000).
Navigate to My Account > Security > Tokens.
Generate a new token and save it securely. Youβll need it in Jenkins.
π Step 5: Configure SonarQube Server in Jenkins
Go to Manage Jenkins > Configure System.
Scroll to SonarQube Servers and click Add SonarQube.
Provide:
Server Name.
SonarQube URL (e.g., localhost:9000).
Add the token under Server Authentication Tokens.
π Step 6: Write a Jenkins Pipeline for the Job
Create a Jenkins pipeline with the following script:
pipeline {
agent any
tools{
maven 'maven3'
jdk 'jdk17'
}
environment{
SCANNER_HOME=tool 'sonar-scanner'
}
stages {
stage('Git Checkout') {
steps {
git branch: 'dev', url: 'https://github.com/Ank911007/FullStack-Blogging-App-.git'
}
}
stage('Compile') {
steps {
sh 'mvn compile'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('SonarQube Analysis') {
steps {
withSonarQubeEnv('sonar-server') {
sh '''$SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=bloggingApp -Dsonar.projectKey=bloggingApp \
-Dsonar.java.binaries=target -Dsonar.branch.name=dev '''
echo "$SCANNER_HOME"
}
}
}
stage('Quality Gate Check') {
steps {
timeout(time: 1, unit: 'HOURS') {
waitForQualityGate abortPipeline: false
}
}
}
}
}
ποΈ Step 7: Build the Job & Analyze the Report
Run the pipeline in Jenkins.
Once the build completes, go to SonarQube at localhost:9000.
View detailed analysis under your projectβs dashboard. π
π― Conclusion
With these steps, you've successfully integrated the Community Branch Plugin into your CI/CD workflow. You now have the power to analyze code quality across branches with ease! π
Pro Tip: Keep iterating and optimizing your pipeline for faster feedback and improved code quality. Happy coding! π»β¨