Skip to main content
GitHub Actions Matrix Builds That Don't Waste Minutes

GitHub Actions Matrix Builds That Don't Waste Minutes

KU
· Founder & Chief Editor
• 8 min read•Updated •51 views

A practical guide to matrix builds in GitHub Actions that tests across versions and platforms without burning your monthly minutes.

Key takeaways

  • A GitHub Actions matrix declares dimensions under strategy.matrix and runs one job per combination, up to 256 jobs per workflow run.
  • Keep it cheap by trimming with exclude, adding edge cases with include, keeping fail-fast on for pull requests, and running macOS only where behavior differs, since macOS minutes cost about ten times Linux.

A matrix build declares dimensions under strategy.matrix, and GitHub Actions runs one parallel job per combination, up to 256 per workflow run. To keep it cheap: trim combinations with exclude, add single edge cases with include instead of new dimensions, leave fail-fast on for pull requests, and keep most jobs on Linux runners.

Verified against

GitHub Actions workflow syntax and runner pricing docs, Node.js 22, 24 and 26 LTS, Python 3.12 to 3.15, actions/checkout@v6, actions/setup-node@v7, actions/cache v6.1.0, against docs.github.com, nodejs.org and devguide.python.org. Checked October 2026.

A matrix is the single biggest lever for testing broadly, and if you are not careful, the fastest way to drain your minutes.

The basic syntax

The matrix lives under strategy.matrix. Every key you add becomes a dimension, and GitHub multiplies the dimensions together to produce one job per combination.

YAML
# .github/workflows/test.yml
name: test
on: [push, pull_request]

jobs:
  test:
    strategy:
      matrix:
        node: [22, 24, 26]
        os: [ubuntu-latest, macos-latest]
    runs-on: ${{ matrix.os }}
    name: node ${{ matrix.node }} on ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v7
        with:
          node-version: ${{ matrix.node }}
      - run: npm ci
      - run: npm test

Three Node LTS lines times two operating systems is six jobs. That number matters, because GitHub's runner pricing (as of October 2026) is $0.006 per minute for a standard Linux runner, $0.010 for Windows and $0.062 for macOS, roughly ten times Linux. Six jobs where half run on macOS cost about as much as 34 Linux jobs, not six. Keep that multiplier in your head every time you add a dimension. If macOS is in your grid, also watch runner image retirements like the macOS 14 runner retirement, which break pinned labels.

Set the name field: the default label is the raw combination, which gets unreadable past two dimensions, and an explicit name makes a red X point straight at the failing combination.

include and exclude

You rarely want the full cartesian product. include adds specific combinations or extra properties, and exclude trims ones you do not care about.

YAML
# .github/workflows/test.yml (strategy block)
strategy:
  matrix:
    node: [22, 24]
    os: [ubuntu-latest, windows-latest]
    exclude:
      # we don't ship Windows builds on the older LTS line
      - os: windows-latest
        node: 22
    include:
      # canary the newest LTS line on one runner, not the whole grid
      - os: ubuntu-latest
        node: 26
        experimental: true
continue-on-error: ${{ matrix.experimental == true }}

Get the mental model right. exclude removes matching combinations from the generated product, and the docs state that all include combinations are processed after exclude. include merges extra keys into an existing combination when it can do so without overwriting an original value; otherwise it appends a brand new job. Here node: 26 matches no original combination, so it becomes one extra job. The experimental flag pairs with job-level continue-on-error, so a failure on the canary does not fail the run or cancel the others.

Reach for include when you want to cover one important edge case rather than a full extra row. Adding 26 to the node list would add two jobs. Adding it through include costs exactly one.

fail-fast: true vs false

fail-fast defaults to true: the moment any matrix job fails, GitHub cancels every in-progress and queued job in that matrix. This is the right default for pull request checks. If Node 22 is already broken, you usually do not need to spend minutes confirming that Node 24, 26, macOS and Windows are also affected. Cancel early, save the runners, fix the problem.

Set it to false when you genuinely want the full picture:

YAML
# .github/workflows/compat.yml (strategy block)
strategy:
  fail-fast: false
  matrix:
    python: ['3.12', '3.13', '3.14', '3.15']
    os: [ubuntu-latest, macos-latest]

The classic case is a compatibility sweep. With fail-fast: true one early failure hides the status of everything else, so you fix one thing, rerun, and discover the next failure. With false you get one complete report, paid for in minutes when a change is fully broken. Our rule is fail-fast: true on PRs and false on scheduled nightly compatibility runs.

max-parallel to cap concurrency

max-parallel limits how many matrix jobs run at once. It does not change how many jobs exist, only how many execute simultaneously; without it, GitHub runs as many as runner availability allows.

YAML
# .github/workflows/integration.yml (strategy block)
strategy:
  max-parallel: 2
  matrix:
    shard: [1, 2, 3, 4, 5, 6]

