Configure SonarQube Scanner with Jenkins
In my previous blogs, I have covered about Jenkins and SonarQube where I have explained their features, installation, and configuration. Now in this blog, I will cover how you can configure SonarQube scanner in Jenkins so that before deploying the code you can scan it through SonarQube and stop the deployment process if the test fails from SonarQube.
This way we can also ensure the code quality before deploying it to the server. So let's start the process of integrating the SonarQube scanner in Jenkins.
Before starting this process please ensure that you have installed SonarQube and Jenkins on your machine. In case you have not installed them please refer to my previous blogs on SonarQube and Jenkins:
- Why SonarQube is important for IT projects ?
- SonarQube installation on Ubuntu
- Analyze your project with SonarQube
- Install Jenkins on Ubuntu
- Configure Jenkins for Automated Code Deployment
Jenkins Setup
Use the following steps for configuring SonarQube with Jenkins:
- Open Jenkins on your browser and login using the credentials.
- Click on 'Manage Jenkins' on the left menu. This will open the Manage Jenkins page with different options.
- Click on the 'Manage Plugins' option from the list.
- Now click on the 'Available' tab to search for the plugin.
- Type 'SonarQube' in the filter text box and hit enter.
- From the list click on the install button of 'SonarQube Scanner for Jenkins' plugin, please refer to the below screenshot:
- This will install the SonarQube scanner plugin.
- Now go to the project page and click on the 'Configure' link from the left menu.
- Scroll down to the 'Execute SonarQube Scanner' section under build.
- You can enter the optional fields like the task to run, JDK, and path to project properties.
- Under analysis properties option add following code and change it as per your project configuration:
sonar.projectKey=project12345
sonar.projectName=project_test
sonar.projectVersion=1.0
sonar.login=admin
sonar.password=admin
sonar.exclusions=vendor/**, storage/**, resources/**
sonar.language=php
sonar.sources=/var/lib/jenkins/workspace/$JOB_NAME
sonar.sourceEncoding=UTF-8
If your SonarQube server is located on a different cluster then provide the SonarQube server location in the Jenkins configuration:
sonar.host.url=http://192.168.12.24:9000/sonar
Your SonarQube scanner integration has been done now to build your project to check the SonarQube scan result under console output. The following screen shows the SonarQube result from Jenkins:
I hope this blog can help you to set up the SonarQube scanner with Jenkins. In case of any doubt please leave a comment.
Comments (9)
Daniel Izquierdo
Aug 28, 2018, 8:12:22 AM
If I have a Sonarqub?e installation on a cluster how do I tell jenkins where my Sonarqube installation is?
Anurag Srivastava
Sep 1, 2018, 1:24:44 PM
Hi Daniel for that you need to provide the SonarQube cluster URL during Jenkins configuration.
Anurag Srivastava
Sep 1, 2018, 1:29:15 PM
The setting which you need in the property file is:sonar.host.url=http://192.xxx.xx.xx:9000/sonar
Rajendra N
Sep 9, 2018, 3:02:46 AM
How to setup https for sonarqube?
Prudhvi Vem
Sep 13, 2018, 8:25:39 PM
I have changed the sonar source and I encounter the below error.ERROR: Invalid value of sonar.sources for com.****.it.***:xyz I have used the file that I have in my Bitbucket. Am I missing something here?
jitender yadav
Mar 27, 2019, 5:28:00 AM
Hi Anurag, can you please answer the above queries??
Anurag Srivastava
Mar 29, 2019, 7:45:52 PM
@Prudhvi: Basically we use sonar source to provide comma-separated paths to directories containing main source files. It may not be able to read your Bitbucket repository. You can try by cloning your code on Jenkins or any other server and then referring to the code path.
harsh patil
Apr 16, 2019, 12:04:36 PM
Hi Anurag, im trying to run sonarqube scanner for a project with jenkins integretion, i got stucked with below Error, 16:27:38 SonarQube Runner 2.4 16:27:38 Java 1.8.0_201 Oracle Corporation (64-bit) 16:27:38 Linux 4.15.0-1035-aws amd64 16:27:38 INFO: Runner configuration file: /opt/sonarscanner/conf/sonar-runner.properties 16:27:38 INFO: Project configuration file: NONE 16:27:38 INFO: Default locale: "en", source code encoding: "UTF-8" 16:27:38 INFO: Work directory: /var/lib/jenkins/workspace/Example_of_SonarQube_Scanner_Usage/.sonar 16:28:08 INFO: ------------------------------------------------------------------------ 16:28:08 INFO: EXECUTION FAILURE 16:28:08 INFO: ------------------------------------------------------------------------ 16:28:08 Total time: 30.112s 16:28:08 Final Memory: 1M/59M 16:28:08 INFO: ------------------------------------------------------------------------ 16:28:08 ERROR: Error during Sonar runner execution 16:28:08 ERROR: java.net.SocketTimeoutException: connect timed out 16:28:08 ERROR: Caused by: connect timed out can you pls suggest something! Thank you!
Anurag Srivastava
Apr 16, 2019, 2:16:09 PM
@harsh: seems that the report zip size is big and scanner is unable to upload that. If there is any library directory which we can ignore then please add it under sonar.exclusions option. let me know if it works for you.
Leave a comment
Related Blogs
Introduction to Logstash
Dec 20, 2019, 11:38:31 AM | Anurag Srivastava
Importing MongoDB data into Elasticsearch
Mar 9, 2019, 8:20:38 AM | Anurag Srivastava
Importing MySQL data into Elasticsearch
Feb 9, 2019, 12:06:18 PM | Anurag Srivastava
Snapshot and Restore Elasticsearch Indices
Sep 16, 2019, 5:55:06 AM | Anurag Srivastava
Log analysis with Elastic stack
Jan 31, 2018, 6:11:29 AM | Anurag Srivastava
Creating Elasticsearch Cluster
Apr 6, 2019, 8:41:41 PM | Anurag Srivastava
Top Blogs
Configure SonarQube Scanner with Jenkins
Jun 21, 2018, 4:58:11 AM | Anurag Srivastava
Execute Commands on Remote Machines using sshpass
Jul 16, 2018, 5:00:02 PM | Anurag Srivastava
Importing MongoDB data into Elasticsearch
Mar 9, 2019, 8:20:38 AM | Anurag Srivastava
Importing MySQL data into Elasticsearch
Feb 9, 2019, 12:06:18 PM | Anurag Srivastava
Configure Jenkins for Automated Code Deployment
Jun 13, 2018, 3:44:01 PM | Anurag Srivastava
Deploying Angular code using Python script
Jun 26, 2018, 4:50:18 PM | Anurag Srivastava