Sub topic 9
Testing across versions and operating systems with a matrix
This chapter assumes you have a working test workflow like the one from earlier chapters. You should know how jobs, steps, runs-on and uses fit together. You should also know that actions/setup-node installs a Node.js version.
Why one environment is not enough
A test suite that passes on Node.js 20 on Linux can still fail on Windows or on Node.js 18. Path separators, line endings, file permissions and newer language features all differ between environments. If your project is a library, your users run it in all of those environments.
You could copy the test job six times, once per combination. That is tedious and easy to get wrong. A matrix strategy does this for you.
How a matrix works
A matrix is a list of values for one or more variables. GitHub Actions creates one job for every combination of those values. Each job reads its own values through the matrix context, for example ${{ matrix.node-version }}.
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [18, 20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm testThis matrix has two operating systems and three Node.js versions. That gives 2 × 3 = 6 jobs, and they run in parallel when runners are available.
Two details matter here:
runs-onuses${{ matrix.os }}, so the runner itself is part of the matrix.- Each job gets a name built from its values, such as
test (ubuntu-latest, 20). You will see these names in the checks list on a pull request.
Controlling failure behavior with fail-fast
By default, fail-fast is true. When one matrix job fails, GitHub cancels the jobs that are still running or queued. That saves minutes, but it hides information. You see one failure and do not learn whether Windows, Node.js 18, or everything is broken.
Setting fail-fast: false lets every combination finish. For a test matrix this is usually what you want, because the pattern of failures points to the cause. If only the Windows jobs fail, suspect paths or line endings.
Adding and removing combinations
Sometimes the full grid is too large or contains a combination you do not support. Use include and exclude under matrix.
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [18, 20, 22]
exclude:
- os: windows-latest
node-version: 18
include:
- os: ubuntu-latest
node-version: 23exclude removes matching combinations from the grid. include adds extra combinations, or adds extra variables to existing ones. The example above runs five grid jobs plus one extra job on Node.js 23.
Common errors
- Unquoted version numbers. YAML reads
node-version: [3.10]as the number3.1. Quote such values:["3.9", "3.10"]. This matters most for Python versions. - Forgetting
runs-on: ${{ matrix.os }}. The matrix creates jobs, but every job runs on a fixed runner. - Shell differences. Windows runners use PowerShell by default for
runsteps, so Bash-only syntax can fail there. Setshell: bashon a step if you need Bash. - Required checks and names. Branch protection matches checks by name. If you change matrix values, the job names change, and a required check can stay pending forever.
Exercise
A matrix defines os with 3 values and node-version with 4 values, and has one exclude entry that matches exactly one combination. How many jobs run, and why would you set fail-fast to false?
Show answer
3 × 4 = 12 combinations, minus 1 excluded, gives 11 jobs. Setting fail-fast to false lets all 11 finish even if one fails, so you can see which operating systems or versions are affected.