Sub topic 7
Running one job across a build matrix
This chapter covers how strategy.matrix turns one job definition into many jobs. It assumes you can read a workflow file and already know what jobs, steps, and runs-on do. It also assumes you can add a file under .github/workflows. Caching comes back in a later chapter. Here the focus is how a matrix is built and what goes wrong with it.
The problem a matrix solves
Suppose your library must work on Node.js 18, 20, and 22, on Linux and Windows. Without a matrix you would copy the same job six times and edit two values in each copy. When a step changes, you would have to edit all six.
A build matrix is a set of named variables, each with a list of values. GitHub Actions creates one job for every combination of those values. You write the job once, and each copy reads its own values from the matrix context.
A first matrix
name: CI
on: [push, pull_request]
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 testThree parts matter here.
strategy.matrixdeclares two variables,osandnode. The names are yours to choose.${{ matrix.os }}and${{ matrix.node }}read the current job's values. Any field in the job can use them, includingruns-on,with:inputs,env,if, andname.- The
stepsblock is written once. Every generated job runs the same steps with different values substituted.
How expansion works
GitHub computes the cross product of all lists. Two operating systems and three Node.js versions give 2 × 3 = 6 jobs. The runner assigns each job one value from every list.
The six jobs are independent. Each one gets its own fresh runner, so one job cannot see files that another job created. They start in parallel, limited by your plan's concurrency. A later chapter covers how to control that.
In the Actions UI, each generated job is named from the job ID and its matrix values, such as test (ubuntu-latest, 18). These names matter when you configure branch protection. A required status check refers to the generated name, not to test. If you change the matrix values, the names change too, and a required check that points to an old name will wait forever.
Matrix values are not only strings
A matrix variable can hold numbers, strings, booleans, or objects. Be careful with the YAML parser, because it decides the type before Actions sees the value.
The classic failure is a Python version list:
strategy:
matrix:
python: [3.8, 3.9, 3.10]YAML reads 3.10 as the number 3.1. The job asks setup-python for version 3.1 and fails or picks the wrong interpreter. Quote version strings that could be mistaken for numbers:
strategy:
matrix:
python: ["3.8", "3.9", "3.10"]Node.js major versions such as 18 and 20 are safe as numbers. Versions with a minor part, such as 3.10 or 1.20, are where this goes wrong. Quoting every version is a cheap habit that removes the risk.
Matrix variables that hold objects
When values must travel together, put objects in the list instead of building a cross product of independent variables. Each object becomes one job:
jobs:
build:
runs-on: ${{ matrix.target.os }}
strategy:
matrix:
target:
- { os: ubuntu-latest, shell: bash, label: linux }
- { os: windows-latest, shell: pwsh, label: windows }
name: build-${{ matrix.target.label }}
steps:
- uses: actions/checkout@v4
- run: echo "Running on ${{ matrix.target.label }}"
shell: ${{ matrix.target.shell }}This matrix has one variable, target, with two values, so it produces two jobs. Inside a job, matrix.target.os reads one field of the object. Use this form when some settings only make sense in pairs. A later chapter shows include and exclude, which handle the same need by adjusting the cross product.
The name: key shows another useful feature. It replaces the default job name, which makes the UI and status checks easier to read.
The operating system changes more than `runs-on`
A matrix across operating systems means your steps run in different environments. Common differences:
| Area | Linux and macOS | Windows |
|---|---|---|
| Default shell for `run` | `bash` | `pwsh` (PowerShell) |
| Path separator | `/` | `\` |
| Line endings in checkout | LF | Can become CRLF depending on Git settings |
| Home directory | `/home/runner`, `/Users/runner` | `C:\Users\runneradmin` |
A step such as run: export FOO=bar works on Linux and fails in PowerShell. If you need the same script on every operating system, set shell: bash on the step, or set a default for the whole job:
jobs:
test:
runs-on: ${{ matrix.os }}
defaults:
run:
shell: bash
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v4
- run: echo "Hello from $RUNNER_OS"Git Bash ships on Windows runners, so shell: bash works there. Also remember that ~/.npm and similar paths expand differently per operating system. This matters when you cache dependencies in a later chapter.
Using matrix values in conditions
Matrix values are available to if: conditions, so one step can run for only some combinations:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm test
- name: Upload coverage
if: matrix.os == 'ubuntu-latest' && matrix.node == 22
run: echo "Upload coverage from one job only"Inside if: you can omit the ${{ }} wrapper. This pattern avoids uploading the same report six times. Prefer a condition like this over splitting the matrix when only one step differs.
Limits and edge cases
- Job count. A single matrix can generate at most 256 jobs. A workflow run that exceeds this fails at planning time, before any job starts.
- Cost grows quickly. Adding a third variable with four values multiplies the job count by four. Each job is billed for its runner minutes. A matrix of 3 × 2 × 4 = 24 jobs costs 24 runner startups, each of which runs
checkoutand installs dependencies. - Empty lists. A variable with an empty list produces no jobs, so the whole job is skipped. Check this if a generated matrix is empty.
- Reserved names. The names
includeandexcludeare special keys insidematrix. Do not use them as variable names. - Dependencies on a matrix job. If another job uses
needs: test, it waits for every generated job. It runs only if all of them succeed, unless you add a condition such asif: always(). - Outputs. Several matrix jobs writing the same job output race each other. Use unique output names or upload artifacts instead.
- Variables are per job.
matrix.nodeexists inside the job that uses it. It is not available in a different job, even if that job hasneeds.
Choosing what belongs in the matrix
A matrix works best for dimensions where you want identical steps with different inputs. Good candidates are language versions, operating systems, and database versions for integration tests.
Avoid matrix variables for things that need different steps. A deploy job and a test job should be separate jobs. Forcing them into one matrix leads to many if: conditions and hard-to-read logs.
Also consider whether you need every combination. Testing the oldest and newest language version on every operating system, but only the middle version on Linux, covers most risk with fewer jobs. The next chapter shows how to shape the matrix that way.
Common errors
- Unquoted version numbers.
3.10becomes3.1. Quote the values. - Using
matrixoutside a job. Thematrixcontext exists only in the job that definesstrategy. It is not available at the workflow level, soenv:at the top of the file cannot read it. - Referencing an undefined variable. A typo such as
${{ matrix.nodes }}evaluates to an empty string, and no error is raised. The step then runs with an emptynode-version. Compare variable names against thematrixblock when a job behaves oddly. - Shell mismatch on Windows. Bash syntax in a
runstep fails under PowerShell. Setshell: bashwhere needed. - Stale required checks. Renaming matrix values changes generated job names and breaks branch protection rules that name them.
Exercise
A workflow has matrix: { os: [ubuntu-latest, macos-latest, windows-latest], python: [3.9, 3.10, 3.11], arch: [x64, arm64] }. How many jobs does it generate, and what is wrong with the python list?
Show answer
It generates 3 × 3 × 2 = 18 jobs. The python list has a problem because YAML reads 3.10 as the number 3.1, so that job requests Python 3.1. Quote the versions as strings: ["3.9", "3.10", "3.11"].
Exercise
A job named test uses a matrix over os and node. A step should upload a coverage report from only the Ubuntu job running Node.js 22. How do you write that, and why is it better than removing the step from the matrix?
Show answer
Add if: matrix.os == 'ubuntu-latest' && matrix.node == 22 to the step. The matrix still tests every combination with identical steps, and only one job uploads the report. Splitting the work into a separate job would duplicate setup and make the workflow harder to maintain.