Sub topic 10
Contexts and Expressions
This chapter assumes you can read YAML and have written a basic workflow with on, jobs, and steps. It covers how a workflow reads data about the run and computes values from it.
What contexts are
A context is a named object that holds information about the run. You read its properties with dot notation. The ones you will use most often are:
| Context | What it holds | Example |
|---|---|---|
| `github` | Event and repository data | `github.ref`, `github.run_number` |
| `env` | Variables set with `env:` | `env.APP_ENV` |
| `matrix` | The current matrix combination | `matrix.node` |
| `needs` | Outputs of jobs this job depends on | `needs.version.outputs.tag` |
| `steps` | Outputs and results of earlier steps in the same job | `steps.meta.outputs.tag` |
| `secrets` | Repository, environment, and organization secrets | `secrets.API_TOKEN` |
| `inputs` | Inputs of a reusable or manually triggered workflow | `inputs.node-version` |
Expression syntax
An expression is written between ${{ and }}. GitHub evaluates it before the step runs and substitutes the result as text.
steps:
- run: echo "Run ${{ github.run_number }} on ${{ github.ref }}"By the time the shell starts, the command line already contains the final text, for example echo "Run 42 on refs/heads/main". The shell never sees the ${{ }} markers.
Expressions support literals, comparison operators (==, !=, <, &&, ||, !), and built-in functions. Comparing strings with == ignores case.
steps:
- run: echo "Pull request from a fork"
if: github.event.pull_request.head.repo.fork == true
- run: echo "Tag is ${{ startsWith(github.ref, 'refs/tags/') }}"
- run: echo "Key ${{ format('{0}-{1}', runner.os, hashFiles('**/package-lock.json')) }}"Useful functions include contains, startsWith, endsWith, format, join, toJSON, fromJSON, and hashFiles. Status functions (success(), failure(), always(), cancelled()) are used in if conditions, which the next chapter covers.
Where expressions are allowed
Expressions work in most values, such as with, env, run, name, runs-on, and if. Each key can only read some contexts, though. For example, env at workflow level cannot read steps, because no step has run yet. If an expression fails to evaluate, the run reports an error, and a context that has no value usually becomes an empty string.
Expression or shell variable
The same value can often be read two ways. This step shows both.
steps:
- name: Compare
env:
BRANCH: ${{ github.ref_name }}
run: |
echo "Expression: ${{ github.ref_name }}"
echo "Shell variable: $BRANCH"Both print the same text. The difference is when the substitution happens. The expression is pasted into the script before the shell parses it. The shell variable is read by the shell at run time.
Contexts across jobs
Each job runs on its own runner, so steps is empty in another job. Use needs for data from earlier jobs, as in the version example from the outputs chapters:
jobs:
version:
runs-on: ubuntu-latest
outputs:
tag: ${{ steps.meta.outputs.tag }}
steps:
- id: meta
run: echo "tag=v1.${{ github.run_number }}" >> "$GITHUB_OUTPUT"
show:
needs: version
runs-on: ubuntu-latest
steps:
- run: echo "Building ${{ needs.version.outputs.tag }}"Common errors
- Using
steps.x.outputs.yin a different job. Useneedsinstead. - Forgetting that outputs are strings.
fromJSONconverts them, sofromJSON(needs.a.outputs.flag)yields a real boolean. - Reading
env.NAMEinside a job-levelruns-onorifwhere the variable is not yet available. Use a context that is available there, such asgithuborvars. - Writing
if: ${{ ... }}with extra text around it. Inif, the markers are optional, but mixing them with other text turns the whole value into a non-empty string, which is always true.
Exercise
A deploy job needs the tag created by a version job. A step in the deploy job uses ${{ steps.meta.outputs.tag }} and prints an empty string. What is wrong, and what is the fix?
Show answer
The steps context only holds steps from the same job, and meta is in the version job. Expose the value as a job output (outputs: tag: ${{ steps.meta.outputs.tag }}), add needs: version to the deploy job, and read ${{ needs.version.outputs.tag }}.