Search code examples
github-actionsgithub-actions-artifacts

Complete list of github actions contexts


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.


Solution

  • 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"