Use it to protect a shared resource, like a test database or a rate-limited API, or to keep one matrix from starving every other workflow under your plan's concurrency ceiling. It trims peak load, not total minutes, and costs wall-clock time.

Dynamic matrices with fromJSON

Hardcoding the matrix is fine until the list of things to test changes on its own, for example one job per package in a monorepo. Generate the matrix in one job and feed it to the next with fromJSON.

YAML
# .github/workflows/packages.yml
jobs:
  discover:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set.outputs.matrix }}
    steps:
      - uses: actions/checkout@v6
      - id: set
        run: |
          pkgs=$(ls packages | jq -R -s -c 'split("\n")[:-1]')
          echo "matrix={\"package\":$pkgs}" >> "$GITHUB_OUTPUT"

  test:
    needs: discover
    strategy:
      fail-fast: false
      matrix: ${{ fromJSON(needs.discover.outputs.matrix) }}
    runs-on: ubuntu-latest
    name: test ${{ matrix.package }}
    steps:
      - uses: actions/checkout@v6
      - run: npm test --workspace packages/${{ matrix.package }}

discover writes a JSON object to GITHUB_OUTPUT and test parses it back into a matrix. Add a diff filter so discover emits only packages the pull request touched, and you test what changed instead of everything. Guard against an empty list with an if: on the test job, so a change that touches no packages skips cleanly instead of handing the matrix nothing to expand.

Avoiding combinatorial explosion

This is the minutes trap. Each new dimension multiplies, it does not add. Four Node versions, three operating systems and two database engines is not nine jobs, it is twenty-four, and every push runs all of them. The habits that keep it sane:

LeverCuts job count?Cuts total minutes?CostUse it for
excludeYesYesNoneCombinations nobody ships
include (instead of a dimension)YesYesNoneOne edge case or canary
fail-fast: trueNoYes, on failurePartial resultsPull request checks
max-parallelNoNoSlower wall clockShared resources, concurrency caps
Dynamic fromJSONYesYesExtra discover jobMonorepos, changed-only testing
Linux-first runnersNoYes, a lotLess OS coverageEverything not OS-specific

Run the exhaustive grid on a nightly schedule and a trimmed one on pull requests, and reserve macOS and Windows for the combinations that actually behave differently there.

Matrix and caching

If your cache key omits the matrix dimension, every combination fights over one cache entry and you get wrong restores or constant misses. Put the dimension in the key.

YAML
# .github/workflows/test.yml (cache step)
      - uses: actions/cache@v6
        with:
          path: ~/.npm
          key: npm-${{ matrix.os }}-node${{ matrix.node }}-${{ hashFiles('package-lock.json') }}

actions/setup-node and similar setup actions handle this when you enable their built-in cache, but any hand-rolled cache step needs the dimension baked into the key.

Questions people ask

How many jobs can a GitHub Actions matrix run?

GitHub's workflow syntax reference caps a matrix at 256 jobs per workflow run, on both GitHub-hosted and self-hosted runners. How many run at the same time depends on runner availability and your plan's concurrency limit, and you can lower it with max-parallel. If you are anywhere near 256, the matrix is almost certainly too wide; split it or generate it dynamically.

Does fail-fast cancel jobs that use continue-on-error?

No. When a job with continue-on-error: true fails, the docs say the other jobs are not affected. Only failures in jobs where continue-on-error is false trigger fail-fast, which cancels all in-progress and queued matrix jobs. That is why the experimental-combination pattern sets continue-on-error from a matrix property: the canary can fail without killing the rest of the run.

What is the difference between include and exclude in a matrix?

exclude removes combinations from the generated product. include either adds keys to existing combinations, when it can without overwriting an original value, or appends new combinations. GitHub processes every include after exclude, so an include entry can add back a combination you excluded. Use exclude to prune and include for single edge cases.

The call we'd make

Start narrow. A basic version-plus-OS matrix with fail-fast: true covers most projects and keeps minutes predictable. Shape the grid with exclude and include before you reach for a new dimension, since each dimension multiplies your bill. Turn fail-fast off only for scheduled compatibility sweeps, use max-parallel when a shared resource or plan ceiling forces it, and graduate to a dynamic fromJSON matrix once "test everything on every push" starts costing real money. Always fold the matrix dimension into your cache keys.

For more patterns, see our GitHub Actions recipes pillar, and if runtime is your real problem, how to speed up a slow CI pipeline.

Sources

React
Share this post
Cite this article
Plain text

Kiril Urbonas (2026). GitHub Actions Matrix Builds That Don't Waste Minutes. DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/github-actions-matrix-builds

Markdown

[GitHub Actions Matrix Builds That Don't Waste Minutes](https://www.devopsness.com/blog/github-actions-matrix-builds) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)

KU

Kiril Urbonas

Founder & Chief Editor

586 articles
View all articles by Kiril Urbonas

You might have missed

Evergreen posts worth revisiting.