Devops -day-14 Jenkins-SonarQube Integration

Ranjith RRanjith R
2 min read

Let’s see how we can quickly set up a SonarQube environment using a Docker container

1. Running SonarQube on Docker

Run Docker container sonarqube

#install docker
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker ubuntu  
newgrp docker
sudo chmod 777 /var/run/docker.sock
#sonarqube
docker run -d --name sonar -p 9000:9000 sonarqube:lts-community

COPY

Now copy the IP address of the ec2 instance

COPY

<ec2-public-ip:9000>

Enter username and password, click on login and change password

COPY

username admin
password admin

Update New password, This is Sonar Dashboard.

2: IntegrationSonarQube

Assume a Scenario: After I committed code to GitHub. I want to ensure my code quality and know bugs, vulnerabilities, code smells, etc. (static code analysis) for my code before I build my code automatically with Jenkins and I want this activity to perform every time I commit code.

In this scenario for Continuous Inspection and Continuous Integration of the code. We will follow the best practices using GitHub-Jenkins-SonarQube Integration for this scenario.

Flow: As soon as the developer commits the code to GitHub, Jenkins will fetch/pull the code from a repository and will perform static code analysis with the help of Sonar Scanner and send the analysis report to SonarQube Server then it will automatically build the project code.

Prerequisite :

  1. Jenkins is setup with GitHub with some build trigger (in my case it's Poll SCM)

  2. SonarQube is running and you have a Sonar Scanner setup in the system.

Step 1. Open SonarQube server- Go to Administration > click on Security > Users > Click on Tokens > Generate token with some name > Copy the token, it will be used in Jenkins for Sonar authentication.

Configure Sonar Scanner in Jenkins: Go to Manage Jenkins > credentials > system > Global credentials > Add

Kind use the > secret text,> token copied from SonarQube Server> Apply and Save

Step 2. Setup SonarQube with Jenkins- Go to Manage Jenkins > Configure system > SonarQube server section > Add SonarQube > Name it, provide Server Url as http://<IP>:<port> > Apply and Save

Go to sonarqube server > administrator >configuration click to Webhook

Create Webhook

Add Name: Jenkins > URL : Jenkins server URL copy to past

url add sonarqube-webhook

http://192.168.58.138:8080/sonarqube-webhook/

create webhook

Run on your Jenkins pipeline

Your Jenkins pipeline sonarqube execute successfully

Sonarqube project Dashboard your code scanning report

0
Subscribe to my newsletter

Read articles from Ranjith R directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ranjith R
Ranjith R