Integrating SonarQube with Jenkins
Recently, we got a requirement where Grails Development team needs to have a one-click interface to run units test cases for their Grails application and send the results to SonarQube.
SonarQube is a web-based application which is used for centralized management of code quality. We decided to integrate it with Jenkins to provide a one click solution.
Scenario: Integrate SonarQube with Jenkins to run unit test cases and publish results to SonarQube.
Here is the step-by-step procedure to perform the scenario:
- Setup a Jenkins server if already not using.
- Goto plugin-manager of Jenkins to install “SonarQube Plugin”.
- Goto “System-configuration” of Jenkins to provide “SonarQube” server’s details as below.
- Create a Jenkins job and choose one source code management option (say git).
- Under build, add “Execute Shell” as build step and write commands to run unit tests. For example:
grails clean-all –non-interactive –plain-output;
grails -Dgrails.env=test test-app:unit –non-interactive –plain-output
grails test-app -coverage -xml[/js]
- Add “Invoke Standalone SonarQube Analysis” as another build step and add below lines to “Analysis properties” block:
[js]sonar.projectKey=App Name- Any Identifier
sonar.projectDescription=Static analysis for the AppName
- That’s all.
Now, run the Grails application job and results could be seen on SonarQube server after job completion.