Sub topic 4
Which contexts you can read in a condition
This chapter assumes you can read a workflow file (on, jobs, steps) and know that if goes on a step or a job. It covers the data an if expression can read.
What a context is
A context is a named object that holds information about the workflow run. You read a property with dot notation, such as github.event_name or runner.os. An if expression can only read the contexts available at that location. Referencing one that isn't available makes the workflow file invalid, or the value empty.
Contexts by location
Step-level and job-level conditions don't get the same set. A job-level if is evaluated before any runner starts, so it can't see runner or step data.
| Context | Job-level `if` | Step-level `if` | What it holds |
|---|---|---|---|
| `github` | Yes | Yes | Event name, ref, actor, repository, event payload |
| `vars` | Yes | Yes | Configuration variables set in repository or organization settings |
| `inputs` | Yes | Yes | Inputs of a reusable or manually triggered workflow |
| `needs` | Yes | Yes | Outputs and results of jobs listed in `needs` |
| `env` | No | Yes | Environment variables set at workflow, job or step level |
| `runner` | No | Yes | Operating system, architecture of the current runner |
| `job` | No | Yes | Status of the current job |
| `steps` | No | Yes | Outputs, `outcome` and `conclusion` of earlier steps |
| `strategy`, `matrix` | No | Yes | Matrix settings of the current job |
| `secrets` | No | Not directly | See the warning below |
Example
This workflow uses one context from each group.
name: Contexts
on: [push, workflow_dispatch]
jobs:
build:
if: vars.ENABLE_BUILD != 'false'
runs-on: ubuntu-latest
env:
TARGET: staging
steps:
- uses: actions/checkout@v4
- name: Linux and staging only
if: runner.os == 'Linux' && env.TARGET == 'staging'
run: echo "Building for $TARGET"
- name: Manual runs only
if: github.event_name == 'workflow_dispatch'
run: echo "Started by ${{ github.actor }}"The job-level if reads vars, which is allowed. It compares with the string 'false' because variables are always strings. The first step reads runner and env, which exist only once the job is running on a runner.
Common errors
- Reading
envin a job-levelif. Theenvcontext is not available there, so a condition such asenv.TARGET == 'staging'on the job doesn't work. Usevarsorgithubinstead, or move the check to a step. - Reading
stepsorrunnerin a job-levelif. These don't exist before the job starts. Pass data between jobs with job outputs andneeds. - Expecting a value to exist. A missing property evaluates to an empty string, not an error.
vars.TYPO == 'true'is simply false.
Where to look
The documentation has a table named "Context availability" that lists every context for every workflow key. Check it when a condition fails validation.
Exercise
A job has if: env.DEPLOY == 'yes', and DEPLOY is set in the job's own env block. Why does the condition not work as intended, and what are two ways to fix it?
Show answer
The env context is not available in a job-level if, which is evaluated before the job's environment exists. Fix it by using a configuration variable (vars.DEPLOY == 'yes') or another available context such as github, or by removing the job-level if and putting if: env.DEPLOY == 'yes' on each step, where env is available.