🎓 A1Learner
🔍
0 of 50 lessons completed

GitHub Actions: Matrix Builds

A matrix build runs the same job several times with different settings. One workflow tests Node 20, 22 and 24 on two operating systems at once.

The matrix block

This block lists three Node versions and two operating systems.

strategy:
  matrix:
    node-version: [20, 22, 24]
    os: [ubuntu-latest, windows-latest]

Read the current value

Each run reads its own combination from the matrix.

runs-on: ${{ matrix.os }}
steps:
  - uses: actions/setup-node@v7
    with:
      node-version: ${{ matrix.node-version }}

The job that runs six times

Three Node versions times two operating systems makes six jobs.

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

Example

name: Matrix

on: push

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

Key points

  • Matrix: a list of values that the same job runs once per combination.
  • strategy.matrix holds the lists of values.
  • Node 20, 22 and 24 across Ubuntu and Windows gives six runs.
  • Each combination runs as its own job.
  • One failing combination turns only that job red.
  • The ${{ }} marks read a value from the matrix.
  • Windows runs are slower than Ubuntu runs.
  • A matrix turns one job into many runs, one per combination.

In this example, the workflow runs on every push. The matrix lists three Node versions and two operating systems. GitHub runs the same job six times, once per combination. Each run checks out the code, installs its Node version, and runs the tests. All six pass.

Note

The demo app is plain JavaScript, so it should pass on every Node version.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

How many jobs does a matrix of Node 20, 22, 24 on Ubuntu and Windows run?