Monday, December 24, 2018

Jenkins & SonarQube

In this post we will use Jenkins to run code analysis on SonarQube. Below are steps needed:
  • Download Jenkins from its official website. I downloaded Jenkins war version from the following options and deploy it to Apache Tomcat webserver.


  • Run Tomcat and follow installation instruction. You will be prompted options to install commonly used Jenkins plugins. Choose to install them. You will also need to setup administrator login.
  • Next we need to install some more plugins. To do this click Manage Jenkins and then Manage Plugins.

  • The Plugin Manager screen will be displayed. Make sure the following plugins have been installed: Git, SonarQube, Email



  • Next we will make configurations of the installed plugins. To do that click Manage Jenkins and then Configure System.

  • The Configuration screen will be displayed. First we need to configure SonarQube as shown below. Enter value for Name, this can be any name to identify this SonarQube configuration. Enter our SonarQube URL in Server URL field. And lastly, enter Server Authentication Token. This token is available in our SonarQube user profile as shown below. We can also generate a new one if it doesn't exist.


  • Next we will configure email as shown below. Here we configure outgoing SMTP server for sending email.

  • Next step is to configure tools. Here we will configure JDK and SonarQube Scanner. To do that click Manage Jenkins menu and then Global Tool Configuration as shown below.

  • First we will configure JDK as shown below. Here we tell Jenkins where to find our JDK home.

  • Next we need to configure SonarQube Scanner as shown below. The SonarQube Scanner is needed to do code analysis. In SonarQube there are many type of scanner, for example scanner for maven which we have tried in this post. Here we use common scanner that will work for various type of project, not only maven.

  • Now that  we are all set, we will create a Jenkins project that will do the following tasks: pull source code from git, compile and run the code, ask SonarQube to analyse the code, send email if anything fails. To do that click New Item menu and choose Freestyle Project as shown below.

  • The project configuration screen will open. Here we need to do, again, several configurations. First we need to specify our git project information as shown below. Note that the project only contains one java class that prints Hello Jenkins on console as shown below. Jenkins will pull our source code from git before building them.


  • Next we will tell Jenkins to do two tasks, to execute some java commands, and to ask SonarQube to analyse the code. First, we will execute java command javac to compile a class, and command java to run the java code as shown below.
  • Second task is to execute Sonar Scanner as shown below. Here we put some properties. sonar.projectKey is used to identify this project on SonarQube. sonar.sources is folders where our source code are available. sonar.projectBaseDir is base folder where SonarQube should look for our code to analyse.
  • Last step is to send email when execution fails as shown below.
  • Now we can build the project. Click Build Now and the build will be available on Build History. Clicking on a single build history will bring us to build menu. Here we can click on Console Output to see the execution console.


  • Below is the output console. Here we can see that Jenkins runs our javac and java command. It also asks SonarQube to run an analysis. Email will only be sent if there is a build error.

  • In SonarQube we can also see that the project has been analysed as shown below.

0 comments:

 

©2009 Stay the Same | by TNB