Sub topic 10
Controlling matrix runs with fail-fast and max-parallel
This chapter assumes you can write a strategy.matrix block and know how include and exclude change the job list. It covers the two settings that control how those jobs run: fail-fast and max-parallel.
How fail-fast works
By default, fail-fast is true. When any job in the matrix fails, GitHub cancels the matrix jobs that are still running or queued. This saves runner minutes when a failure means the whole run is already bad.
Cancelling early has a cost. A cancelled job reports nothing about its own configuration. If Windows fails, you won't learn whether Node 20 on Ubuntu would also have failed. To see every result in one run, set fail-fast: false.
Limiting concurrency with max-parallel
By default, GitHub runs as many matrix jobs at once as runners allow. max-parallel sets a ceiling on how many run at the same time. Use it when jobs share a limited resource, such as a test database, a rate-limited API, or a small pool of self-hosted runners.
max-parallel only caps concurrency. It doesn't change which jobs exist or how many there are.
A complete example
This workflow combines both settings with exclude and include.
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental == true }}
strategy:
fail-fast: false
max-parallel: 2
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20, 22]
exclude:
- os: windows-latest
node: 18
include:
- os: ubuntu-latest
node: 22
experimental: true
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm testThe matrix starts with six combinations (2 operating systems × 3 Node.js versions). exclude removes Windows with Node 18, which leaves five jobs. include matches the existing Ubuntu and Node 22 job and adds experimental: true to it. It doesn't create a sixth job.
With max-parallel: 2, at most two of the five jobs run at once. With fail-fast: false, a failure in one job doesn't cancel the others.
Tolerating failures in some jobs
The continue-on-error line at the job level makes the experimental job non-blocking. Its failure doesn't fail the workflow run, and with fail-fast: true it also wouldn't cancel the other jobs. For non-experimental jobs, matrix.experimental is undefined, so the expression evaluates to false and they fail normally.
Choosing values
| Situation | Suggested setting |
|---|---|
| Fast feedback on pull requests, and any failure blocks the merge | `fail-fast: true` (default) |
| Compatibility testing where you need every result | `fail-fast: false` |
| Jobs share a limited external resource | Set `max-parallel` |
| One configuration is allowed to fail | `continue-on-error` on that combination |
Fewer parallel jobs also matter for caching. If a matrix job saves a cache that other jobs could restore, running fewer jobs at once lets later jobs find it. That only helps when jobs share a key, which a later chapter on matrix cache keys discusses.
Exercise
A matrix has 2 operating systems and 4 Node.js versions. You exclude one combination and set max-parallel: 3 and fail-fast: false. How many jobs run in total, and what happens if one fails?
Show answer
There are 2 × 4 = 8 combinations, and excluding one leaves 7 jobs. At most 3 run at the same time. Because fail-fast is false, a failing job does not cancel the others, and each of the remaining jobs runs to completion and reports its own result.