Home DevOpsGitHub Actions Matrix Builds: Test Every Version Fast

GitHub Actions Matrix Builds: Test Every Version Fast

by Knowledge Base Linux
GitHub Actions matrix builds testing code across multiple language versions in parallel with checkmarks

Testing against one version of a language or operating system gives false confidence: code that passes on Python 3.12 may fail on 3.11, and a tool that works on Ubuntu may break on macOS. GitHub Actions matrix builds run the same job across a grid of versions, operating systems, and other dimensions in parallel, turning one workflow into a full compatibility test without duplicating jobs by hand.

GitHub Actions matrix builds running parallel test runners on different operating systems with one failure flagged

This guide explains GitHub Actions matrix builds practically: the matrix syntax, combining dimensions, controlling failures, using includes and excludes, and keeping costs under control. If you run your own runners, see our self-hosted runners guide, and for protecting credentials in workflows, our secrets management guide.

Prerequisites

  • A GitHub repository with Actions enabled, and a workflow file under .github/workflows/.
  • A test command that runs locally, for example pytest, npm test, or go test ./....
  • Basic familiarity with YAML and the GitHub Actions workflow syntax.

GitHub Actions Matrix Builds Basics

A matrix is defined under strategy.matrix in a job. Each dimension is a list, and GitHub creates one job for every combination. Inside the job, the current combination is available as matrix.VARIABLE. This workflow tests a Python project against three Python versions:

name: Tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.11", "3.12", "3.13"]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - run: pip install -r requirements.txt
      - run: pytest

That single job definition becomes three jobs, one per Python version. Add a second dimension and the combinations multiply: three Python versions times two operating systems is six jobs. That multiplication is the power of a matrix and also the cost trap, so keep dimensions deliberate.

Combining Dimensions in GitHub Actions Matrix Builds

Combine operating systems with versions when cross-platform behaviour matters, and use the matrix value to select the right setup step. This example tests Node.js across operating systems and versions, with a conditional step for one platform:

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

Note the --omit=dev flag instead of the older production-only flag, which keeps the install step current. For finer control, use include to add a combination that is not in the grid, or to attach extra variables to one combination, and exclude to remove a combination that does not make sense:

strategy:
  matrix:
    os: [ubuntu-latest, windows-latest]
    node-version: [20, 22]
    exclude:
      - os: windows-latest
        node-version: 20
    include:
      - os: macos-latest
        node-version: 22
        experimental: true

An include entry that matches an existing combination adds variables to it; an entry that does not match creates a new combination. This is how you test an experimental version on one platform without expanding the whole grid.

Fail-Fast, Max-Parallel, and Failure Handling

By default, GitHub cancels all remaining matrix jobs when one fails. That saves minutes but hides whether other combinations also fail. For a compatibility matrix, turning fail-fast off is usually better: you learn the full failure picture in one run instead of fixing one version, re-running, and discovering the next failure:

strategy:
  fail-fast: false
  max-parallel: 4
  matrix:
    python-version: ["3.11", "3.12", "3.13"]

The max-parallel setting caps how many matrix jobs run at once, which protects shared resources like a test database or a rate-limited API. For jobs that should tolerate failure, such as an experimental version, use continue-on-error on the job or step so a red experimental run does not block merging, while still showing its result.

Keeping GitHub Actions Matrix Builds Fast and Cheap

Matrix jobs multiply cost, so control the grid. Test the full grid on pull requests and main-branch pushes, but use a smaller matrix for routine pushes, or a scheduled full run nightly. Cache dependencies per matrix combination so each job reuses its own cache: include the matrix value in the cache key, or let the setup action do it automatically.

- uses: actions/setup-python@v5
  with:
    python-version: ${{ matrix.python-version }}
    cache: pip
- run: pip install -r requirements.txt

Also avoid running the whole matrix for documentation-only changes by using path filters, and split slow suites so a matrix job tests one slice. If a matrix grows beyond about a dozen jobs, ask whether every combination is really needed: dropping an end-of-life version or a redundant operating system often saves more time than any caching optimisation.

Troubleshooting GitHub Actions Matrix Builds

The matrix runs only one job. Check the YAML indentation: matrix belongs under strategy, not under steps, and each dimension must be a list. A single string instead of a list produces one combination.

A job fails only on one combination. That is the matrix working as intended. Open that job’s logs, reproduce locally with the same version and operating system, and check for platform-specific paths, line endings, or dependency versions that differ by platform.

Jobs are cancelled after the first failure. Set fail-fast: false when you need the full picture. For pull requests, this also makes reviews easier because reviewers see every failing combination at once.

Costs spike suddenly. Count the combinations: dimensions multiply, they do not add. Review the workflow’s matrix, check for an include that accidentally added many jobs, and consider max-parallel and path filters to reduce the load.

GitHub Actions Matrix Builds Best Practices

Keep the matrix small and meaningful, name jobs with the matrix values so failures are readable at a glance, use fail-fast: false for compatibility testing, and cache per combination. Document which versions you support and why, and remove end-of-life versions promptly so the grid does not grow forever. A matrix should answer one question: does this change work everywhere we promise to support?

GitHub Actions matrix builds give you that answer in a single run, with one workflow definition instead of a dozen copy-pasted jobs. Start with one dimension, add a second only when cross-platform behaviour matters, and control the grid with includes, excludes, and fail-fast settings. For the official reference, see the GitHub Actions workflow syntax documentation.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.