21

While investigating how, within github actions, I can diff the "base" version of a specific file in a repository, with the Pull Request's ("head") version of the file ... while investigating this, I found among various sources (for example, github.community, and code examples in the github.com/actions/checkout README file) ... I found that the following context variables are available:

  • github.ref
  • github.sha
  • github.event.pull_request.head.ref
  • github.event.pull_request.head.sha
  • github.event.pull_request.base.ref
  • github.event.pull_request.base.sha

However, other than the first two (github.ref and github.sha) I cannot find the other four in any of the github actions documentation.

My question is this: Is there any place where the full list of available context variables is documented?

I have, for example, found this, but it only lists context variables one level down from the github context object. I can't find documentation for the more deeply nested variables noted above. Likely there are other context variables that may be very useful, but I can't seem to find a complete list, rather only those that happen to be mentioned and scatter about in various code examples.

Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61

1 Answers1

25

I think you want to distinguish between context variables and payloads.

Context variables are available in most cases and exceptions are documented (as you found): https://docs.github.com/en/actions/learn-github-actions/contexts#example-printing-context-information-to-the-log

The payload, on the other hand, depends on the event type. If you run a workflow as a result of on: pull_request you'll get a different payload than running it as a result of on: push (etc..).

I have never seen docs that list all payloads, but I believe you can take inspiration from the webhooks. For example, if you run a workflow when a pull request is created, you could look at the webhook payload for pull requests here: https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#webhook-payload-example-33

Since the two not are documented to be the same, you might have to resort back to just dumping an event and checking what you actually get. In the docs, GitHub has an example how to dump contexts as part of a workflow:

jobs:
  one:
    runs-on: ubuntu-latest
    steps:
      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJSON(github) }}
        run: echo "$GITHUB_CONTEXT"
rethab
  • 7,170
  • 29
  • 46
  • 2
    Thanks. This is helpful. I was actually thinking to dump the context as a way of exploring it but could not figure out how. Then, since you mentioned it, a quick search found this: https://docs.github.com/en/actions/learn-github-actions/contexts#example-printing-context-information-to-the-log-file Thanks. Hopefully that will give me everything I need. – Daniel Goldfarb Nov 25 '21 at 13:06
  • 1
    If you stumbled on this answer looking for a quick way to print/log all Actions contexts for debugging purposes, there is an action that allows you to do it in one line: `- uses: crazy-max/ghaction-dump-context@v1`. – V. Rubinetti Feb 07 '23 at 23:29
  • 1
    The link address for my comment above has changed. The new link is: https://docs.github.com/en/actions/learn-github-actions/contexts#example-printing-context-information-to-the-log – Daniel Goldfarb Aug 31 '23 at 20:22