Skip to content

Commit ef5f865

Browse files
committed
adding post on GitHub contexts
1 parent 023cace commit ef5f865

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

_posts/2024-06-14-github-context.md

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
---
2+
title: 'GitHub Actions: Working with the GitHub Context'
3+
author: Josh Johanning
4+
date: 2024-06-14 10:30:00 -0500
5+
description: Take your Actions knowledge to the next level by mastering the GitHub context
6+
categories: [GitHub, Actions]
7+
tags: [GitHub, Actions, IssueOps]
8+
media_subpath: /assets/screenshots/2024-06-14-github-context
9+
image:
10+
path: github-context-dark.png
11+
width: 100%
12+
height: 100%
13+
alt: Printing the GitHub context
14+
---
15+
16+
## Overview
17+
18+
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.
19+
20+
## Contexts
21+
22+
{% raw %}
23+
24+
There are actually several [contexts](https://docs.github.com/en/actions/learn-github-actions/contexts) in addition to the GitHub context, such as the `env`, `job`, `jobs`, `steps`, `runner`, `secrets`, `strategy`, `matrix`, `needs`, and `inputs` 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 }}`.
25+
26+
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 }}`.
27+
28+
## Working with the GitHub Context
29+
30+
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:
31+
32+
```yaml
33+
- name: Write GitHub context to log
34+
env:
35+
GITHUB_CONTEXT: ${{ toJSON(github) }}
36+
run: echo "$GITHUB_CONTEXT"
37+
```
38+
39+
You want to do it this way (setting the `GITHUB_CONTEXT` environment variable) for string escaping purposes (if you try to `echo '${{ toJSON(github) }}'` directly, this will sometimes error out). Also, this is a good practice to [mitigate against script injection attacks](https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#good-practices-for-mitigating-script-injection-attacks) since we're directly printing user input!
40+
41+
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.
42+
43+
![Printing the GitHub context](github-context-dark.png){: .shadow }{: .dark }
44+
![Printing the GitHub context](github-context-light.png){: .shadow }{: .light }
45+
_Printing the GitHub context_
46+
47+
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:
48+
49+
```yml
50+
on:
51+
issues:
52+
types: [opened]
53+
54+
jobs:
55+
new-repo-create:
56+
runs-on: ubuntu-latest
57+
if: contains(github.event.issue.labels.*.name, 'new-repo')
58+
steps:
59+
- name: print issue title
60+
env:
61+
ISSUE_TITLE: ${{ github.event.issue.title }}
62+
run: echo "Issue title $ISSUE_TITLE"
63+
- name: print issue body
64+
env:
65+
ISSUE_BODY: ${{ github.event.issue.body }}
66+
run: echo "Issue body $ISSUE_BODY"
67+
- name: print issue author
68+
env:
69+
ISSUE_AUTHOR: ${{ github.event.issue.user.login }}
70+
run: echo "Issue author $ISSUE_AUTHOR"
71+
- name: print issue number
72+
env:
73+
ISSUE_NUMBER: ${{ github.event.issue.number }}
74+
run: echo "Issue number $ISSUE_NUMBER"
75+
```
76+
{: file='.github/workflows/new-repo-create.yml'}
77+
78+
This can be super helpful for IssueOps and LabelOps scenarios!
79+
80+
> 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](https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#good-practices-for-mitigating-script-injection-attacks).
81+
{: .prompt-tip }
82+
83+
## Exporting all Contexts
84+
85+
You can do the same thing to print out the other contexts as well. Here's a full workflow example:
86+
87+
```yml
88+
jobs:
89+
write_contexts_to_log:
90+
runs-on: ubuntu-latest
91+
steps:
92+
- name: Write GitHub context to log
93+
env:
94+
GITHUB_CONTEXT: ${{ toJSON(github) }}
95+
run: echo "$GITHUB_CONTEXT"
96+
- name: Write job context to log
97+
env:
98+
JOB_CONTEXT: ${{ toJSON(job) }}
99+
run: echo "$JOB_CONTEXT"
100+
# this errors out if you try to access it w/o using a reusable workflow
101+
# - name: Write jobs context to log (reusable workflows)
102+
# env:
103+
# JOB_CONTEXT: ${{ toJSON(jobs) }}
104+
# run: echo "$JOBS_CONTEXT"
105+
- name: Write steps context to log
106+
env:
107+
STEPS_CONTEXT: ${{ toJSON(steps) }}
108+
run: echo "$STEPS_CONTEXT"
109+
- name: Write runner context to log
110+
env:
111+
RUNNER_CONTEXT: ${{ toJSON(runner) }}
112+
run: echo "$RUNNER_CONTEXT"
113+
- name: Write strategy context to log
114+
env:
115+
STRATEGY_CONTEXT: ${{ toJSON(strategy) }}
116+
run: echo "$STRATEGY_CONTEXT"
117+
- name: Write matrix context to log
118+
env:
119+
MATRIX_CONTEXT: ${{ toJSON(matrix) }}
120+
run: echo "$MATRIX_CONTEXT"
121+
- name: Write env context to log
122+
env:
123+
ENV_CONTEXT: ${{ toJSON(env) }}
124+
run: echo "$ENV_CONTEXT"
125+
- name: Write secrets context to log
126+
env:
127+
SECRETS_CONTEXT: ${{ toJSON(secrets) }}
128+
run: echo "$SECRETS_CONTEXT"
129+
```
130+
{: file='.github/workflows/write-contexts-to-log.yml'}
131+
132+
{% endraw %}
133+
134+
## Summary
135+
136+
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! 🚀
Loading
Loading

0 commit comments

Comments
 (0)