Sub topic 7
Expressions and contexts you need
This chapter assumes you can write a basic workflow (on, jobs, steps) and know that job conditions come later in this tutorial. Here you learn only the expression syntax and the contexts that the change-detection pattern relies on.
Expressions
An expression is code that GitHub evaluates before a step or job runs. You write it between ${{ and }}. Expressions can read values, compare them, and call built-in functions.
In an if: key you may leave out the ${{ }} wrapper, because GitHub treats the whole value as an expression. Everywhere else, such as run, env, and with, the wrapper is required.
Strings in expressions use single quotes. Double quotes are a syntax error.
Contexts
A context is an object that holds information about the run. These are the ones this tutorial uses:
| Context | What it holds | Example |
|---|---|---|
| `github` | Event and repository data | `github.event_name`, `github.ref` |
| `needs` | Results and outputs of jobs listed in `needs` | `needs.changes.outputs.backend` |
| `steps` | Outputs and results of earlier steps in the same job | `steps.filter.outputs.backend` |
| `matrix` | The current matrix values | `matrix.package` |
| `env`, `vars` | Environment variables and repository variables | `vars.RUN_SLOW_TESTS` |
Not every context works everywhere. For example, steps is available inside a job, but a job-level if cannot read it. A job-level if can read github, needs, vars, and inputs.
Functions
These functions appear often in monorepo workflows:
contains(search, item)checks whether a string contains a value, or whether an array has an item.startsWith(string, prefix)checks the beginning of a string.fromJSON(string)turns a JSON string into an object or array.always(),success(),failure(), andcancelled()check job status. They are only valid inifconditions.
Outputs are strings
This is the most common source of bugs. Every job output and step output is a string, even when it looks like a boolean. The output true is the text 'true', so compare it as text.
name: expressions-demo
on:
pull_request:
jobs:
detect:
runs-on: ubuntu-latest
outputs:
docs-only: ${{ steps.check.outputs.docs-only }}
steps:
- id: check
run: echo "docs-only=true" >> "$GITHUB_OUTPUT"
test:
needs: detect
if: needs.detect.outputs.docs-only != 'true'
runs-on: ubuntu-latest
steps:
- run: echo "Running tests for ${{ github.event_name }}"The detect job exposes a step output as a job output. The test job reads it through needs and runs only when the value is not the string 'true'.
Common errors
- YAML parsing. A value that starts with
!or{must be quoted, so writeif: '${{ !cancelled() }}'. - Wrong context. A job-level
ifthat readssteps.*fails validation. Pass the value through a job output instead. - Skipped dependencies. If a job in
needswas skipped, the dependent job is skipped too, unless itsifincludes a status function such asalways(). A later chapter covers this.
Exercise
A job changes has the output backend with the value false. A second job has needs: changes and if: needs.changes.outputs.backend. Does the second job run, and how do you fix the condition?
Show answer
It runs, because the output is the non-empty string 'false', which is truthy. Fix it by comparing text: if: needs.changes.outputs.backend == 'true'.