TExcelTAD
Home › Release Engineering & CI/CD › Matrix Builds

Matrix Builds

A release usually has to build and test the same code on several operating systems, CPU architectures and runtime versions. A matrix writes that once: strategy.matrix lists the axes, and GitHub Actions creates one job per combination (at most 256 per workflow run).

The basics

yaml
jobs:
  test:
    strategy:
      fail-fast: false               # default true: one failure cancels the rest
      max-parallel: 4                # default: as many as runners allow
      matrix:
        os: [ubuntu-24.04, windows-2025, macos-15]
        node: [22, 24]
        exclude:
          - os: macos-15
            node: 22
        include:
          - os: ubuntu-24.04
            node: 25
            experimental: true
    runs-on: ${{ matrix.os }}
    continue-on-error: ${{ matrix.experimental == true }}
    name: test (${{ matrix.os }}, node ${{ matrix.node }})
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: ${{ matrix.node }}
      - run: npm ci && npm test
Setting Effect
fail-fast true (default) cancels in-progress and queued matrix jobs when one fails. Set false for test matrices: you want to see every failing platform, not only the first.
max-parallel caps concurrent jobs (to protect a shared test database or a licence server)
continue-on-error (job level) the run stays green even if this job fails: the usual way to try a new runtime version without blocking anyone
exclude removes combinations; applied before include
include adds variables to existing combinations, or adds whole new combinations (rules below)

How include works

include is the part people get wrong. For each object in include, GitHub tries to add its key/value pairs to every original combination where doing so wouldn't overwrite one of the matrix's original values (values that came from an axis). Keys that were added by an earlier include can be overwritten. If an object can't be added to any combination, it becomes a new combination of its own. This model implements those rules and runs the example from the GitHub documentation:

python · try it yourself
from itertools import product

def expand(matrix):
    include = matrix.get("include", [])
    exclude = matrix.get("exclude", [])
    axes = {k: v for k, v in matrix.items() if k not in ("include", "exclude")}
    combos = [dict(zip(axes, values)) for values in product(*axes.values())]
    combos = [c for c in combos if not any(all(c.get(k) == v for k, v in e.items()) for e in exclude)]
    originals = [dict(c) for c in combos]          # remember which values came from the axes
    extra = []
    for inc in include:
        added = False
        for combo, original in zip(combos, originals):
            # may not overwrite an original value; added values may be overwritten
            if all(k not in original or original[k] == v for k, v in inc.items()):
                combo.update(inc)
                added = True
        if not added:
            extra.append(dict(inc))
    return combos + extra

docs_example = {
    "fruit": ["apple", "pear"],
    "animal": ["cat", "dog"],
    "include": [
        {"color": "green"},
        {"color": "pink", "animal": "cat"},
        {"fruit": "apple", "shape": "circle"},
        {"fruit": "banana"},
        {"fruit": "banana", "animal": "cat"},
    ],
}
for job in expand(docs_example):
    print(job)

print()
release = {
    "target": ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "x86_64-pc-windows-msvc", "aarch64-apple-darwin"],
    "include": [
        {"target": "x86_64-unknown-linux-gnu", "runner": "ubuntu-24.04", "archive": "tar.gz"},
        {"target": "aarch64-unknown-linux-gnu", "runner": "ubuntu-24.04-arm", "archive": "tar.gz"},
        {"target": "x86_64-pc-windows-msvc", "runner": "windows-2025", "archive": "zip"},
        {"target": "aarch64-apple-darwin", "runner": "macos-15", "archive": "zip"},
    ],
}
for job in expand(release):
    print(f"{job['runner']:<17} builds tad-1.4.2-{job['target']}.{job['archive']}")
{'fruit': 'apple', 'animal': 'cat', 'color': 'pink', 'shape': 'circle'}
{'fruit': 'apple', 'animal': 'dog', 'color': 'green', 'shape': 'circle'}
{'fruit': 'pear', 'animal': 'cat', 'color': 'pink'}
{'fruit': 'pear', 'animal': 'dog', 'color': 'green'}
{'fruit': 'banana'}
{'fruit': 'banana', 'animal': 'cat'}

ubuntu-24.04      builds tad-1.4.2-x86_64-unknown-linux-gnu.tar.gz
ubuntu-24.04-arm  builds tad-1.4.2-aarch64-unknown-linux-gnu.tar.gz
windows-2025      builds tad-1.4.2-x86_64-pc-windows-msvc.zip
macos-15          builds tad-1.4.2-aarch64-apple-darwin.zip

The second example is the standard release matrix pattern: one axis (target), and include entries that attach the runner and archive format to each target. Because each include matches exactly one original combination, it adds information without creating new jobs.

Native runners vs cross-compiling

Approach Example Trade-off
native runner per platform ubuntu-24.04-arm, macos-15 (Apple Silicon), windows-2025 simplest, tests run on the real platform; macOS minutes cost more
cross-compile on Linux Rust --target aarch64-unknown-linux-gnu, Go GOOS/GOARCH, Zig as a C cross-compiler cheap and fast; you can't run the tests for the other platform (or need QEMU)
containers with QEMU docker buildx build --platform linux/amd64,linux/arm64 one command for multi-arch images; emulated builds are slow

Cross-compilation basics are in Cross-Compilation; the Rust and Go courses show their own toolchains (Cargo, Go Modules).

Dynamic matrices

A matrix can come from a previous job's output, for example "only the packages that changed in this monorepo":

yaml
jobs:
  plan:
    runs-on: ubuntu-24.04
    outputs:
      packages: ${{ steps.changed.outputs.packages }}
    steps:
      - uses: actions/checkout@v7
        with:
          fetch-depth: 0
      - id: changed
        run: echo "packages=$(./scripts/changed-packages.sh | jq -R . | jq -sc .)" >> "$GITHUB_OUTPUT"
  build:
    needs: plan
    if: ${{ needs.plan.outputs.packages != '[]' }}
    strategy:
      matrix:
        package: ${{ fromJSON(needs.plan.outputs.packages) }}
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v7
      - run: ./scripts/build.sh "${{ matrix.package }}"

The if: guard matters: a matrix with an empty list is an error, not "zero jobs".

Test yourself

Why set fail-fast: false on a test matrix?

  1. To make jobs faster
  2. So one failing platform doesn't cancel the others and you see every failure
  3. To retry failed jobs
  4. It is required for include

An include entry {os: macos-15, node: 20} matches no existing combination. What happens?

  1. It is ignored
  2. It is added as a new combination
  3. The workflow fails
  4. It overwrites the first combination

Which order does GitHub apply matrix modifiers in?

  1. include, then exclude
  2. exclude, then include
  3. they are mutually exclusive
  4. random