Implementing Continuous Integration/Continuous Deployment with Jenkins and SonarQube

Muhammad Umar Al Fajar
5 min readNov 27, 2023

--

In the world of software development, Continuous Integration/Continuous Deployment (CI/CD) has become an essential practice for teams looking to deliver high-quality code quickly and efficiently. In this article, we’ll explore how to set up a CI/CD pipeline using Jenkins and SonarQube in a project.

Create Spring Boot Project

For this project we only need a simple project that installed SonarQube and JaCoCo plugin and implemented unit test. You can create your own project or just simply clone mine (Don’t forget to configure the database connection in application properties):

mumaralfajar/task-management-system (github.com)

In that project I’ve provided APIs and some unit tests using Junit.

Create Container for SonarQube and Jenkins

docker-compose.yaml

version: '3'
services:
sonarqube:
image: sonarqube:latest
ports:
- 9000:9000
volumes:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_extensions:/opt/sonarqube/extensions
- sonarqube_logs:/opt/sonarqube/logs

jenkins:
image: jenkins/jenkins:lts
ports:
- 8080:8080
- 50000:50000
volumes:
- jenkins_home:/var/jenkins_home

volumes:
sonarqube_data:
sonarqube_extensions:
sonarqube_logs:
jenkins_home:

After the container is running, access the SonarQube through browser (http://localhost:9000).

If the login page shows up, then it means the SonarQube runs perfectly. For the first time login with username: admin and password: admin.

After that the SonarQube will tell you to change the password, remember that or keep that in somewhere safe.

After changing your password, you will be redirected to the main page, choose to manually create a local project.

For the project setu up, just keep it simple by choosing to use the global setting.

After the project was created, choose Locally for how the project will be analyzed.

Name the token, and then generate the token.

save the generated token in somewhere save.

After that click Continue and then you will be given a build configuration, I choose maven since I’m using maven for the project.

Add it to pom.xml in <properties/>

<!-- SonarQube properties -->
<sonar.projectKey>task-management-system</sonar.projectKey>
<sonar.projectName>task-management-system</sonar.projectName>
<sonar.host.url>http://localhost:9000</sonar.host.url>
<sonar.login>sqp_1d331e503727c363d9e8606eb37aea1a17236d6c</sonar.login>

If it’s done, then run:

mvn clean verify sonar:sonar

Now go to the project overview in SonarQube to see:

Setup Jenkins

For the first time, you will need to enter a password. You can find it in the Jenkins container’s log.

Next, just install suggested plugins.

After creating the admin user, you will be redirected to dashboard. While in the dashboard, choose manage Jenkins and choose plugins.

Install the SonarQube Scanner plugin, and then just restart the Jenkins container in Docker.

Next, go to Manage Jenkins — System, add SonarQube installations. Then add an authentication token by clicking add and then Jenkins.

Choose secret text. Then enter the token in the Secret form.

If done, then save.

Go to Manage Jenkins — Tools. Add the Java home.

Don’t forget the SonarQube Scanner.

Also add the maven. Then click save.

Then go to the dashboard and click new item to create a job. Give it a name and choose Pipeline.

Next, in the General section, check the GitHub project and fill in the project URL with our GitHub project URL.

Go to the Pipeline section and provide your Git repo URL. After that we will create new credentials by clicking Add and then Jenkins.

Before that, we have to add the ssh key of the Jenkins container to our GitHub. Enter the Jenkins container and generate the ssh key.

Now, copy the private key with:

cat .<your/directory>id_rsa

in my case:

cat ./var/jenkins_home/.ssh/id_rsa

Copy from the word “begin” to “end”. Then go back to the web Jenkins, choose SSH Username with private key. For ID and Description, it is optional, but for Username, please fill in your friends’ GitHub Username. Enter the private key that you have copied earlier and enter the passphrase that you created when generating the ssh key. If done, click add.

Go to the Jenkins container and copy the public ssh key by using:

cat .<your/directory>id_rsa.pub

Or, in my case:

cat ./var/jenkins_home/.ssh/id_rsa.pub

Then, add it to your GitHub account.

After that, back to Jenkins and click save.

Go to the Jenkins container and do this command to install maven:

apt-get update
apt-get install maven

And then, click build now in Jenkins.

Finally, go to SonarQube to see the result.

--

--