Output of github.event.pull_request.head is blank

Hello! I’m trying to add a binder badge to PRs on my repo as per Example 1. Whenever I run the action, it fails because of the url. However, in examining the log, it appears $PR_HEAD_REF and $PR_HEAD_USERREPO are not being set. I’ve dug deep into the GitHub Actions docs and can’t figure it out.

I used this post to create a MWE and confirm that the environment variables are being set as empty values.

MWE:

name: Binder Badge
on:
  push:
  workflow_dispatch:
  pull_request_target:
    types: [opened]

jobs:
  binder:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: print vars
      run: |
        echo $PR_HEAD_REF
        echo $PR_HEAD_USERREPO
      env:
        PR_HEAD_REF: ${{ github.event.pull_request.head.ref }}
        PR_HEAD_USERREPO: ${{ github.event.pull_request.head.repo.full_name }}

Result:
image

I’ve seen this syntax in a lot of working examples. Why isn’t ${{ github.event.pull_request.head.ref }} returning anything?

1 Like

I’m not sure what is wrong with your approach, but I think it can be done simpler. You could use github.head_ref directly

github.head_ref (string)
The head_ref or source branch of the pull request in a workflow run. This property is only available when the event that triggers a workflow run is either pull_request or pull_request_target

(There is also github.ref and github.base_ref, by the way)

For the repo name there is github.repository:

github.repository (string)
The owner and repository name. For example, Codertocat/Hello-World .

check out the docs for the github context.

Also, you don’t need to set env vars, to access them, e.g.

    - run: |
        echo ${{ github.head_ref }}
        echo ${{ github.repository }}

But if you do need env vars for some reason, there are corresponding default vars:

  • $GITHUB_HEAD_REF = ${{ github.head_ref }}
  • $GITHUB_REPOSITORY = ${{ github.repository }}

Thanks @laughedelic! I dove into your suggestions, and determined that the underlying issue still matches the title, regardless of how you try to access the info.

It bothered me that the ${{ github.event.pull_request.head }} syntax examples didn’t work, and after some experimentation, it turns out echo $GITHUB_HEAD_REF also returns no value (if I use ${{ github.head_ref }}) or throws an undeclared variable error (if I use $GITHUB_HEAD_REF). So github.event.pull_request.head doesn’t work because head is never being populated:

- name: print vars
      run: |
        echo $GITHUB_REPOSITORY
        echo ${{ github.head_ref}}
        echo ${{ github.ref}}
        echo ${{ github.base_ref}}

Returns:

   [org]/[repo] #(as expected)
   
   refs/heads/[branch_name] #(as expected)
   

This leads me to believe that github.event.pull_request.head and github.event.pull_request.base are not being populated at all because it’s a pull_request_target and not a pull_request event. If that’s the case, is this relatively new behavior (and why)? Otherwise I struggle to understand the prolific number of examples/suggestions (including this one) using a pull_request_target event that suggest github.event.pull_request.base is available per the pull request event docs.

Unlike this post, transitioning from github.head_ref to github.event.pull_request.head.ref made no difference either (both were blank).