Matrix generators
A matrix generator lets you define a set of parameters and automatically run jobs across every combination of those parameters in parallel. This achieves better resource utilization and faster job execution in CI pipelines — without duplicating job definitions.
GitHub Actions example
name: Matrix Generator Example
on:
push:
branches:
- main
jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
node_version: [12, 14, 16]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node_version }}
- name: Install dependencies
run: npm install
- name: Build and test
run: npm run build && npm test
The matrix is defined under strategy. It generates every combination of
os and node_version — 3 OS × 3 versions = 9 parallel jobs.
GitLab example
stages:
- build
demo:
stage: build
parallel:
matrix:
- names: ["test1", "test2"]
script:
- echo "hello ${names}"
GitLab’s parallel: matrix generates one job per parameter value. In this
case, it creates demo:[test1] and demo:[test2] as separate parallel
jobs, each with the correct variable injected.
Use cases
Matrix generators are especially useful for eliminating repetitive job definitions. For example, if you have four jobs deploying to four different clusters, a matrix generator lets you define the logic once and parameterize the cluster name — reducing your pipeline config significantly.