1

Is there a command to know the targeted branch name of a PR when building this PR?

Context:
I am building some code with a "Multibranch Pipeline" jenkins job.
When I create a Github Pull request, this job is triggered and I would like to know, in this job execution, what is the targeted branch name of this PR.

I have tried different commands in my Jenkinsfile but none are giving me the expected result.

If I try this: git rev-parse --abbrev-ref HEAD it gives me: HEAD

If a execute: git branch I get: * (HEAD detached at 464f73c0)

What I want to get is, for instance, master if the PR is targeting master as destination branch.

Any idea ?
Thanks for your help

Vishwanath
  • 6,284
  • 4
  • 38
  • 57
krissou
  • 41
  • 6
  • If you are doing this in Jenkins and building a PR. The target branch name should be in some environment variable. Check the documentation of Jenkins or it's plugin getting used for this. – Vishwanath Dec 17 '19 at 19:17
  • 1
    Does this answer your question? [Jenkins GitHub pull request builder - get branch name for execute shell](https://stackoverflow.com/questions/30926515/jenkins-github-pull-request-builder-get-branch-name-for-execute-shell) – Dibakar Aditya Dec 17 '19 at 21:35

1 Answers1

3

Thanks to @Vishwanath's idea I was able to find the answer:

In jenkins "Multibranch Pipeline" jobs, when building a PR, the targeted branch is available as an environment variable: CHANGE_TARGET

krissou
  • 41
  • 6
  • This document might be helpful when you are using Jenkins with Bitbucket [Environment variables for Bitbucket Branch Source plugin](https://docs.mohami.io/webhook-to-jenkins-for-bitbucket/environment-variables-for-bitbucket-branch-source-) – Akram Rabie Feb 03 '23 at 16:10