Quickstart →

GitHub Actions matrix strategy: include, exclude, fail-fast and dynamic matrices

Run one job across OS, version and architecture combinations with strategy.matrix: include and exclude, fail-fast, max-parallel, dynamic JSON matrices.

Matrix strategy

A matrix runs the same job once for every combination of the values you list. Two operating systems and three Node.js versions give six jobs, all from one job definition, all running in parallel.

.github/workflows/ci.yml
name: Test
on:
push:
branches: [main]
pull_request:
jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [22, 24, 26]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm test

Each combination becomes its own job, named after its values (test (ubuntu-latest, 22)), on its own runner. Inside the job, ${{ matrix.<key> }} gives the current value. Keys are free-form: os and node mean nothing special to GitHub, which is why runs-on: ${{ matrix.os }} is needed to actually change the runner.

A few facts worth knowing up front:

  • A matrix can generate at most 256 jobs per workflow run.
  • By default the matrix is fail-fast: when one job fails, GitHub cancels the matrix’s in-progress and queued jobs (see below).
  • Matrix jobs run in parallel up to your plan’s concurrency limit. Extra jobs wait in the queue.
  • A job that calls a reusable workflow can have a matrix too: put strategy next to uses: and pass the values in with: (target: ${{ matrix.target }}). Each entry calls the workflow once.
  • To require “all matrix jobs passed” in branch protection, add one job that needs the matrix job and require that one: matrix job names change whenever you edit the values. Give that job if: always(), as in the gate job below.
.github/workflows/ci.yml
jobs:
test:
# ... the matrix job above
all-tests-passed:
if: always()
needs: test
runs-on: ubuntu-latest
steps:
- run: test "${{ needs.test.result }}" = success

Without always(), the gate job is skipped when a test fails, and a skipped required check counts as passing. Keep always() rather than !cancelled(), so that the gate also runs, and fails, when the run is cancelled. needs.test.result covers the whole matrix: one failed entry makes it failure, unless that entry has continue-on-error. Comparing it with success also fails the gate when test was cancelled or skipped. To gate on more jobs, list them all in needs and check each result the same way. See Conditions on needs.

Example: building a Docker image for amd64 and arm64#

The classic multi-architecture build ran both platforms on an amd64 runner, with QEMU emulating arm64. Emulation is slow, often 5 to 10 times slower for compile-heavy builds. GitHub now offers native arm64 Linux runners (ubuntu-24.04-arm) on every plan, so the matrix can pick a native runner per platform:

.github/workflows/docker.yml
name: Build and push Docker images
on:
push:
branches: [main]
jobs:
build:
strategy:
matrix:
include:
- platform: linux/amd64
runner: ubuntu-24.04
suffix: amd64
- platform: linux/arm64
runner: ubuntu-24.04-arm
suffix: arm64
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@v7
- uses: docker/setup-buildx-action@v4
- uses: docker/login-action@v4
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }} # a Docker Hub access token, not your password
- uses: docker/build-push-action@v7
with:
context: .
push: true
platforms: ${{ matrix.platform }}
tags: my-org/my-app:${{ github.sha }}-${{ matrix.suffix }}
cache-from: type=gha,scope=${{ matrix.suffix }}
cache-to: type=gha,mode=max,scope=${{ matrix.suffix }}
manifest:
needs: build
runs-on: ubuntu-latest
steps:
- uses: docker/setup-buildx-action@v4
- uses: docker/login-action@v4
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Combine both images under one multi-arch tag
run: |
docker buildx imagetools create \
-t my-org/my-app:${{ github.sha }} \
my-org/my-app:${{ github.sha }}-amd64 \
my-org/my-app:${{ github.sha }}-arm64
  1. include-only matrix: each entry is a complete combination, so the matrix has exactly two jobs, and each one carries its own runner and tag suffix.
  2. Native runners: runs-on: ${{ matrix.runner }} sends the arm64 build to an arm64 machine, so no QEMU step is needed.
  3. Separate cache scopes: scope= keeps each architecture’s layers apart in the Actions cache, see Docker layer caching.
  4. Manifest job: once both builds pass, docker buildx imagetools create publishes one tag that points to both images, so docker pull my-org/my-app:<sha> picks the right one on any machine.

If you’d rather not maintain the matrix and the manifest job yourself, Docker’s reusable workflows in docker/github-builder ↗ do the same split for you: each platform builds on its own runner, ubuntu-24.04-arm for Arm by default, and the workflow creates the final manifest.

If you have no arm64 runner available, keep a single job with platforms: linux/amd64,linux/arm64 and add docker/setup-qemu-action@v4 before Buildx: slower, but it works anywhere.

Advanced features of the matrix strategy#

Excluding configurations#

In complex workflows, you might not need to run a job for every possible combination in a matrix. GitHub Actions allows you to explicitly exclude certain configurations using the exclude keyword. This is particularly useful when certain combinations are known to be incompatible or unnecessary.

