Working with the GitHub Context in Actions #134460
joshjohanning
started this conversation in
Discover
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Overview
Understanding the GitHub context, and how to print out the entire context, can be super useful when working with GitHub Actions. It provides information about the workflow run, the repository, and the event that triggered the workflow. This context is available to every step in a workflow run and can be used in expressions, conditions, and even as out of the box variables.
Contexts
There are actually several contexts in addition to the GitHub context, such as the
env
,job
,jobs
,steps
,runner
,secrets
,strategy
,matrix
,needs
, andinputs
contexts. You probably reference some of these without knowing, such as when you're referencing a secret you would use${{ secrets.MY_SECRET }}
, or when you are using an input from the workflow with${{ inputs.my_input }}
.The GitHub context is the most probably the most commonly used and most helpful context that you don't already know about that provides information about the workflow run, the repository, and the event that triggered the workflow. You might be already using the
github
context without knowing it, such as when you're referencing the repository name with${{ github.repository }}
or the branch/tag name with${{ github.ref_name }}
. However, there are so many more additional properties that you can use! For example, in a workflow trigger via a pull request, you can access the pull request number with${{ github.event.pull_request.number }}
.Working with the GitHub Context
Whenever I'm working on a complex workflow, I always start by printing out the entire GitHub context to see what's available. This is super easy to do with a simple step like this:
You want to do it this way (setting the
GITHUB_CONTEXT
environment variable) for string escaping purposes (if you try toecho '${{ toJSON(github) }}'
directly, this will sometimes error out). Also, this is a good practice to mitigate against script injection attacks since we're directly printing user input!This will print out the entire GitHub context to the log, which you can then use to reference the properties you need. Traverse the JSON to see what's available and what you can use in your workflow. In this example, I can use
${{ github.event.enterprise.name }}
to get the enterprise name of the repository running the workflow.You can even use the contexts in expressions. For example, we can conditionally run a job based on the labels of an issue that triggered the workflow:
This can be super helpful for IssueOps and LabelOps scenarios!
Tip
Follow this pattern of setting any user-provided input to an environment variable before using it in a script to mitigate against script injection attacks.
Exporting all Contexts
You can do the same thing to print out the other contexts as well. Here's a full workflow example:
Summary
Once you understand the GitHub context, so many more variable combinations and expressions become available to you. This can help you write more dynamic and flexible workflows that can adapt to different scenarios. I hope this helps you take your GitHub Actions knowledge to the next level! 🚀
Beta Was this translation helpful? Give feedback.
All reactions