Sub topic 9
Running variations with a matrix
This chapter assumes you can read a basic workflow file and know what a job, a runner, and a step are. It covers the strategy.matrix key, which runs one job definition many times with different values.
What a matrix does
A matrix expands a single job into several jobs, one for each combination of values you list. Each expanded job runs on its own runner, in parallel, up to your plan's concurrency limit. You write the job once and reference the current values through the matrix context.
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
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 testThis produces four jobs: every pairing of two operating systems with two Node versions. The number of jobs is the product of the list lengths, so adding a third list of three values would give twelve.
Controlling the expansion
Three keys adjust how the matrix behaves.
includeadds extra combinations or adds extra values to existing ones.excluderemoves combinations you do not want.fail-fastandmax-parallelcontrol how the jobs run.
strategy:
fail-fast: false
max-parallel: 2
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20]
exclude:
- os: windows-latest
node: 18
include:
- os: ubuntu-latest
node: 22
experimental: trueHere exclude drops the Windows and Node 18 pairing. include adds a fifth job that runs Node 22 on Ubuntu and carries an extra experimental value that the steps can read as matrix.experimental.
By default fail-fast is true. When one matrix job fails, GitHub cancels the jobs still running or queued. Set it to false when you want to see every failure in one run. max-parallel caps how many matrix jobs run at once.
Matrix jobs and later jobs
A job that lists the matrix job in needs waits for every expanded job to finish. If any of them fails, the dependent job is skipped unless you override that with an if condition.
Each expanded job has the same job ID, so files they produce collide unless you give them distinct names. Include a matrix value in the name, as in dist-node-${{ matrix.node }}, when uploading artifacts.
- uses: actions/upload-artifact@v4
with:
name: dist-node-${{ matrix.node }}
path: dist/Common errors
- Writing
node: 20instead of a list. The matrix needs a list such as[20], or the value must be set outside the matrix. - Reading
matrix.nodeoutside the job that defines it. The context exists only inside the expanded jobs. - Uploading artifacts with the same name from every matrix job, which fails in
upload-artifact@v4. - Unquoted version numbers such as
3.10, which YAML reads as the number3.1. Quote them:"3.10".
Exercise
A matrix has os: [ubuntu-latest, macos-latest, windows-latest] and python: ["3.11", "3.12"], with an exclude entry for macos-latest with Python "3.11". How many jobs run, and why should the Python versions be quoted?
Show answer
Five jobs run. The full matrix has 3 × 2 = 6 combinations, and exclude removes one. The versions are quoted so YAML keeps them as strings. Without quotes, a value such as 3.10 would be read as the number 3.1.