Sub topic 7
Running one job across a build matrix
This chapter assumes you can write a basic workflow file under .github/workflows, read YAML, and use a package manager such as npm. You should also know what a job, a step, and a runner are.
What a matrix does
A build matrix is a set of variable lists that GitHub Actions expands into many jobs. You write the job once. Actions creates one copy of it for every combination of values. This is how you test on several operating systems or language versions without copying the job.
You define a matrix under strategy.matrix. Each key becomes a variable that you read through the matrix context, for example ${{ matrix.node }}.
A first matrix
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm testThere are two operating systems and three Node.js versions. Actions multiplies them and creates six jobs. Each job runs on its own runner, and all six appear in the workflow run.
Two details matter here:
runs-oncan use a matrix value. This is how one job definition targets different operating systems.node: [18, 20, 22]holds numbers. Be careful with versions such as3.10in Python. YAML reads an unquoted3.10as the number3.1. Write"3.10"as a string.
How expansion works
The jobs are independent. They do not share a filesystem, and they may run at the same time. Each one checks out the code and installs its own dependencies. This repeated work is the reason caching matters so much for matrices. The later chapters on matrix cache keys build on this.
Job names in the interface
Actions names each matrix job after the job ID and the matrix values, such as test (ubuntu-latest, 20). If you use branch protection with required status checks, you must require each of these names. Changing the matrix changes the names, and a required check that no longer exists can block merges.
Common errors
- Using a matrix variable outside the job. The
matrixcontext exists only inside the job that defines it. You cannot read it in the workflow-levelenvor in another job. - Misspelling a key.
${{ matrix.nodes }}does not fail. It evaluates to an empty string, andsetup-nodemay then use a default version. Check that the names inmatrixand in your expressions match exactly. - Expecting a limit that does not exist. A matrix can produce at most 256 jobs per workflow run. A larger matrix makes the run fail.
- Forgetting that every job costs runner time. Six jobs use six times the minutes of one job. Windows runners also use a higher billing multiplier than Linux runners on private repositories.
Check your understanding
Exercise
A workflow defines os: [ubuntu-latest, macos-latest, windows-latest] and python: ["3.10", "3.11", "3.12", "3.13"]. How many jobs does Actions create, and why should 3.10 be quoted?
Show answer
It creates 12 jobs (3 operating systems times 4 Python versions). The quotes keep YAML from reading 3.10 as the number 3.1, which would select the wrong Python version.
Exercise
Your job has runs-on: ubuntu-latest and uses ${{ matrix.node }}, but the matrix defines the key node-version. What happens?
Show answer
The expression evaluates to an empty string without an error, so setup-node receives no version and falls back to its default. Rename the key or the expression so that they match.