Guides / Build a matrix
Build a matrix
A matrix runs one job once per combination of values, such as every Node version against every database. You write the job once and list the values on its workflow entry.
Prerequisites
Section titled “Prerequisites”- A job that should run with different values. This guide uses a Node.js test job.
-
Add a matrix to the workflow entry
Section titled “Add a matrix to the workflow entry”matrixmaps each key to a list of values:.hatchrun/config.yml jobs:test:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v5- uses: actions/setup-node@v4with:node-version: ${{ matrix.node }}- run: npm ci- run: npm testworkflows:ci:on:pull_request: {}jobs:- test:matrix:node: [20, 22, 24]The run plans three jobs:
test (20),test (22)andtest (24). Steps read the job’s value as${{ matrix.node }}. -
Add a second key
Section titled “Add a second key”Each new key multiplies the jobs. Two Node versions on two databases make four jobs:
.hatchrun/config.yml jobs:test:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v5- run: |docker run -d --name db -p 5432:5432 -e POSTGRES_PASSWORD=pw postgres:${{ matrix.postgres }}until docker exec db pg_isready -U postgres; do sleep 1; done- uses: actions/setup-node@v4with:node-version: ${{ matrix.node }}- run: npm ci- run: npm testworkflows:ci:on:pull_request: {}jobs:- test:matrix:node: [20, 22]postgres: [16, 17]The jobs are
test (20, 16),test (20, 17),test (22, 16)andtest (22, 17). Values appear in the order of their keys sorted by name, and the first key changes slowest. -
Feed a matrix key into a parameter
Section titled “Feed a matrix key into a parameter”When the job declares a parameter with the same name as a matrix key, the matrix also sets the parameter. Use this when a value must reach a command, or must keep its type:
.hatchrun/config.yml commands:setup:parameters:node: { type: integer }steps:- uses: actions/checkout@v5- uses: actions/setup-node@v4with:node-version: ${{ parameters.node }}jobs:test:runs-on: ubuntu-latestparameters:node: { type: integer }steps:- setup: { node: "${{ parameters.node }}" }- run: npm testworkflows:ci:on:pull_request: {}jobs:- test:matrix:node: [20, 22] -
Validate
Section titled “Validate”Terminal window hatchrun validate
- Every key needs at least one value.
- A value may appear only once per key.
- A key cannot also be set as a parameter value on the same entry.
- A matrix may have at most 256 combinations, and the whole workflow
at most 256 jobs. With
parallelism, each combination counts once per copy. - An entry that requires a matrix entry waits for all its jobs.
GitHub’s include and exclude have no equivalent. To add a leg,
list the job again under another name with parameter values.
To leave a combination out, split the matrix into entries that list only what you want. See Migrate: matrix include and exclude.