jenkins + sonar + github integration
Asked Answered
B

3

24

Problem: I am setting up jenkins + sonar + github integration for automatic pullrequest static code check through sonar.

My Configuration:

  1. Installed Sonar with github

  2. Installed jenkins

  3. In jenkins post-build action I have the following properties

-Dsonar.github.login=bhuwang   
-Dsonar.github.repository=company/repo  
-Dsonar.verbose=true 
-Dsonar.analysis.mode=preview 
-Dsonar.issuesReport.console.enable=true 
-Dsonar.forceUpdate=true 
-Dsonar.github.login=gitusername 
-Dsonar.github.oauth=token
  1. Token was generated from my github account.

In this link I have read that I have to provide the following properties while running sonarqube: enter image description here

I have added all except sonar.github.pullRequest properties. I don't know how do I get this property value dynamically. Seems like above four properties are must to work properly.

Edit: I have found the way to add property -Dsonar.github.pullRequest=pullrequestNo

And the good news is it is working perfectly fine now with hardcoded pull request no. but I need the dynamic way to get the pull request no.

Does anyone know how to get the pull request no. dynamically inside jenkins.?

I have found that pull request builder will work but no luck at my end. I am not able to use pull request builder environment variables inside sonar properties.

https://issues.jenkins-ci.org/browse/JENKINS-24590

Banker answered 17/8, 2015 at 10:1 Comment(4)
Could you describe the process you're going through, what order things are running in? I've set up a process like this in the past and it sounds like you're following a different route. In the past I've had Jenkins poll Github -> Build -> Run tests -> Output to SonarQube for code/test metrics.Ball
@Ball here is my setup flow: 1. git checkout 2. build 3. in post build action it runs sonar 4. in #3 it should call sonar github plugin to write comments.Banker
Ah, so all you want to do is post comments on your git repo automatically from SonarQube, presumably with some code metrics about the code?Ball
Yes I want to post sonar code review comments to pull request directly. Read this link to get more insight on what I want. docs.sonarqube.org/display/PLUG/GitHub+PluginBanker
B
33

Finally I am able to solve this issue. Here is the detail:

  1. Install Sonar with GitHub plugin.
  2. Install Jenkins with the following plugins

    • GitHub Pull Request Builder
    • SonarQube Plugin
    • GIT plugin
    • GitHub plugin

Follow this link to setup pull request builder plugin: https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin#GitHubpullrequestbuilderplugin-EnvironmentVariables

    1. Global Jenkins System Setup:
  • Goto Manage Jenkins -> Configure System
  • Configure jdk enter image description here
  • Install sonarQube Runner enter image description here
  • Configure Sonar enter image description here
  • Git WebHook Setup enter image description here
  • PullRequest Builder Setup enter image description here

    1. Create Free Style Jenkins job enter image description here
  • add github url to GitHub Project section enter image description here

  • Setup Source Code Management Section enter image description here

  • Setup Build Trigger enter image description here

  • Setup Sonar for post build actions enter image description here

  • In the additional properties section provide the following sonar properties

-Dsonar.sourceEncoding=UTF-8 
-Dsonar.analysis.mode=preview 
-Dsonar.github.repository=company/repo
-Dsonar.github.login=gitusername 
-Dsonar.github.oauth=oauthtoken
-Dsonar.host.url=sonarhostedurl
-Dsonar.login=admin 
-Dsonar.password=pass
-Dsonar.github.pullRequest=${ghprbPullId}
${ghprbPullId}: this will be provided via github pullrequest builder plugin

Note: The job should be triggered through pullrequest builder plugin otherwise ${ghprbPullId} will return blank. If you run the job manually this will not work for that you have to pass this ${ghprbPullId} property as a build parameter. If you want to check the environment parameter available follow this Git environment variables for use in executing Jenkins shell scripts

I hope this helps.

Banker answered 19/8, 2015 at 12:19 Comment(1)
Hi @Bhuwan Gautam, thanks for helping, I managed to setup like you did. One thing I'm wondering tho; if the project doesn't pass the quality gates Jenkins doesn't care and sets the pull request status on green anyways. I thought have to fail in that case...?Wherewithal
B
4

Too long for a comment so I'll have to write it here.

After some research on the web I've found a question on SO from someone a few steps ahead of you in the process of trying to setup a similar system, here. They're trying to get this all working with Maven, I'm not sure if you are also, but either way you can then see the link they've been using to help them with their script:

https://github.com/SonarSource/sonarqube/blob/master/travis.sh

This shows they need to write a script that will retrieve the pull number dynamically for them. I think you may need to follow a similar approach and write a script that will retrieve the number for you, I believe one of the github conf files keeps track of the current pull request number for its own tracking purposes.

Not really as straight forward an answer as you might have been hoping for, but hopefully this is new information that helps you get there.

Ball answered 18/8, 2015 at 13:36 Comment(1)
Thanks for your effort. I have found this link: issues.jenkins-ci.org/browse/JENKINS-24590 where it mentions that jenkins pull request builder plugin will work for this. I will give a try and let you know the detail.Banker
Z
0

See my project's implementation Jenkins + SONAR + Github with code at https://github.com/lqtruong/ci

Zoophyte answered 8/4, 2021 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.