Guide · CI for Agent-Written Code
GitHub Actions matrix: include, exclude, fail-fast and dynamic matrices
One job definition, many runs. How GitHub turns a matrix into jobs, what happens when one leg fails, and how to make a matrix a check you can require.
Shipped by Agents Editors · Checked against GitHub's documentation on
What a matrix does
jobs.<job_id>.strategy.matrix turns one job definition into several job runs. Each key under matrix is a variable with a list of values, and GitHub runs the job once for every combination of them. Inside the job, matrix.<variable> holds that run's value.
jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [20, 22, 24]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm test- Jobs are created in the order the variables are written: every
nodevalue for the firstos, then everynodevalue for the second. GitHub does not promise they run or finish in that order. - Variable names are case insensitive:
OSandosare the same variable. - A value can be an object as well as a string or number. A
nodeentry of{ version: 22, env: ... }is read asmatrix.node.version. - A matrix generates at most 256 jobs per workflow run, on GitHub-hosted and self-hosted runners alike.
matrixis available in most of the job's keys, includingruns-on,name,env,concurrency,timeout-minutes,continue-on-errorand every step. It is not available in the job'sif, which GitHub evaluates before the matrix is expanded.
include and exclude
exclude removes combinations; include adds variables to combinations or adds whole new ones. GitHub applies exclude first and include after it, so include can bring back a combination that exclude removed.
| Key | What an entry does | Matching rule |
|---|---|---|
exclude | Removes every combination it matches | A partial match is enough: { os: windows-latest } removes every Windows job |
include, entry fits | Adds its extra keys to every original combination it can join without changing an original value | Its keys that are already matrix variables must equal that combination's values |
include, entry does not fit | Becomes a new job of its own, with only the keys it lists | Applies when joining any original combination would overwrite an original value |
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [20, 22, 24]
exclude:
- os: windows-latest
node: 20
include:
- os: ubuntu-latest
node: 24
coverage: true
- os: macos-latest
node: 24That runs five of the six original jobs, sets matrix.coverage only on the Ubuntu and Node 24 job, and adds a sixth job on macOS. In the jobs where an include added nothing, matrix.coverage is empty, so a step can use if: ${{ matrix.coverage }} to run only where it was set.
Two details of include trip people up. Values an include entry added can be overwritten by a later include entry, but original matrix values never are. And a matrix with only include and no variables runs exactly the listed entries, one job each: a short way to write a fixed list of configurations that share one job definition.
When one leg fails: fail-fast and continue-on-error
strategy.fail-fast defaults to true. When any job in the matrix fails, GitHub cancels every job in that matrix that is still running or queued. continue-on-error works per job: a job with continue-on-error: true can fail without failing the workflow run, and its failure does not trigger fail-fast for the others.
jobs:
test:
runs-on: ubuntu-latest
continue-on-error: ${{ matrix.experimental }}
strategy:
fail-fast: false
matrix:
node: [20, 22, 24]
experimental: [false]
include:
- node: 25
experimental: trueWhether to keep fail-fast on is a trade between runner time and information. With it on, the first failure stops the other legs, and they show as cancelled, not passed or failed. With it off, every leg reports, and you can tell a failure on one platform from a failure everywhere.
When a coding agent reads CI to decide its next push, that difference matters: a run where one leg failed and the rest were cancelled tells it less than a run where every leg finished. On pull request matrices that agents iterate against, fail-fast: false usually pays for itself; superseded runs are better stopped with concurrency and cancel-in-progress than with fail-fast.
max-parallel and the account's job limit
By default GitHub starts as many legs at once as runner availability allows. strategy.max-parallel caps that for one matrix, which is useful when the jobs share something that cannot take the load, such as a test database, a rate-limited API or a small pool of self-hosted runners.
strategy:
max-parallel: 2
matrix:
os: [ubuntu-latest, windows-latest]
node: [20, 22, 24]Separately, every leg counts against the account's concurrent job limit for GitHub-hosted runners, which is set by the plan, not the workflow. A wide matrix on a busy account can make other workflows wait for a slot. GitHub lists the current numbers on its Actions limits page.
A matrix built from another job's output
The matrix does not have to be written out in the workflow. A job can compute it, for example from the packages a pull request touched, and pass it as an output. Outputs are strings, so the matrix job reads it with fromJSON.
jobs:
plan:
runs-on: ubuntu-latest
outputs:
packages: ${{ steps.list.outputs.packages }}
steps:
- uses: actions/checkout@v4
- id: list
run: echo "packages=$(./scripts/changed-packages.sh)" >> "$GITHUB_OUTPUT"
test:
needs: plan
if: ${{ needs.plan.outputs.packages != '[]' }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
package: ${{ fromJSON(needs.plan.outputs.packages) }}
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm test --workspace ${{ matrix.package }}- The script has to print a JSON array on one line, such as
["api","web"].changed-packages.shhere stands for whatever your repository uses to work that out. - Guard against an empty list. A matrix with no values is an error, not a skipped job, so check the output in the job's
if, which runs before the matrix is expanded. - A skipped matrix job is not a failure, and that is the point of the next section: if this job is a required check, decide what a skipped run should report.
Running only the tests a change can reach is the main reason teams build matrices this way. How to do that without losing the verdict of a full run is covered in selective test execution.
Sharding one test suite across legs
A matrix can also split one suite into parts rather than test several configurations. Give the matrix a shard number and pass it to a test runner that can split its own suite; Jest, Vitest and Playwright all take a --shard=<index>/<total> flag.
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
shard: [1, 2, 3, 4]
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npx vitest run --shard=${{ matrix.shard }}/${{ strategy.job-total }}The strategy context describes the matrix the job is in: strategy.job-total is the number of jobs in it, and strategy.job-index is this job's position, counted from 0. Use them to name per-leg artifacts uniquely, as in name: report-${{ strategy.job-index }}. Sharding only helps when each leg's setup, such as checkout and dependency install, is small next to the tests it runs; warm caches keep it that way.
Outputs and needs across a matrix
A job that needs a matrix job waits for every leg. Outputs from the legs are merged into one set of outputs for the matrix job, and GitHub does not guarantee which leg finishes last. If two legs set the same output name, the last one to finish wins; an empty value does not overwrite a value another leg set. Give each leg its own output name, or upload an artifact per leg and collect them in the next job.
Requiring a matrix job as a status check
Every leg reports its own check, named after the job with its matrix values, such as test (ubuntu-latest, 22). Branch protection and rulesets require checks by exact name, so requiring the legs one by one ties the rule to today's matrix: add Node 26 or drop Windows and the rule waits for a check that no longer runs, or misses the new one.
Instead, require one job that sits after the matrix and reports its overall result:
tests-passed:
needs: test
if: ${{ always() }}
runs-on: ubuntu-latest
steps:
- if: ${{ needs.test.result != 'success' && needs.test.result != 'skipped' }}
run: exit 1if: always()matters. Without it, a failed leg makes this job skip, and GitHub documents that a job skipped because a job it needs failed may not block merging.- Whether
skippedshould pass depends on why the matrix can be skipped. With a guarded dynamic matrix like the one above, a skipped run means there was nothing to test, so it passes here. Drop that condition if a skip should never count as green. - Require
tests-passed, not the legs. The matrix can then change without touching branch protection. - If the repository uses a merge queue, the workflow needs a
merge_grouptrigger for this check to be reported there too.
Common mistakes
- A job-level
concurrencygroup without the matrix values in its name. Every leg lands in the same group and they cancel or wait on each other. Include them, as ingroup: test-${{ github.ref }}-${{ matrix.os }}-${{ matrix.node }}. - Referencing
matrixin the job'sif. The job'sifis evaluated before the matrix exists; put per-leg conditions on steps instead. - Expecting
includeto overwrite an original value. It never changes an original combination; an entry that would have to becomes a separate job. - Reading a cancelled leg as a pass or a failure. Under fail-fast, cancelled legs did not finish and say nothing about their configuration.
- Artifact names that collide. Every leg uploading an artifact called
reportfails or overwrites; add the matrix values orstrategy.job-indexto the name. - Matrices that grow by multiplication. Three variables of four values each is 64 jobs per run, and the limit is 256; use
includefor the combinations you actually support.
Questions
How many jobs can a GitHub Actions matrix create?
At most 256 jobs per workflow run, on both GitHub-hosted and self-hosted runners. How many of them run at once depends on max-parallel, runner availability and your account's concurrent job limit.
Does fail-fast default to true?
Yes. Unless you set fail-fast: false, a failure in any leg cancels the matrix's legs that are still running or queued. Jobs with continue-on-error: true can fail without triggering it.
Can I use matrix values in a job's if condition?
No. GitHub evaluates jobs.<job_id>.if before it expands the matrix, so matrix is not available there. Use step-level if conditions, or remove the combination with exclude.
How do I build a matrix from a previous job?
Have the earlier job write a JSON array to $GITHUB_OUTPUT, expose it as a job output, and read it in the matrix with fromJSON(needs.<job>.outputs.<name>). Guard the matrix job with an if so an empty list skips it instead of erroring.
How do I make a matrix job a required status check?
Add a job that needs the matrix job, runs with if: always(), and fails unless the matrix succeeded. Require that one job's check rather than each leg, so changing the matrix does not break branch protection.



