DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Implementing CI/CD Pipelines With Jenkins and Docker
  • Java CI/CD: From Local Build to Jenkins Continuous Integration
  • Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags
  • Pipeline as a Service: How To Test Pipelines in GitLab

Trending

  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • How to Practice TDD With Kotlin
  • A Guide to Container Runtimes
  • Solid Testing Strategies for Salesforce Releases
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. DevOps and CI/CD
  4. Jenkins Pipeline With Sonarqube and Gitlab

Jenkins Pipeline With Sonarqube and Gitlab

In this guide, we are going to deploy a continuous integration process between Jenkins, GitLab, and SonarQube. The end goal will be to review the code qualit...

By 
Sudip Sengupta user avatar
Sudip Sengupta
DZone Core CORE ·
May. 22, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
33.8K Views

Join the DZone community and get the full member experience.

Join For Free

In this guide, we are going to deploy a continuous integration process between Jenkins, GitLab, and SonarQube.

The end goal will be to review the code quality through SonarQube for GitLab repository using Jenkins. Following is the process flow we need to manage:

  • Push code to GitLab from the local machine.
  • Next, checkout code and run tests in Jenkins, here it is acting as a Continuous Integrator.
  • Generate quality reports via SonarQube as a code analyzer.

Setup Jenkins, SonarQube, and GitLabs

In case, if you haven't set up the required software yet, go through the below-mentioned steps:

Step 1:

Make sure that your host machine has enough power to run these components, as GitLab alone requires 4 GB of RAM to perform its operations smoothly. To run all three together, you need at least 8 GB of RAM and 20 GB of hard disk space.

Step 2:

For SonarQube, you need to set the recommended values as a root user on the host machine:

Shell
 




xxxxxxxxxx
1


 
1
$ sysctl -w vm.max_map_count=262144 
2
$ sysctl -w fs.file-max=65536 
3
$ ulimit -n 65536 
4
$ ulimit -u 4096 



Step 3:

For this guide, we are going to use a simple docker-compose.yml file. However, you are free to edit the file as per your requirement.

Before launching the containers, please make sure that you change the value of GITLAB_SECRETS_DB_KEY_BASE, GITLAB_HOST, SONARQUBE_JDBC_PASSWORD, and POSTGRES_PASSWORD. Although, along with this, you can also modify the define ports in the file.

Dockerfile
 




xxxxxxxxxx
1
69


 
1
version: "3.2"
2
 
3
services:
4
   jenkins:
5
       image: jenkinsci/jenkins:lts
6
       container_name: jenkins-container
7
       restart: always
8
       ports:
9
           - "8080:8080"
10
           - '50000:50000'
11
       volumes:
12
       - '/var/run/docker.sock:/var/run/docker.sock'
13
       - '/data/jenkins/jenkins_home:/var/jenkins_home'
14
       environment:
15
       - "JENKINS_OPTS=--prefix=/jenkins"
16
 
17
   gitlab:
18
       image: gitlab/gitlab-ce:latest
19
       container_name: gitlab-container
20
       restart: always
21
       environment:
22
           - GITLAB_SECRETS_DB_KEY_BASE=long-and-random-alphanumeric-string
23
           - GITLAB_HOST=your-host-ip-here
24
           - GITLAB_PORT=10080
25
           - GITLAB_SSH_PORT=10022
26
       ports:
27
           - "10080:80"
28
           - "10022:22"
29
       volumes:
30
       - '/data/gitlab/config:/etc/gitlab'
31
       - '/data/gitlab/logs:/var/log/gitlab'
32
       - '/data/gitlab/data:/var/opt/gitlab'
33
 
34
   sonarqube:
35
       image: sonarqube:7.9.2-community
36
       container_name: sonar-container
37
       restart: always
38
       networks:
39
           - sonarnet
40
       environment:
41
           - SONARQUBE_JDBC_USERNAME=sonar
42
           - SONARQUBE_JDBC_PASSWORD=your-strong-password-here
43
           - SONARQUBE_JDBC_URL=jdbc:postgresql://postgressdb:5432/sonarqube
44
       ports:
45
       - "9000:9000"
46
       - "9092:9092"
47
       volumes:
48
       - '/data/sonar/sonarqube_conf:/opt/sonarqube/conf'
49
       - '/data/sonar/sonarqube_data:/opt/sonarqube/data'
50
       - '/data/sonar/sonarqube_extensions:/opt/sonarqube/extensions'
51
       - '/data/sonar/sonarqube_bundled-plugins:/opt/sonarqube/lib/bundled-plugins'
52
  
53
   postgressdb:
54
       image: postgres:12.1
55
       container_name: postgres-container
56
       restart: always
