Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 3
Message 1 of 6

How to get PR branch name in GitHub Actions

https://help.github.com/en/articles/virtual-environments-for-github-actions#default-environment-vari...
The GITHUB_REF var is currently returning value like "refs/pull/421/merge" which contains no mention of any branch name from which the PR was created. How can I achieve the same?

5 Replies
Pilot Lvl 1
Message 2 of 6

Re: How to get PR branch name in GitHub Actions

The pull request event contains the information you're looking for. If you're in a javascript action, context.payload.pull_request.head.ref will give you the branch name. (where context comes from const {context} = require('@actions/github')).

If you're in yaml, then I believe it would be github.payload.pull_request.head.ref (see https://help.github.com/en/articles/contexts-and-expression-syntax-for-github-actions#github-context).

Copilot Lvl 3
Message 3 of 6

Re: How to get PR branch name in GitHub Actions

@jaredly Here's my problem, I am trying to use bundlesize package for my repo using github actions. The package uses another package called ci-env which reads branch name from process.env.GITHUB_REF here, which is wrong. So I just wanted to replace this env var value with the context value you suggested. But I see here that the GITHUB_ env vars can't be overridden. What should be done now?

Pilot Lvl 1
Message 4 of 6

Re: How to get PR branch name in GitHub Actions

You'll need to fork ci-env to allow the branch name to be overridden (using a different env vbl), I should think

Copilot Lvl 3
Message 5 of 6

Re: How to get PR branch name in GitHub Actions

That should be my final recourse. Is there no simpler solution according to you? Otherwise I will just raise a PR in ci-unv to fix the variable read.
Copilot Lvl 3
Message 6 of 6

Re: How to get PR branch name in GitHub Actions

I have raised a PR to fix this
https://github.com/siddharthkp/ci-env/pull/29/files

process.env.GITHUB_HEAD_REF || process.env.GITHUB_REF && process.env.GITHUB_REF.split('/')[2]

should suffice, right?