Install, configure SonarQube by script + integration and create Webhook SonarQube Job on Jenkins

Tram Ho

Hi everyone, today I will share a little experience of installing SonarQube on CentOS 7 environment and how to integrate SonarQube on Jenkins to scan source code and create Code Quality Gate.

1. Install SonarQube

I relied on a script from a DEV guy’s gist, but I didn’t save the link for a long time (If you accidentally read this post, if you can comment, let me note the author, thank you ^^) create a script to To install SonarQube, everyone needs to define the version as well as help me install a compatible Java version.

  • Install the Java version, please install the openjdk-devel version, please help me
  • Install PostgreSQL database
  • Remember to change your password information to default xxxxxx

After the installation is complete, check the status of SonarQube to see if it is active with the command: systemctl status sonarqube

Anyone who wants to check or change the access configuration can follow this path: /opt/sonarqube/conf/sonar.properties

2. Integrating SonarQube Deployment on Jenkins

Install SonarQube Scanner Plugin on Jenkins

Go to Manage Jenkins => Configure System

Go to the SonarQube server section and set the necessary parameters

Note the token part, I took 2 steps to help me: First log in to the SonarQube site to create a token

Step 2: Go to Credentials => System => Gloabl section to generate the secret key from the SonarQube site you just got

Once done, create a SonarQube Job in turn to scan the source code

Configure Pipeline, everyone

Information about my pipeline is attached below

Note the line mvn sonar:sonar to help me is the project everyone created on SonarQube, after creating there will be this scan token line, remember to save it for later use.

Creating a new project will follow the steps below, first I create a manual project and select Localy project

The line of tokens I noticed to everyone is the line when creating this project.

3. Create Webhook for SonarQube Job on Jenkins

Ok, here is the process of completing the installation and setting up a job pipeline on Jenkins, but SonarQube has one final setup, which is to create a webhook to fire an event to let Jenkins know that Sonar has finished scanning, please update the status for the Job. success or failure. In the Project Settings section, choose to go to webhooks.

Note that the webhook path on Jenkins will be http://jenkins-host :port/sonarqube-webhook/ , everyone, when accessing the interface, it will report as shown.

Okie now start to turn on the job and wait for the results. If there is no Webhook, the job will run until the timeout stops, everyone.

On Sonar, everyone, go to Project Settings => Background Tasks to check the status of the scan job

Ah one more note for those who scan Java source code, they should add a configuration to ignore source folders that do not want to be scanned to increase speed. Sample template as shown below.

Thank you everyone for following this line, see you in the next post ^^

Link to my article: https://gociter.wordpress.com/2022/09/29/cai-dat-cau-hinh-sonarqube-tren-jenkins-va-tao-webhook-sonarqube-job/

Share the news now

Source : Viblo