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.

2 COMMENTS

  1. Hi I found this tutorial very helpful, but I’ve one doubt that where to add the “sonar-project.properties” file, u mentioned it in root repository that means in sonar-scanner installed repository or in the jenkins path i.e. “/var/lib/jenkins/workspace/$JOB_NAME/”, can you please make me this clear as I’m getting error while executing the pipeline.

  2. Hi, thanks for your helpful video, I am stuck here to start the elasticsearch service. I am getting this error “systemctl restart wazuh-manager.service
    Job for wazuh-manager.service failed because the control process exited with error code. See “systemctl status wazuh-manager.service” and “journalctl -xe” for details.

    Please help me.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,521FansLike
364FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to setup FTP Server on CentOS

You are allowed to use any one of the FTP servers as you like. However, in this tutorial, we will be installing and using vsftpd, short for Very Secure FTP Daemon. It is a fast, stable, and secure FTP server that will help you transfer files to and from a remote system in a jiffy.

CentOS Vs. Ubuntu Server : Everything You Need to Know

Choosing the perfect Linux distribution to set up your server can be confusing since Linux provides a limitless number of options. The main reason behind these many distributions is because Linux is an opensource platform. Anybody with the required skills can contribute to the development or build and release their distribution. Currently, there are more than 600 Linux Desktop and Server distributions in the market.

Top 10 Reasons to use Xfce as your Desktop Environment

There are many choices for desktop environments for Linux based operating systems. Mainly, you can install any DE of your choice on most of the Linux based distributions, even if they are not offered as a package officially. In our recent articles, we discussed the best of KDE and Cinnamon. In this article, we wish to present to you the top reasons why you should consider Xfce as your desktop environment.

The 10 Best Linux Network Monitoring Tools

Having total control over your network is essential to prevent programs from overusing your network resources and slowing down the overall performance. This is why you should install a network monitoring tool on your system, giving you a visual overview of everything that's happening on your network. Networking Tools are like swiss-knife for the system administrators for troubleshooting system issues.

How to install CouchPotato on Ubuntu

Downloading movies and copying them over to your home server can get frustrating, especially if you are doing it daily! What if you have an option to download videos automatically, quickly, and above all with excellent quality. CouchPotato allows you to download movies easily once they are available and released automatically.

Top 20 Rsync Examples in Linux

The Rsync (remote sync) command is a Linux/Unix utility used to synchronize and copy files and directories either locally or remotely. Most Linux geeks use Rsync to mirror, backup or migrate data across folders, across disks and networks. One notable feature with the Rsync command is that it uses the "delta transfer algorithm."

MUST READ

Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

How to create a Live Linux USB drive using Etcher

In the Linux community, one thing is common between almost all of the users: trying out different Linux distributions. Most of the Linux based distributions have a great way of being tested as they provide ISO images for creating a Live USB.

How to install Android Studio on Ubuntu

Android Studio, like the name suggests, is a full-featured cross-platform IDE that enables you to develop Android applications to run on any android device. Based on JetBrains' IntelliJ IDEA, Android Studio comes installed with all the necessary features needed for Android development.

6 cool and fun Linux commands you don’t want to miss

The real purpose of this article is to help Linux newbies get comfortable and confident with the Linux command-line.  While knowledge and comfort of the Linux GUI allow great power, it is the mastery of the command line, or CLI, affords the Linux user unlimited power and certifies them as a Linux power user well on their way to becoming an expert.

6 ways to find out your Linux file system type

Any Operating system in the market whether its Windows, Linux, Unix, macOS, and any other, must be able to access and manage files and data on storage devices.