.github/workflows/ci.yml
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [22, 24, 26]
exclude:
- os: windows-latest
node: 22

In this example, the job runs for every combination except Windows with Node 22: five jobs instead of six. A job-level if: can’t read matrix, so use exclude to skip a combination, or put the condition on the job’s steps (see the conditions guide).

Including additional configurations#

Conversely, you can include additional configurations that are not part of the standard matrix combinations using the include keyword. This allows for testing against specific versions or settings without creating a completely separate workflow.

.github/workflows/ci.yml
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [22, 24, 26]
include:
- os: macos-latest
node: 24
experimental: true

Here, an additional job for macOS with Node 24 is added to the matrix, with an extra experimental value. include has a subtle second use: an entry whose values match existing combinations adds keys to them instead of creating a new job. { os: windows-latest, shell: pwsh } would add shell: pwsh to all three Windows jobs. Only entries that match no existing combination become new jobs.

Pair experimental with continue-on-error to let a job fail without failing the run:

.github/workflows/ci.yml
jobs:
test:
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental == true }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
node: [22, 24, 26]
include:
- os: ubuntu-latest
node: 28-nightly
experimental: true

The experimental job tests tomorrow’s Node.js: actions/setup-node resolves 28-nightly to the latest nightly build of Node.js 28, the development line as of November 2026.

fail-fast: false is optional here. Without it, a regular job’s failure still cancels the rest of the matrix, but the experimental job’s failure doesn’t.

Dynamic matrix generation#

For projects that require a dynamic configuration, you can generate the matrix from the output of a previous job. It can’t come from a step in the same job: strategy can read only the github, needs, vars and inputs contexts, so the JSON must be another job’s output, a variable or an input. (For a full explanation of $GITHUB_OUTPUT, job outputs, and the needs context used below, see Passing data between steps and jobs.)

