Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub Branch name from which the Pull Request is raised

I'm using Multibranch Pipeline Job in Jenkins.

How do I find the branch name from which the Pull Request is raised in GitHub?

I'm using /github-webhook/ & also tried with /ghprbhook/ and tried with the following environment variables: GIT_LOCAL_BRANCH, GIT_BRANCH, ghprbSourceBranch, but I didn't get any result.

If there are any suggestions, I would love to try them.

like image 232
Sunil Boga Avatar asked Dec 12 '17 14:12

Sunil Boga


People also ask

How can I change my branch name after raising PR?

Open a new PR with a new (renamed) branch. Close the old PR referencing the new one (e.g. Closed in favor of #new_pr_id) Modify the description of the new PR (e.g. Supersedes #old_pr_id) (optional) Make a comment about the relevant discussion on the old PR.

What happens to a branch after pull request?

Pull requests let you tell others about changes you've pushed to a branch in a repository on GitHub. Once a pull request is opened, you can discuss and review the potential changes with collaborators and add follow-up commits before your changes are merged into the base branch.

What is base branch in GitHub pull request?

Repository: The remote repository the Pull Request should be created in. Base Branch: The "Base Branch" is the branch where your new changes should be applied / integrated into. Head Branch: The "Head Branch" is the branch that contains the changes you want to integrate.

What is the branch name in GitHub?

By default, GitHub names the default branch main in any new repository. You can change the default branch for an existing repository.


Video Answer


4 Answers

CHANGE_BRANCH gives the correct name of the source branch of the PR.

CHANGE_TARGET gives the target name of the PR merge

like image 155
jayas Avatar answered Oct 23 '22 23:10

jayas


There are a few different parameters and it can be difficult to find the correct one depending on the context.

BRANCH_NAME

For a multibranch project, this will be set to the name of the branch being built, for example in case you wish to deploy to production from master but not from feature branches; if corresponding to some kind of change request, the name is generally arbitrary (refer to CHANGE_ID and CHANGE_TARGET).

This can either be the branch name (like in branch builds) or some other identifier (like the pull request Id). The documentation is clear that it can be either, but it is surprising behavior if you haven't read the full documentation.

The other answer by MZ2010 shows a way that will probably work as the checkout. It may depend on plugin versions and is probably affected by the same issue as above.

def scmVars = checkout scm
def branchName = scmVars.GIT_BRANCH

The way that that you can get it is if you use the GitHub Branch Source Plugin which supports the CHANGE_BRANCH environment variable. This was added in JENKINS-43418 and you should be able to reference it if you are using env.CHANGE_BRANCH. It may not be available in Multibranch jobs, though.

like image 37
mkobit Avatar answered Oct 24 '22 01:10

mkobit


The environment variable CHANGE_BRANCH should give you the source branch name and CHANGE_TARGET the target branch name. For PR from forks, the CHANGE_FORK gives you the repository name of the fork. For PR from origin, CHANGE_FORK is not set.

To see the description of the environment variables, navigate to a branch job then Pipeline Syntax > Global Variable Reference > env. It should display the description of those variables.

like image 3
Allan BURDAJEWICZ Avatar answered Oct 24 '22 00:10

Allan BURDAJEWICZ


For multibranch pipelines, use:

env.BRANCH_NAME

for accessing the branch name.

Edit: If you need to debug env variables or git variables you can try:

  1. access GIT variables

    def scmVars = checkout scm
    def branchName = scmVars.GIT_BRANCH
    
  2. Print ENV variables

    sh("printenv")
    
like image 1
bp2010 Avatar answered Oct 23 '22 23:10

bp2010