GITHUB_HEAD_REF is inconsistent

Opposite from this topic: GITHUB_REF is inconsistent
I need the name of the branch from within my GitHub actions.

Issue:

  • On pull_request event → expected outcome, I can use the environment variable github.head_ref to get the name of the branch since github.ref is a pull request
  • On pull_request_review event → github.head_ref is empty, but github.ref still reflects the pull request, leaving me without the branch name in this case.

Workflow that requires this:

There are branch protection rules in place on master to prevent anyone (including the GitHub action as we can’t exclude it) from pushing to master. So the file that gets changed within the action needs to be pushed to the pull request incoming branch. We also require pull requests be approved by a reviewer before they can be merged. So we do not want this to fire on a pull_request, but only on the pull_request_review (since no pull_request_approved event exists)

I verified this behavior by echoing both github.head_ref and github.ref for a pull_request event, and in a separate pull request for the event pull_request_review.

Any help is appreciated

Hi @SamuraiTheNinja, welcome to the GitHub Support Community! Is it the head ref you’re looking for? I think you’ll be able to get this from inside the github.event context at github.event.pull_request.head.ref for both of these.

There’s example payloads for the webhook events that trigger workflows on this page so I’d recommend double checking that this is what you’re looking for.

2 Likes

Thank you @thomasshaped that has solved the problem I was having. Finding the right documentation for most of GitHub Actions has been a nightmare, so I really appreciate the help!