Sub topic 6
Contexts: github, runner, env, job, steps, and more
This chapter assumes you can read a workflow file (on, jobs, runs-on, steps) and know the ${{ }} expression syntax from the previous chapter.
What a context is
A context is an object that GitHub builds while a workflow runs. You read its properties inside ${{ }} with dot notation, such as github.ref. GitHub evaluates the expression on its own servers or on the runner, before the shell sees the step. A context is not an environment variable, although some contexts hold similar data.
The main contexts
| Context | What it holds | Example |
|---|---|---|
| `github` | Event, repository, actor, ref, run data | `github.event_name` |
| `env` | Variables set with `env` at workflow, job, or step scope | `env.APP` |
| `vars` | Configuration variables | `vars.DEPLOY_REGION` |
| `job` | Information about the running job | `job.status` |
| `steps` | Outputs and results of earlier steps in this job | `steps.first.outputs.answer` |
| `runner` | The machine running the job | `runner.os` |
| `secrets` | Secret values | `secrets.API_TOKEN` |
| `needs` | Outputs of jobs this job depends on | `needs.build.outputs.version` |
| `strategy`, `matrix` | Matrix settings for the current job | `matrix.node` |
| `inputs` | Inputs of a manual or reusable workflow | `inputs.environment` |
Reading several contexts
This workflow reads from five contexts. Each value is copied into env first, so the shell treats it as data.
name: contexts-tour
on: push
env:
APP: demo
jobs:
tour:
runs-on: ubuntu-latest
steps:
- id: first
run: echo "answer=42" >> "$GITHUB_OUTPUT"
- name: Read contexts
env:
ACTOR: ${{ github.actor }}
OS_NAME: ${{ runner.os }}
APP_NAME: ${{ env.APP }}
JOB_STATUS: ${{ job.status }}
ANSWER: ${{ steps.first.outputs.answer }}
run: |
echo "Actor: $ACTOR"
echo "Runner OS: $OS_NAME"
echo "App: $APP_NAME"
echo "Job status: $JOB_STATUS"
echo "Answer: $ANSWER"The step with id: first is what makes steps.first exist. Without an id, later steps cannot refer to it.
Where each context comes from
Common errors
- Reading a context from the wrong scope.
stepsonly covers the current job. To use a value from another job, expose it as a job output and read it throughneeds. - Confusing
env.NAMEwith$NAME.${{ env.NAME }}is resolved by GitHub before the step starts.$NAMEis resolved by the shell while the step runs. A variable written toGITHUB_ENVmid-job works with$NAMEin later steps, but not in the same step. - Expecting every context everywhere. A key such as
runs-oncannot readsteps, because no step has run yet. A later chapter covers availability by key. - Misspelled properties. A missing property evaluates to an empty string without an error. If a value is blank, check the spelling and the context first.
Exercise
A job named deploy needs the version string produced by a step with id: ver in a job named build. Which context do you use in deploy, and why can't you use steps.ver.outputs.version?
Show answer
Use needs.build.outputs.version, after declaring needs: build in deploy and mapping the step output to a job output in build. The steps context only contains steps from the current job, so deploy cannot see steps.ver.