How to configure SonarQube Scanner plugin for Jenkins to preview results in GitHub PullRequest

Hey guys,

I’m using SonarQube Scanner as a plugin in Jenkins and I’m trying to configure it on a Jenkins Job which is triggered on a pull request in GitHub.

I normally use SonarQube Scanner on my main Jenkins Job the following way and everything works:

  1. SonarQube Scanner Begin Analysis (with setup Project Key, Name and Version)
  2. MSBuild
  3. SonarQube Scanner End Analysis

I’m trying to put it on the pull request job in preview mode so whenever a pull request is made the Jenkins job is triggered and the SonnarQube Scanner makes his analysis without pushing it to the real project in Sonnar and post the errors as a comment on GitHub.

I have an example of this thing working with Jenkins Pipeline and from there I saw that there are a couple of arguments that need to be set up in order for the Scanner to work this way and I figure it out that these arguments can be passed in the Additional arguments field as follows:

/d:sonar.analysis.mode=preview
/d:sonar.github.pullRequest=[PullRequest Id]
/d:sonar.github.repository=[GitHub Repository Url]
/d.sonar.github.oauth=[some Sonnar Key for GitHub i guess]

Unfortunately, I can’t understand where should I get this OATH key and is this the right way to do this kind of integration or there is some other way to do this that is more appropriate?

Thanks in advance for any help!

Kind Regards,
Borislav

Hi Borislav,

The value of the sonar.github.oauth parameter is a personal access token generated on GitHub like this: https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/

For more information about the GitHub plugin and its parameters, please read the following article: https://docs.sonarqube.org/display/PLUG/GitHub+Plugin

Kind regards,
Valeri

Hey Valeri,

I created the token and put it in Jenkins global Credentials. I successfully retrieve the value in the job and pass it as an additional argument to SonnarQube Scanner but the job is still failing with the following error

ERROR: Error during SonarQube Scanner execution
ERROR: Failed to execute project builder: org.sonar.plugins.github.PullRequestProjectBuilder
ERROR: Caused by: Unable to perform GitHub WS operation: {“message”:“Not Found”,“documentation_url”:“Repositories - GitHub Docs”}
ERROR:

This is how the build step in the job looks like:

I can see in the console output at the top that proper values are loaded and I don’t know why this job keeps failing. Do you have any idea what can be wrong?

[Service PR] $ c:\jenkins\tools\hudson.plugins.sonar.MsBuildSQRunnerInstallation\SonarScanner_MSBuild\MSBuild.SonarQube.Runner.exe begin /k:Service “/n:Service” /v:1.0.0.0 /d:sonar.host.url=https://sonarqube.com /d:sonar.analysis.mode=preview /d:sonar.github.pullRequest=151 /d:sonar.github.repository=organisation/Service /d:sonar.github.oath=****

P.S. I intentionally changed some of the values for privacy reasons.

No idea what’s happening here. Judging by the GitHub response, I would say the value of the sonar.github.repository parameter could be wrong. Or could it be that the user you created the token for, does not have access to that repository?

Hey Valeri,

I managed to fix it. I don’t know what exactly was wrong with the token but I regenerated it and everything worked with the new one. Thank you for the support!