Project Guide On Jenkins Integration with Sonar Server: A Complete Guide

Enhancing Code Quality with Jenkins Integration and Sonar Server πŸš€

In today's fast-paced software development world, ensuring high code quality is crucial. 🌟 One powerful way to achieve this is by integrating Jenkins with Sonar Server. Let's dive into how this dynamic duo can supercharge your development process! πŸ’»

Why Sonar Server?

Sonar Server (or SonarQube ) is an open-source platform for continuous inspection of code quality. It performs automatic reviews with static analysis of code to detect bugs, code smells, and security vulnerabilities.

What is Jenkins?

Jenkins is an open-source automation server that helps automate the building, testing, and deployment of applications. It's incredibly flexible and extensible, making it a favorite among developers for continuous integration and delivery (CI/CD).

Benefits of Integration

βš™οΈ Automated Code Quality Checks: Jenkins can trigger SonarQube scans automatically whenever code is pushed or merged, ensuring every change undergoes rigorous quality checks.

πŸ“ˆ Centralized Reporting: SonarQube provides detailed reports on code quality metrics, such as code coverage, duplications, and complexity. Integration with Jenkins makes these reports easily accessible to the entire team.

πŸ”’ Early Detection of Issues: By integrating SonarQube into your Jenkins pipeline, potential issues like bugs or security vulnerabilities are caught early in the development cycle, reducing the cost and effort of fixing them later.


Setting Up Jenkins and Sonar Server

On AWS EC2 Machine Deploy Jenkins Server

Prerequisite (Step 1 )

  • Jenkins installation on AWS EC2

  • Create an EC2 instance with ubuntu Linux AMI

  • Connect to your EC2 instance

  • Update all packages by following command :

  •         $sudo apt update -y
    
  • Install java by following command :

  •       sudo apt install openjdk-11-jdk -y
    

    Step 2

    Jenkins installation on AWS EC2 Using apt

    Add Jenkins to your apt Repository using following command

  •     sudo wget -q -O /usr/share/keyrings/jenkins-keyring.asc https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
        echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
        sudo apt-get update
        sudo apt-get install jenkins
    

    Start and Enable Jenkins Service

$sudo systemctl start Jenkins
$sudo systemctl enable jenkins
$sudo systemctl status jenkins

Get the initial administration password

$sudo cat /var/lib/jenkins/secrets/initialAdminPassword

step 3