57
       networks:
58
           - sonarnet
59
       environment:
60
           - POSTGRES_USER=sonar
61
           - POSTGRES_PASSWORD=your-strong-password-here
62
           - POSTGRES_DB=sonarqube
63
       volumes:
64
       - '/data/postgresql/sonarqube_db:/var/lib/postgresql'
65
       - '/data/postgresql/postgresql_data:/var/lib/postgresql/data'
66
 
67
networks:
68
   sonarnet:
69
    driver: bridge-



Now, run the following command to launch the stack:

Shell
 




xxxxxxxxxx
1


 
1
$ docker-compose up -d 



Note that the installation process is going to take some time, so sit back and have some coffee. You deserve it!

Step 4:

Now, to verify if all systems are up and running, run this command:

Java
 




xxxxxxxxxx
1


 
1
$ docker ps
2

            
3
CONTAINER ID    IMAGE                     COMMAND             CREATED         STATUS                  PORTS                                                   NAMES
4
f54e223417fa    gitlab/gitlab-ce:latest   "/assets/wrapper"   1 minute ago    1 minute ago (healthy)  443/tcp, 0.0.0.0:10022->22/tcp, 0.0.0.0:10080->80/tcp   gitlab-container
5
9102f03f69b8    sonarqube                 "./bin/run.sh"      1 minute ago    1 minute ago            0.0.0.0:9000->9000/tcp                                  sonar-container
6
05f554da39fc    jenkinsci/jenkins:lts     "/bin/tini --..."   1 minute ago    1 minute ago            0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp        jenkins-container



If all went well, then you can access all three of them by following mentioned URLs:

As you are visiting your Jenkins environment for the first time, it is going to prompt you to enter the administrator password, which was generated during the installation process. To access the password from the container, you can run the following command:

Shell
 




xxxxxxxxxx
1


 
1
$ docker exec -it <your-jinkins-container-id> cat /var/jenkins_home/secrets/initialAdminPassword 
2
$ gSzHTGfbtXSpcBXMyJ6gzmNfNH7BjhK23 



Once you enter the password, you can select the option Install suggested plugin. Wait until the download finishes.

For SonarQube, the default username and password are admin and admin, respectively. Whereas, for GitLab, you will be prompted to enter the new password for the default username root.

Configuring Jenkins Pipeline with SonarQube and GitLab integration

SonarQube Configuration:
We will begin with SonarQube. To connect it with Jenkins, you need to generate the token to access the SonarQube instance.

Login into the SonarQube dashboard and go to the Administrator tab. In the Security drop-down menu, select User tab as shown in the below image:

Selecting User tab

Here you will find Administrator user, for which you are going to generate the access token:

Admin user access tokens

In the pop-up that displays on your screen, enter the desired name and click on the Generate button. Copy the newly generated token and save it somewhere safe, as you won't be able to view/copy the generated token again.

Tokens administrator

GitLab Configuration:
To establish a connection between GitLab and Jenkins, again, we need to generate an access token, which can be done as mentioned below:

To generate an access token, go to your GitLab dashboard and follow Dashboard > Setting > Access Tokens. Once you have the token with you, copy it for further use.

Access tokens dashboard

Integrating SonarQube and GitLabs in Jenkins:
This guide assumes that you have already done the basic setup for Jenkins. Now, let's move to configure GitLab and SonarQube in the Jenkins server.

First, we are going to integrate SonarQube. Go Dashboard > Manage Jenkins > Manage Plugins and search for SonarQube.

Managing plugins

Make sure you restart Jenkins once the plugin is successfully installed.

Installing plugins

Once the restart is completed, you have to set up Sonar Scanner, which is a standalone tool used for scanning the source code and send the result to SonarQube Server. Here you are going to install it in the Jenkins container itself, although you are free to launch a separate container for the same purpose.

Shell
 




xxxxxxxxxx
1


 
1
$ docker exec -it <your-jinkins-container-id> bash $ cd /var/jenkins_home $ wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.2.0.1873-linux.zip $ unzip sonar-scanner-cli-4.2.0.1873-linux.zip 



Go back to the Manage Jenkins page and select Global Tool Configuration.

In this page, find SonarQube Scanner and add complete the installation:

Installing SonarQube scanner

Don't forget to uncheck Install automatically as you have to define the installation path of Sonar Scanner explicitly.

As Jenkins and SonarQube are running in separate docker containers, we need to create a Webhook at SonarQube Server so that both can communicate with each other. To do so, follow Dashboard > Administrator > Configuration > Webhook:

Creating Webhook

Lastly, you have to add the access token you generated on your SonarQube server in Jenkins. Follow the URL http://your-ip-addr-here:8080/credentials/store/system/ or go to Dashboard > Credentials > System, as shown below:

Adding credentials

Click on Add Credentials link and select "Secret Text" field under Kind:

Adding credentials

In the Secret field, paste the generated token from SonarQube that you previously created.

Now, add the SonarQube server to your Jenkins environment. Go to Dashboard > Manage Jenkins > Configure System. Find SonarQube server on the page and add the required details as mentioned below:

SonarQube servers

Now, you have to add Gitlab in Jenkins, visit Dashboard > Credentials > System. Here, you are going to add the access token you previously created to your Jenkins server. Although, you can also use Kind with username and password.

Adding Gitlab credentials

As you are through with the SonarQube and GitLab integration in Jenkins, it's time to manage other dependencies. In this guide, we are going to use a simple NodeJs application; hence it's time to install NodeJS on your Jenkins Server.

NodeJS Environment in Jenkins:

To install NodeJS plugin, go to Dashboard > Manage Jenkins > Manage Plugins > Available and search and select NodeJS. Click on the Install without restart button, as shown in the figure below.

Node.js configuration

Upon successful installation of NodeJS plugin in Jenkins, make sure that you restart Jenkins.

To configure NodeJS plugin, go to Dashboard > Manage Jenkins > Global Tool Configuration and find "NodeJS". Click on NodeJS installation button and add the necessary details, as shown in the figure:

Node configuration

Give a name and select the NodeJS version as per your requirement; you can also install Global NPM packages and set the refresh rate depending upon the project requirements.

Once done, you need to save the configuration. As we are through with the gruesome part of installing and configuring the environment, it's time to create the project pipeline.

Creating Jenkins Pipeline

Here you are going to create a pipeline using Declarative Scripted Pipeline. Follow the below steps to get started:

Step 1 - Create a New Job
Go to Jenkins' Dashboard and click on the "New Item" link. Now, enter the item name and select Pipeline option as shown in the figure:

Creating Jenkins pipeline

Step 2 - Pipeline
The below-mentioned simple pipeline script helps you to pull the code from GitLab, quality check via SonarQube, and use NodeJS.

JSON
 




xxxxxxxxxx
1
35


 
1
pipeline {
2
agent any
3
tools {nodejs "nodenv"}
4
stages {
5
 stage("Code Checkout from GitLab") {
6
  steps {
7
   git branch: 'master',
8
    credentialsId: 'gitlab_access_token',
9
    url: 'http://your-ip-here:10080/root/test-project.git'
10
  }
11
 }
12
   stage('Code Quality Check via SonarQube') {
13
   steps {
14
       script {
15
       def scannerHome = tool 'sonarqube';
16
           withSonarQubeEnv("sonarqube-container") {
17
           sh "${tool("sonarqube")}/bin/sonar-scanner \
18
           -Dsonar.projectKey=test-node-js \
19
           -Dsonar.sources=. \
20
           -Dsonar.css.node=. \
21
           -Dsonar.host.url=http://your-ip-here:9000 \
22
           -Dsonar.login=your-generated-token-from-sonarqube-container"
23
               }
24
           }
25
       }
26
   }
27
   stage("Install Project Dependencies") {
28
   steps {
29
       nodejs(nodeJSInstallationName: 'nodenv'){
30
           sh "npm install"
31
           }
32
       }
33
   }
34
}
35
}



Make sure that you change the values of GitLab repo URL, SonarQube host URL, and its access token.

Add the above-mentioned pipeline script and save it, as shown in the below figure:

Saving pipeline script

Step 3 - Run Pipeline
Now, as you have saved the pipeline script, it's time to build your application in Jenkins. Go to Dashboard > YOUR PROJECT > Build Now.

Building pipeline

As soon as you click the Build Now link, Jenkins will start building project as per pipeline script. In Build History, you will see the progress bar for the current build along with the Stage View:

Building pipeline

If your build runs successfully, you will be able to see the time taken by each stage, in Stage View:

Node application pipeline

Also, you can visit the SonarQube dashboard to see the project code report, by visiting the link named, "SonarQube" on the project pipeline page.

Pipeline stage

That's it! You have successfully created a Jenkins Pipeline while using SonarQube and GitLab. Now, every time you push the code to the repo, you can build the project, which will show the code quality.

Jenkins (software) GitLab Pipeline (software) Node.js Docker (software) Continuous Integration/Deployment

Published at DZone with permission of Sudip Sengupta. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Implementing CI/CD Pipelines With Jenkins and Docker
  • Java CI/CD: From Local Build to Jenkins Continuous Integration
  • Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags
  • Pipeline as a Service: How To Test Pipelines in GitLab

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!