Continuous Integration With Jenkins — Part 2

Priya sivakumar
5 min readApr 7, 2021

--

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

Integration of SonarQube and Jenkins

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

sonar-dockercompose.yaml

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.

SonarQube Home Page

Step 2: Create new Project

Create Project

Step 3: Generate a new token, copy the generated token and Continue

Generate token
Token generated

Step 4: Choose the appropriate options for the project

Project Analysis

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”

Project Settings → Webhooks

Step 5a: Create Webhook with the URL where Jenkins is up and running

Create Webhook
Webhook Created

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.

SonarQube Scanner and SonarQube Quality Gates Plugins

Step 2: Add the generated token in Jenkins as Credentials

Manage Jenkins -> Manage Credentials

Manage Credentials

Step 3: Add the SonarQube Server with the generated token

Manage Jenkins->Configure System

Add SonarQube Server

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

Pipeline Configuration

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

Code Analysis Stage

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

Quality Gates — Sonar way

Our Pipeline script looks like this:

Pipeline script/Jenkinsfile

Step 4: Save and Run the Pipeline

Pipeline execution — Success

We can also view the result in SonarQube UI.

Code Analysis — Success

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

Quality Check logs — Webhooks
Quality gate logs — Webhooks

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.

--

--