Open your EC2 instance public IP (https://<public_IP>:8080/) along with port 8080 in your favorite browser. And provide the administration password obtained during the installation.

Note: Make sure you enable 8080 port in Security Group Inbound Rules.

Provided password which we have copied to unlock Jenkins.

Select "Install Suggested Plugins" Card (It will install those plugins )

Here is your Jenkins server all set

On AWS EC2 Machine Deploy SonarQube Server

Environment SetupπŸ–₯️

Pre-requites:

Java is installed on your Machine

if SonarQube 7.6 ----> Java 1.8 version is installed

if SonarQube 7.6 -----> java 11 version is Installed

Note : We can Check this Compatability in official SonarQube Website

Hardware Requirements

Minimum RAM : 2 GB

βš™οΈStep for deploy SonarQube

Create EC2 Instance with 4 GB Ram (t2.medium )

Connect with EC2 instance using Git-Bash

Switch to Root User

sudo -i

Update packages

sudo apt update -y

Install wget package using following command

sudo apt install wget -y

change directory

cd /opt
sudo apt install openjdk-11-jdk -y
java -versionInstall java openjdk

unzip package install

sudo apt install unzip -y

install SonarQube zip file change version according to your version

wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.4.0.35506.zip

unzip file

unzip sonarqube-8.4.0.35506.zip

Note : SonarQube Server Will Not Run With Root User

we have to create new user called Sonar

create new user

useradd sonar
visudo

File be look like below image we have to configure user n passwd

sonar ALL=(ALL) NOPASSWD: ALL

change Ownership for Sonar Folder

chown -R sonar:sonar /opt/sonarqube-8.4.0.35506

Change file permission

chmod -R 775 /opt/sonarqube-8.4.0.35506

Now switch to Sonar user

su - sonar

Go to SonarQube folder

cd sonarqube-8.4.0.35506
cd bin
cd linux-x86-64
./sonar.sh start

Congratulations to yourself!

Access your SonarQube :

URL : http : // EC2-VM-Public-IP :9000/

Note : SonarQube runs on 9000 port by Default . Enable this port in security group as custom TCP


Now Sonar Server With Jenkins Integration

Pre - Requisites

  1. SonarQube Server (already we done that using above steps )

  2. Jenkins Server (already we done that using above steps )

  3. On SonarQube Server generate a Token

Steps configure Token

Go to Sonar --->Login ---> Click On profile ----> My Account ---> Security --------------->Generate Token

  1. On Jenkins Server
  • Install Apache maven

  • Install Sonar Plugins

  • Configure SonarQube Credentials

  • Install Sonar Scanner

  • Run Jenkins Pipeline Job

Execute Below Commands In Jenkins Server VM CLI

sudo su
cd /opt
wget https://dlcdn.apache.org/maven/maven-3/3.8.8/binaries/apache-maven-3.8.8-bin.tar.gz
tar -xvf apache-maven-3.8.8-bin.tar.gz

Now go to Jenkins Dashboard

Install Scanner Plugin

  • Click on Manage Jenkins

  • Click on Plugins then go to Available

  • Click On SonarQube Scanner Plugin

  • Install It

Configure SonarQube Server

  • Click on Manage Jenkins

  • click on configure System

  • Go to SonarQube Server

  • Add SonarQube Server

Name: Sonar -server -7.8

Server URL : (give Your Sonar Server URL )

Add Sonar Server Token ( Token We Should add As Secrete Text )

Save it

Configure SonarQube Server Scanner

  • Click on Manage Jenkins

  • Click on Global Tools Configuration

  • Click on SonarQube Scanner

Name : Sonar -Scanner-4.7

Select Sonar Version (Sonar -Scanner-4.7)

save it

Create Jenkins Pipeline

pipeline {
    agent any

    environment {
        PATH = "$PATH:/opt/apache-maven-3.8.8/bin" // Corrected Maven path
    }

    stages {
        stage("GetCode") {
            steps {
                git "https://github.com/divyasatpute/java-maven-app.git"
            }
        }

        stage("Build") {
            steps {
                sh "mvn clean package"
            }
        }

        stage("SonarQube Analysis") {
            steps {
                withSonarQubeEnv('sonarqube-8.4') {
                    sh "mvn sonar:sonar"
                }
            }
        }
    }
}

Run Job

  • click on New Item

  • Give Name---->> Select Pipeline ---->Click on OK

  • Paste Pipeline

  • Apply and Save

Here are my some test Results

Hurray you completed this project

Hurray! Celebrating Successful Jenkins and SonarQube Integration! πŸš€

Congratulations on setting up Jenkins and SonarQube integration smoothly! πŸŽ‰ This achievement highlights your dedication to improving code quality and streamlining development processes. With Jenkins orchestrating builds and SonarQube providing insightful code analysis, your team is empowered to deliver high-quality software efficiently.

Keep up the fantastic work! 🌟 Here's to more milestones and continuous improvement in your software development journey. Cheers to innovation and excellence! πŸ₯‚

NOTE: if you face some issue comment below and connect with me on Linkdin @https://www.linkedin.com/in/divya-satpute-68666a300/

0
Subscribe to my newsletter

Read articles from Divya vasant satpute directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Divya vasant satpute
Divya vasant satpute

, I'm a seasoned DevOps engineer πŸ› οΈ with a knack for optimizing software development lifecycles and infrastructure operations. πŸ’‘ Specializing in cutting-edge DevOps practices and proficient in tools like Docker, Kubernetes, Ansible, and more, I'm committed to driving digital transformation and empowering teams to deliver high-quality software with speed and confidence. πŸ’»