All guides

Matrix builds in GitHub Actions

A matrix turns one job definition into a job for every combination of the values you list. Three operating systems and four versions make twelve jobs, and GitHub rounds the time of each one up to a whole minute.

Code in an editor on a laptop screen

A basic matrix

Put the variables under strategy.matrix. GitHub runs one job for each combination and exposes the values in the matrix context.

YAML

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

Two values times two values make four jobs. Variable names are case insensitive, so OS and os are the same variable. The operating systems and versions here are a choice for this example; list your own.

Include and exclude

exclude removes combinations, and a partial match is enough to exclude one.

include takes a list of objects. GitHub adds the keys of an object to every combination where they overwrite none of the original matrix values. If the object fits no combination, it becomes a new one. include is processed after exclude, so it can add back a combination that was excluded.

This is GitHub's example with other values. It runs four jobs, and the job for windows-latest and Node.js 24 gets one more variable:

YAML

strategy:
  matrix:
    os: [windows-latest, ubuntu-latest]
    node: [22, 24]
    include:
      - os: windows-latest
        node: 24
        coverage: true

A step can test the variable with if: ${{ matrix.coverage }}. A matrix that has no variables and only include runs every listed configuration, which gives you a matrix that is not fully populated.

Failures and fail-fast

strategy.fail-fast applies to the whole matrix and defaults to true. When any job in the matrix fails, GitHub cancels all its jobs that are in progress or queued.

continue-on-error applies to a single job. GitHub's example sets it from a matrix value:

YAML

jobs:
  test:
    runs-on: ubuntu-latest
    continue-on-error: ${{ matrix.experimental }}
    strategy:
      fail-fast: true
      matrix:
        version: [6, 7, 8]
        experimental: [false]
        include:
          - version: 9
            experimental: true

The workflow starts four jobs. If a job with continue-on-error: false fails, all jobs in progress or queued are canceled. A failure of the job marked continue-on-error: true leaves the other jobs untouched.

A cap on parallel jobs

By default GitHub runs as many jobs in parallel as runner availability permits. strategy.max-parallel sets the maximum that run at the same time. GitHub's example runs at most two of its six jobs at a time, even when runners are available for all six:

YAML

strategy:
  max-parallel: 2
  matrix:
    version: [10, 12, 14]
    os: [ubuntu-latest, windows-latest]

A dynamic matrix from a job output

A matrix value can be an expression. One job writes a JSON array to an output, and a later job reads it with fromJSON.

YAML

jobs:
  list:
    runs-on: ubuntu-latest
    outputs:
      services: ${{ steps.find.outputs.services }}
    steps:
      - id: find
        run: echo 'services=["api", "web", "worker"]' >> "$GITHUB_OUTPUT"

  build:
    needs: list
    runs-on: ubuntu-latest
    strategy:
      matrix:
        service: ${{ fromJSON(needs.list.outputs.services) }}
    steps:
      - uses: actions/checkout@v7
      - run: ./build.sh "$SERVICE"
        env:
          SERVICE: ${{ matrix.service }}

The contexts reference types a job output as a string, and fromJSON returns the JSON data type for it, here an array. The three names and ./build.sh stand for your own list and your own build command.

When a matrix job sets outputs, the outputs of all its jobs are combined. Give each output a unique name. With a shared name, the last job that sets it overrides the value, and GitHub does not guarantee the order in which matrix jobs run.

The job limit and the cost

A matrix generates at most 256 jobs per workflow run. The limit applies to GitHub-hosted and self-hosted runners.

GitHub's billing and limits pages add four facts about those jobs:

  • Rounding. GitHub rounds the minutes and partial minutes of each job up to a whole minute. Twelve jobs of 20 seconds are billed as twelve minutes.
  • A fresh runner. On GitHub-hosted runners each job starts in a clean runner image and downloads its dependencies again.
  • The rate. The runner pricing page lists, per minute, 0.006 USD for Linux 2-core, 0.010 USD for Windows 2-core and 0.062 USD for macOS. One macOS minute costs as much as 10.3 Linux minutes.
  • Concurrency. On GitHub Free, 20 jobs run concurrently on standard runners, and at most 5 of them on macOS. The limits page lists the other plans.

Standard GitHub-hosted runners are free in public repositories.

Ways to keep a matrix small

  1. Multiply the values before you add a variable. Each value of a new variable adds one job per existing combination.
  2. Use include to add one configuration, such as a single macOS job, in place of a whole variable.
  3. Leave fail-fast at its default, so that a failure cancels the jobs that are still queued.
  4. Build the list in a job, as above, so that the workflow decides how many jobs run.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides