Configuring Jenkins Pipeline with SonarQube and GitLab integration

In this tutorial, we are going to configure the declarative pipeline with sonar and GitLab integration.

Jenkins Pipeline is a suite of Jenkin features. It is one of the several ways of defining some Jenkins steps or combination of jobs using code and automate the process of deploying software.

If you haven’t set up Jenkins yet, head over to our Jenkins installation guide for instructions. The pipeline uses a Domain Specific Language(DSL) with two different syntaxes:

  • Declarative Pipeline
  • Scripted Pipeline

In this tutorial, we are going to configure the declarative pipeline with sonar and GitLab integration.

Configuring Jenkins Pipeline with SonarQube and GitLab integration

1. Prerequisites

Installing Pipeline plugin

If you had selected the option install suggested plugins when you configured Jenkins, it should have automatically installed all the needed plugins. If not, don’t worry, you can install the plugins now.

Launch Jenkins and go to Manage Jenkins -> Manage Plugins -> Available

If you can’t find the Pipeline plugin in the Available section, check the installed tab.

Pipeline Plugin
Pipeline Plugin

Installing Sonar Scanner

Login to Jenkins server first. Here, I am going to download the sonar scanner to the “/opt” folder.

cd /opt

Download using wget.

wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.2.0.1873-linux.zip

if wget command is not available, you can install it using the following commands:

For Ubuntu / Debian:

apt-get install wget -y

For Centos / Redhat:

yum install wget -y

Back to Sonar, it’s time to unzip downloaded Sonar file.

unzip sonar-scanner-cli-4.2.0.1873-linux.zip

Install unzip if you get command not found an error.

For Ubuntu / Debian:

apt-get install unzip -y

for Centos / Redhat:

yum install unzip -y

Rename sonar package.

 mv sonar-scanner-4.2.0.1873-linux sonar-scanner

Rename Sonar Scanner
Rename Sonar Scanner

Go to the sonar directory.

cd sonar-scanner

Get the path.

pwd

Sonar Scanner Path
Sonar Scanner Path

Copy sonar scanner path:

/opt/sonar-scanner

Installing Sonar plugin

From Jenkin’s head to Manage Jenkins -> Manage Plugins -> Available

Search for Sonar and select SonarQube Scanner and click Install without Restart.

Then Jenkins should install the relevant plugin.

SonarQube Scanner Plugin
SonarQube Scanner Plugin

 

SonarQube plugin install
SonarQube plugin install

Select Restart Jenkins when the installation is complete. Jenkins should restart.

Restarting
Restarting

Configuring Sonar plugins

Go to Manage Jenkins -> Global Tool Configuration.

Global Tool Configurations
Global Tool Configurations

Find SonarQube Scanner and click on Add SonarQube Scanner.

Unselect install automatically.

Give any name and paste copied sonar scanner path to SONAR_RUNNER_HOME.

SonarQube Scanner Settings
SonarQube Scanner Settings

Then save configurations.

Configuring Sonarqube server settings

Get SonarQube token from the SonarQube server.

Login to the SonarQube server. Follow our SonarQube server installation guide for install Sonarqube Server.

Go to Administration -> Security -> users.

SonarQube Server

Click on Tokens.

Tokens
Tokens

Give any name and click on Generate Token.

Create Token
Create Token

Copy generated Token.

Generated Token
Generated Token

Now go to Jenkins Server.

Click Credentials -> System -> Global Credentials -> Add Credentials.

Add Global Credentials
Add Global Credentials

Select Secret text. Paste copied SonarQube Token to Secret and give any name to ID and description.

SonarQube Credentials
SonarQube Credentials

Adding SonarQube server to Jenkins

Go to Manage Jenkins -> Configure System.

Find SonarQube servers and click on Add SonarQube.

SonarQube Server Details
SonarQube Server Details

Select Enable on injection of SonarQube server, give any name, and add sonarQube server Url.

Select authentication token from the dropdown menu. The token we added previously should list here.

Adding the sonar-project.properties file to the repository root

Here is our file:

# Required metadata
sonar.projectKey=fosslinux-nodejs
sonar.projectName=fosslinux-nodejs

# Comma-separated paths to directories with sources (required)
sonar.sources=./

# Language
sonar.language=js
sonar.profile=node
# Encoding of sources files
sonar.sourceEncoding=UTF-8

Finally, click on save.

Integrating GitLab server with Jenkins

Follow our Gitlab guide for Installing and Configuring GitLab.

Go to Credentials -> System -> Global Credentials -> Add Credentials.

Select a username with a password. Add GitLab login credentials and click on OK.

Gitlab Credentials
Gitlab Credentials

Login to Jenkins server and install git.

For Ubuntu/Debian:

apt-get install git -y

For CentOS/Redhat:

yum install git -y

Here we are going to work with the NodeJS app, so shall install it first.

Installing NodeJS plugin

Go to Manage Jenkins -> plugin manager -> available.

Search for NodeJS.

NodeJS Plugin
NodeJS Plugin

Then select the plugin and install without a restart.

Installation Of Plugin
Installation Of Plugin

Click on Restart Jenkins when the installation is complete, and no jobs are running, and Jenkins should restart automatically.

Configuring NodeJS plugin

Click on Manage Jenkins > Global Tool Configuration -> NodeJS

NodeJS Settings
NodeJS Settings

Give any name. Here we have selected install automatically and NodeJS 10.

Save the configurations.

Creating a declarative pipeline

Go to Jenkins dashboard, click on New Item. Then enter an item name and select the ‘Pipeline’ project. Click OK.

Create Pipeline
Create a Pipeline

Select Pipeline Script

Select Pipeline Script
Select Pipeline Script

Here is the simple pipeline script for git clone, SonarQube quality check, and NodeJS.

pipeline {
         agent any
         tools {nodejs "fosslinuxnode"}

    stages {
        stage("Code Checkout") {
            steps {
                git branch: 'development',
                credentialsId: 'fosslinuxgitlablogin',
                url: 'https://git.fosslinux.com/demo/fosslinux-demo.git'
                  }
              }
         stage('Code Quality') {
                   steps {
                       script {
                          def scannerHome = tool 'fosslinxsonar';
                          withSonarQubeEnv("fosslinxSonarqubeserver") {
                          sh "${tool("fosslinxsonar")}/bin/sonar-scanner"
                                       }
                               }
                           }
                        }

         stage("Install Dependencies") {
                                  steps {
                                        sh "npm install"

                                       }
                                }

         stage("unit Test") {
                            steps {
                                sh "npm test"

                              }
                        }
             }
     }

Add the above pipeline script and save it.

Pipeline Script
Pipeline Script

Pipeline Concepts

a) Pipeline: This is a user-defined block that contains all the processes, such as build, deploy, etc.

b) Agent: The agent section specifies where the entire Pipeline, or a specific stage, will execute in the Jenkins environment depending on where the agent section is placed.

c) Any: This option runs the pipeline/stage on any available agent.

d) Stage: A stage block contains a series of steps in a pipeline. That is, the clone, build, deploy, etc. process a stage.

e) Steps: A series of steps can be added into a stage block. Simply this is a single task that executes a specific process. Now click on build.

Build the Job
Build the Job

The pipeline should start to run.

Pipeline Running
Pipeline Running

Here is a completed pipeline:

Completed Pipeline
Completed Pipeline

That’s all about installing and configuring Jenkins Pipeline.

Darshana
Hey! I'm Darshana, a Linux / DevOps Engineer and also a contributor to FOSS Linux. I enjoy working on various kind of Linux distributions and cloud technologies. During my free time, I love to swim and hike across nature trails. Linux is my love and I'm here to share all my learnings with all of you! Hope you enjoyed reading my article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

24,108FansLike
213FollowersFollow
7SubscribersSubscribe

LATEST ARTICLES

apt_vs_apt-get
Most Linux users, both veterans, and newbies, often get confused about what the difference between the Linux commands apt, and apt-get are and when they should use one or the other.
essential linux commands
Last week, we shared with you several "cool and fun" commands to get comfortable and confident with the Linux command-line. In our quest to further aid Linux users with mastery of the command line, or CLI, we present you with a variety of command-line utilities essential for all Linux users, regardless of proficiency level.
switch to linux
Do you love Windows or Linux? Being on a Linux website, it's not hard to take a guess, but what are the strong reasons to switch to Linux? Linux is a 100% free OS. Is that reason alone enough? Let's find out! Never have there been more compelling reasons to make the switch. Here are just five great reasons for users to create that magical move from the wayward world of Windows to the lauded land of Linux.