Access owner and branch of a PR head within GitHub Actions

Imagine I have the repository me/myrepo. A PR tries to merge someone-else/myrepo/feature into me/myrepo/master. I need to somehow access someone-else (owner) and feature (branch) within a GitHub Actions workflow.

From the environment variables I successfully utilize GITHUB_HEAD_REF for the branch. For the owner I use GITHUB_ACTOR which only works as long as only the person who created the PR adds commits.

Is there a way to get the owner of a PR without relying on the actor that triggered the workflow?

@pmeier,

You can use the expression (github.event.pull_request.head.repo.owner.login) to get the owner of the head repository from the github context.

on: pull_request
jobs:
  job1:
    . . .
    steps:
      - name: get the owner of the head repository
        run: echo "head owner = ${{ github.event.pull_request.head.repo.owner.login }}"

Of course, if you feel that this expression is too long, you can map it as a environment variable with a shorter name.

on: pull_request

env:
  HEAD_OWNER: ${{ github.event.pull_request.head.repo.owner.login }}

jobs:
  job1:
    . . .
    steps:
      - name: get the owner of the head repository
        run: echo "head owner = ${{ env.HEAD_OWNER }}"

About the default environment variables automatically set by GitHub, most of them (start with the prefix ‘GITHUB_’) have the corresponding properties in the github context.
However, not every property in the github context will be mapped as a default environment variable. So, many times you need to use the appropriate expression to fetch the value of the property from the github context.
Other Contexts are similar.

You can print the github context to view more information about the current workflow run and the event that triggered the run.

- name: Print the GitHub context
  env:
    GITHUB_CONTEXT: ${{ toJson(github) }}
  run: echo "$GITHUB_CONTEXT"

Thanks for the tip. I didn’t know about the github context. I’ll give it a try an get back to you if that works for me. Problem might be that I need this to work regardless if I’m within a PR or the PR is merged into master.

@pmeier,

When the workflow is not triggered by the events that related to the PR, if you want to get the information of the PR in this workflow, you can execute the “Get a pull request” API in the workflow, and extract the information you need from the response of the API.

@pmeier,

How are things going?
Have you tried as my above suggestion that using the expression (github.event.pull_request.head.repo.owner.login) to get the owner of the head repository from the github context, or the “Get a pull request” API? Is it helpful for you?

Please try it. Any update, feel free to tell me. Thanks.

Hey @brightran, thanks for checking in. Something else came up and I have to handle this first. From what I understand from skimming the links you posted, this is exactly what I need. I’ll just accept your answer as solution for now and ping you if it somehow does not work as I intended it.

Thanks again for your support :slight_smile:

@pmeier,

You’re welcome.
Any questions about this topic, feel free to contact me.