.github/workflows/ci.yml
jobs:
list-packages:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
count: ${{ steps.set-matrix.outputs.count }}
steps:
- uses: actions/checkout@v7
- id: set-matrix
run: |
m=$(ls packages | jq -R -s -c '{include: [split("\n")[] | select(length > 0) | {package: .}]}')
echo "matrix=$m" >> "$GITHUB_OUTPUT"
echo "count=$(jq '.include | length' <<<"$m")" >> "$GITHUB_OUTPUT"
test:
needs: list-packages
if: needs.list-packages.outputs.count != '0'
strategy:
matrix: ${{ fromJson(needs.list-packages.outputs.matrix) }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- run: npm ci
- run: npm test
working-directory: packages/${{ matrix.package }}

list-packages turns the names under packages/ into one include entry each ({"include":[{"package":"api"},{"package":"web"}]}), and fromJson turns that output back into a matrix with one test job per package. An empty matrix is an error, not zero jobs. If the list comes out empty, as it can when a filter keeps only changed packages, {"include":[]} fails the run with Matrix must define at least one vector. The count output guards against that. GitHub evaluates the job’s if: before it expands the matrix, so with no packages, test is skipped instead.

Fail-fast behavior#

By default, GitHub Actions uses a fail-fast strategy for matrix jobs. This means that if one job in the matrix fails, GitHub cancels the matrix’s in-progress and queued jobs. A failure in a job with continue-on-error: true doesn’t cancel anything. This can save time and resources when an issue is detected early in one of the configurations. However, you might want to disable this behavior if you need results from all matrix jobs, regardless of individual failures.

.github/workflows/ci.yml
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
node: [22, 24, 26]

Setting fail-fast to false ensures that all jobs complete, providing a full set of results for analysis.

Max parallel jobs#

To limit the load on a shared pool of runners, such as your own self-hosted runners, or to stay under a rate limit, you can limit the number of jobs that run in parallel using the max-parallel key. This is particularly useful when you have a large matrix and a limited number of runners available.

.github/workflows/ci.yml
strategy:
max-parallel: 2
matrix:
os: [ubuntu-latest, windows-latest]
node: [22, 24, 26]

In this configuration, no more than two jobs will run at the same time, even if more runners are available. This helps in managing the load on the available infrastructure and can be adjusted based on the specific needs of the project or the CI environment. On RunsOn, whether to set it depends on the mode: see On RunsOn.

Using the matrix context and strategy.job-index variable#

GitHub Actions provides a matrix context that can be used within a job to access the current matrix configuration. Additionally, the strategy.job-index variable is available to identify the current job’s index within the matrix strategy. This can be particularly useful for tasks that need to reference the position or order of the job.

.github/workflows/ci.yml
jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [22, 24, 26]
runs-on: ${{ matrix.os }}
steps:
- name: Display matrix and index
run: |
echo "Running on ${{ matrix.os }} with Node.js ${{ matrix.node }}"
echo "This is matrix job #${{ strategy.job-index }} of ${{ strategy.job-total }}"

In this example, each job will output which operating system and Node.js version it is using, along with its index in the matrix. This index is zero-based, so the first job in the matrix will have an index of 0, and strategy.job-total is the number of jobs in the matrix.

Getting results out of a matrix#

All the jobs of a matrix share one set of job outputs, so when several of them set the same output, the last one to finish wins. To collect a result from every entry, have each one upload an artifact with a name of its own, such as results-${{ strategy.job-index }} (uploading to a name that already exists fails), then download them together in a job that needs the matrix job. Outputs from a matrix job shows the full pattern.

Test sharding with a matrix#

Test sharding splits one test suite across several jobs, each running a part of it on its own runner. Give the matrix a shard key with one value per shard and a total key with a single value, then pass both to a test runner that supports sharding: Playwright, Vitest and Jest all take --shard=<index>/<total>. Here Playwright runs end-to-end tests in four shards, and a last job merges their reports into one:

.github/workflows/e2e.yml
name: E2E tests
on:
push:
branches: [main]
pull_request:
jobs:
e2e:
strategy:
fail-fast: false
matrix:
shard: [1, 2, 3, 4]
total: [4]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 24
- run: npm ci
- run: npx playwright install --with-deps
- run: npx playwright test --shard="$SHARD/$TOTAL" --reporter=blob
env:
SHARD: ${{ matrix.shard }}
TOTAL: ${{ matrix.total }}
- uses: actions/upload-artifact@v7
if: ${{ !cancelled() }} # failed shards upload their report too
with:
name: blob-report-${{ matrix.shard }}
path: blob-report
retention-days: 1
e2e-report:
if: ${{ !cancelled() }}
needs: e2e
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 24
- run: npm ci
- uses: actions/download-artifact@v8
with:
pattern: blob-report-*
path: all-blob-reports
merge-multiple: true
- run: npx playwright merge-reports --reporter html ./all-blob-reports
- uses: actions/upload-artifact@v7
with:
name: playwright-report-attempt-${{ github.run_attempt }}
path: playwright-report
retention-days: 14
  1. shard and total: total has a single value, so it adds no jobs. Add both keys to a matrix that already has others, such as the two operating systems and three Node.js versions above, and each combination runs shards 1/4 to 4/4. Don’t use strategy.job-total as the shard count: it counts every job in the matrix (2 × 3 × 4 = 24 in that case), so each combination would run shards 1/24 to 4/24 and silently skip five sixths of the suite.
  2. One report per shard: the blob reporter writes each shard’s results to blob-report/, and each shard uploads it as an artifact with a name of its own. The upload runs even when tests fail, so the merged report shows the failures.
  3. Merging: e2e-report needs the matrix job, so it starts once every shard has finished. !cancelled() lets it run when tests failed but not when the run was cancelled. merge-multiple: true puts all the blob files in one folder (their names contain the shard number, so they don’t clash), and merge-reports turns them into one HTML report. Vitest does the same with --reporter=blob and vitest run --merge-reports.

Split by files or by timings. Vitest splits test files, not test cases, between shards, so a shard that gets a few large files runs longer than the others. Playwright splits files too, unless the project sets fullyParallel: true, which balances individual tests across shards. Tools that split by timing use the durations of an earlier run instead: pytest-split ↗, for example, records them with pytest --store-durations in a .test_durations file you commit, then pytest --splits 4 --group 1 runs the first of four groups of roughly equal duration.

fail-fast and max-parallel. Keep fail-fast: false on a sharded job. With the default, the first failing shard cancels the others, so the merged report covers only the shards that finished. max-parallel caps how many shards run at once: with max-parallel: 2, four shards run in two rounds and take about as long as two shards would. The same happens when the run reaches your plan’s concurrency limit. Each shard repeats the checkout, the install and the browser download, so more shards than can run at once only add setup time. To split work inside one job instead, use parallel steps.

On RunsOn#

RunsOn adds no GitHub-style concurrency cap. Your AWS vCPU quotas set the ceiling (on Fleet, also the max_runners you configure). How to run a large matrix depends on the RunsOn mode.

On Flex, leave max-parallel out: GitHub queues every matrix job when the run is created and Flex starts a runner for each one, so max-parallel holds jobs back but not instance launches. The runners of held-back jobs sit idle, and are removed after 10 minutes without a job. Give each matrix entry a label of its own, built from the job name, the run attempt and the job index, so that entries don’t take each other’s runners:

.github/workflows/ci.yml
jobs:
test:
strategy:
matrix:
node: [22, 24, 26]
runs-on: runs-on=${{ github.run_id }}-test-${{ github.run_attempt }}-${{ strategy.job-index }}/runner=2cpu-linux-x64

On Fleet, the label names a runner the platform team publishes (runs-on/fleet=linux-small/env=production), and GitHub assigns jobs to it only as they become eligible to run. max-parallel works as it does on GitHub-hosted runners and is the way to cap runner demand for a large matrix: see Matrix jobs and max-parallel.