Friday, September 2, 2016

Configure SonarQube with Jenkins

   Jenkins provides easy integration with different kinds of plugins which are helpful in overall improvement and management of development life cycle and code quality. One of such plugin is SonarQube.

    SonarQube is code quality analysis software. It runs through the code and identifies code quality issues. It has got web portal where you can generate different kinds of reports like

  1. Technical Debt
  2. Code Issue, with actual code linkage.
  3. Different types of reports.
  4. Maintains the history of issue details. Can check increase and decrease.
  5. Rules configuration.
  6. Quality Gates
  7. Code duplication
  8. and Many others.
    There is provision in Jenkins to integrate this plugin, which can be executed during the building of applications. Also there is option of failing the build process if the code quality does not match the defined quality gate in Sonar.

  Now i will show how to integrate this plugin with Jenkins. Assumption is that Jenkins is installed in your system. 

  To start first you need to download sonar server in your system or if it already installed and running then you can skip this step.

If Sonar is not installed then follow these steps.
  1. Download SonarQube from the link.
  2. After download, unzip the file in your system.
  3. Then to start SonarQube server, navigate to "SONAR_HOME/bin".
  4. Based on your environment, go to specific folder.
  5. I am using "windows-x86-64" as i am having Windows, 64Bit system. 
  6. Click on "StartSonar.bat" to start the sonar server.
  7. Sonar server should start, and you will see new command prompt window opening for you.
  8. Sonar can be added into window service as well if you are system admin. There batch files present in same folder.
  9. Access the SonarQube in WebBrowser by entering url: "http://localhost:9000/"
  10. Empty dashboard page of sonar server if it is fresh installation.
  11. Once page is launched, it means that sonar is installed and running fine.
  12. Please note this server url will be used in Jenkins when we install SonarQube plugin in coming sections below.
following message in the console tell if sonar server is up and running.
jvm 1    | 2016.08.30 23:02:43 INFO  app[o.s.p.m.Monitor] Process[web] is up


Now coming back to configuring SonarQube in Jenkins.

1. Access jenkins portal by accessing "http;//localhost:8080" url in browser.

2. Go to --> Manage Jenkins menu in left navigation. Then click on "Manage Plugins",


3. Navigate to "Available" Tab, in the "Filter" option, type "SonarQube", you will see "SonarQube Plugin" listed below, select the checkbox and click on "Download now and install after restart" button.


4. Following screen will be shown, restart Jenkins, plugin will be installed and it can be verified in installed Tab under Manage Plugin page.


5. After installation next step will configure SonarQube Plugin.

6. Navigate to "Manage Jenkins" --> "Configure System", look for section named "SonarQube", as shown below.

7. Provide basic  details, like Name, and Server Url, This URL should be the url for SonarQube Server which we installed above.



8. Once the global configuration is complete, next step will be to configure SonarQube for a specific projects. I will show next.

9. Navigate to Jenkins Home, by clicking on Jenkins menu in horizontal menu bar.

10. Next click on "New Item", you will get following screen, provide details of the project. Here we are creating new project which will be mapped to a java project.

  • Item Name
  • Select Freestyle Project
Click on OK button.




11. It will launch detailed project screen, where project related information can be configured, like SCM, and other configuration. These points is out of scope of this blog.

12. Now to add Sonar configuration to the project, click on "Add Build Step" and click on "Invoke Standalone SonarQube Analysis" menu. it will expand a new section specific to SonarQube configuration.



13. Provide the details for project configuration related to sonar server.

Values inside "Analysis properties" are important for sonar server.

  • sonar.projectKey - This will be used to uniquely identify project in sonar server, ensure that this does not repeat if you have multiple project in Sonar Server. Otherwise it will override the project data.
  • sonar.projectName - Project will be given in sonar server.
  • sonar.projectVersion - Usefull in maintaining history of the changes.
  • sonar.sources - folder where source code files are present.
  • sonar.language- language of the source code.




14. Once values are entered, then click on "Save" button.
15.  Now setup is complete for the project.
16. Next is to start Build process and verify if sonarqube analysis is getting triggered or not. Click on "Build Now" in project left navigation.



17. Once build starts, then monitor the logs in Jenkins console. You will see following logs. this mean SonarQube analysis is getting triggered and it is getting tracked in sonar server also.



18. To check project in SonarQube server, navigate with following link given in above console. you will see below screen.



That is all which needs to be done for configuring sonarqube in jenkins.

No comments:

Post a Comment

Components of Big Data - Hadoop System

In this blog i will explain important components which are part of Hadoop System. I will give very brief overview of these components. Be...