Continuous Integration With Jenkins — Part 2
Introduction:
In the previous article, we have installed Jenkins as a Docker Container in our AWS EC2 Machine and built a sample Spring Boot Project using the Classic Jenkins UI and Blue Ocean UI.
Note: Further in this series, we will configure Jenkins using the Classic UI and configure the Pipeline using Blue Ocean UI
In this article, we will discuss how to enable SonarQube in Jenkins Pipeline for Code Analysis and Quality check.
This series of articles includes:
Part 1: Setup a Jenkins Pipeline and Build the Spring Boot Source Code
Part 2: Enable SonarQube in the Pipeline for Code Review and Analysis.
Part 3: Wrap the application in a Docker image, build it in Jenkins, then push it to Docker Hub
SonarQube:
- SonarQube is an open-source platform developed for continuous inspection of code quality to perform automatic reviews with static analysis of code to detect bugs, code smells, and security vulnerabilities on 20+ programming languages.
Pre-requisite:
- Jenkins Pipeline with Build Stage (Covered in Part 1)
1. SonarQube Installation and Project Creation
We will install SonarQube as a Docker Container in EC2 machine using the below sonar-dockercompose.yaml file
Step 1: Run the below command to start the Jenkins server.
docker-compose -f sonar-dockercompose.yaml up
Note: If there is a problem with max_map_count while starting SonarQube, you may run the below UNIX command and start again
sudo sysctl -w vm.max_map_count=262144
Once the Sonar server is up, navigate the URL , http://{YourIP}:{YourPort} to access SonarQube.
Step 2: Create new Project
Step 3: Generate a new token, copy the generated token and Continue
Step 4: Choose the appropriate options for the project
Our project is created.
Once the analysis is done, SonarQube notifies Jenkins through Webhooks.
Step 5: To create Webhooks, select “Project Settings” and choose “Webhooks”
Step 5a: Create Webhook with the URL where Jenkins is up and running
2. SonarQube Configuration in Jenkins
Step 1: Add SonarQube Scanner and Sonar Quality Gates Plugin in Jenkins
Manage Jenkins -> Manage Plugins
SonarQube Scanner plugin helps us centralize the configuration of SonarQube server connection details in Jenkins global configuration.
Step 2: Add the generated token in Jenkins as Credentials
Manage Jenkins -> Manage Credentials
Step 3: Add the SonarQube Server with the generated token
Manage Jenkins->Configure System
3. Pipeline Configuration
We need to add new stages for Code Analysis and Quality Check in our Pipeline
Step 1: Open Blue Ocean and edit the Pipeline
Step 2: Add Code Analysis stage and add the below script in “Run arbitrary Pipeline script”
withSonarQubeEnv(‘SonarQube’)
{
sh “mvn sonar:sonar -Dsonar.projectKey=demo”
}
“SonarQube” is the name that we configured while adding SonarQube Server in Jenkins(Section 2, Step 3). “demo” is our project in SonarQube
Step 3: Add Quality Check stage and add the below script in “Run arbitrary Pipeline script”
def qualitygate = waitForQualityGate()
if (qualitygate.status != "OK")
{
error "Pipeline aborted due to quality gate coverage failure: ${qualitygate.status}"
}
Sonar way is the default Quality Gate used for Code Quality check.
Note : We can use this default one or create a Quality Gate and add the necessary conditions
Our Pipeline script looks like this:
Step 4: Save and Run the Pipeline
We can also view the result in SonarQube UI.
Note : The Quality Check stage is successful only when the Quality Gate status is “Ok”. We can see the log of the quality check in Webhooks
Select the highlighted icon to see the logs
Conclusion:
We have done the Code Analysis and the Quality Check using SonarQube in Jenkins. In the next article, we will dockerize our application and push it to Docker Hub using Jenkins.