24

I'm trying to analyse my source code with Sonar using Jenkins pipelines. To ask Sonar to notify Github with the results I need to specify the Pull Request ID.

How can I get this Pull Request ID from Jenkins Pipelines?

We are using GitHub Organization Folder Plugin to build pull requests, not GitHub pull request builder plugin. That's why $ghprbPullId is not working for me. Any ideas how to get the pull request id in a different way?

3 Answers3

24

Jenkins exposes a global variable named CHANGE_ID:

For a multibranch project corresponding to some kind of change request, this will be set to the change ID, such as a pull request number.

This variable is only populated for pull request builds, so you have to disable branch builds and enable PR builds in your pipeline's configuration for branch sources:

enter image description here

My pipeline step then looks like this:

def PULL_REQUEST = env.CHANGE_ID

stage('Analysis') {
        withCredentials([[$class: 'StringBinding', credentialsId: '***', variable: 'GITHUB_ACCESS_TOKEN']]) {
            withSonarQubeEnv('Sonar') {
                withMaven(maven: 'M3') {
                    sh "mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar " +
                            "-Dsonar.analysis.mode=preview " +
                            "-Dsonar.github.pullRequest=${PULL_REQUEST} " +
                            "-Dsonar.github.oauth=${GITHUB_ACCESS_TOKEN}"
                }
            }
        }
    }
Thomas
  • 2,231
  • 1
  • 19
  • 27
  • Thanks, that helped. DO you know what the difference between "merged with base branch" and "unmerged head" is? – Aliaksandr Kavalevich Jan 20 '17 at 17:04
  • If you want to merge the base branch into your PR before building, select the former. This helps detect conflicts. See https://go.cloudbees.com/docs/cloudbees-documentation/cje-user-guide/index.html#github-branch-source – Thomas Jan 20 '17 at 17:11
  • Does this answer have issues with branch names starting with `pr-` due to discovering pull requests rather than branches? – dimwittedanimal May 24 '18 at 15:53
4

You get the PR number through for example env.BRANCH_NAME.

if (env.BRANCH_NAME.startsWith('PR-')) {
    def prNum = env.BRANCH_NAME.replace(/^PR-/, '')
    ...
}
stigsb
  • 311
  • 1
  • 3
  • 8
    Thanks for your reply, but unfortunately it can't be applied for my case. In my case BRANCH_NAME does not have any relation to PR Id. For example, we usually have branch names as "feature/JIRA-34" and PR id is not necessary 34. It might be any number. – Aliaksandr Kavalevich Jan 17 '17 at 21:21
  • 1
    Just minor note that in my case replace expression was: `prNumber = env.BRANCH_NAME.replace('PR-', '')` – Dzmitry Hubin Sep 20 '18 at 13:46
4

In the case that Thomas' answer doesn't work or apply to you, you may also (possibly) use the branch name to get the Pull Request number by querying the Github REST API. All you need is an API token and the branch name, lookup the pull requests in order of date updated DESC, and find the first PR that matches your branch name. That will have the Pull Request number.

This only works if you have a unique branch name for each pull request (such as a JIRA issue ticket number).

Michael Butler
  • 6,079
  • 3
  • 38
  • 46