Sub topic 14
Conditions that depend on the matrix
This chapter assumes you know how a strategy.matrix creates one job per combination of values. It also assumes you can write a step-level if and read the github and runner contexts.
Why matrix conditions exist
A matrix runs the same steps in every job it creates. Some steps should not run everywhere. Linting once is enough, and uploading coverage from every operating system wastes time. The matrix context holds the values for the current combination, so a step-level if can compare against them.
Step-level conditions on matrix values
Use matrix.<key> inside the step's if. Each job sees only its own values.
name: Matrix conditions
on: [push, pull_request]
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
node: ['18', '20']
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm test
- name: Lint
if: matrix.os == 'ubuntu-latest'
run: npm run lint
- name: Coverage
if: matrix.os == 'ubuntu-latest' && matrix.node == '20'
run: npm run coverage
- name: Collect Windows logs
if: failure() && runner.os == 'Windows'
run: echo "Collecting logs"Lint runs in two of the four jobs. Coverage runs in one. The last step combines a status function with runner.os. failure() is true only when an earlier step in the same job failed, so the step never runs in a passing job.
Matrix values and types
Matrix values keep their YAML type. An unquoted 20 is a number, and '20' is a string. The == operator coerces types, so matrix.node == '20' matches either form. Quoting versions is still safer, because an unquoted 3.10 becomes the number 3.1.
Job-level if cannot read matrix
The matrix context is not available in a job-level if. GitHub decides whether to skip a job before it expands the matrix, so matrix.os has no value at that point.
jobs:
test:
# Wrong: matrix is not available here
if: matrix.os == 'ubuntu-latest'
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
steps:
- run: echo "never reached as intended"To change which combinations exist, edit the matrix itself with exclude and include.
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: ['18', '20']
exclude:
- os: windows-latest
node: '18'
include:
- os: ubuntu-latest
node: '22'
experimental: trueThe experimental key exists only in the included job. In other jobs matrix.experimental is an empty string, which is falsy, so if: matrix.experimental is true only for the included job.
Choosing between the options
| Goal | Use |
|---|---|
| Skip one step in some jobs | Step-level `if` with `matrix.<key>` |
| Remove a combination entirely | `exclude` |
| Add an extra combination or flag | `include` |
| Skip based on event or branch | Job-level `if` with `github` |
Exercise
A workflow has a matrix of os: [ubuntu-latest, macos-latest]. You want a job-level if: matrix.os == 'ubuntu-latest' so that macOS jobs are skipped. Why does this fail, and what are two correct ways to get the intended result?
Show answer
The matrix context is not available in a job-level if, because the condition is evaluated before the matrix expands. One fix is to remove macos-latest with an exclude entry or to drop it from the os list. The other is to put if: matrix.os == 'ubuntu-latest' on each step you want to limit to Linux.