Matrix Builds and Parameterization
You'll learn how to use matrix strategies to run the same job with different parameters in parallel, testing multiple configurations efficiently.
What is Matrix Parameterization?
A matrix build lets you define a set of variables (like operating systems, programming language versions, or environment configurations) and automatically runs your job once for each combination. Instead of writing separate jobs for Node 14/Ubuntu, Node 16/Ubuntu, Node 14/Windows, etc., you define the matrix once and GitHub Actions generates all combinations.
Think of it like a spreadsheet: you define columns (OS, node version) and GitHub Actions fills in every row automatically, running each as a parallel job.
Why Use Matrix Builds?
Matrix strategies solve the "configuration explosion" problem. When you need to test across 3 operating systems and 4 language versions, that's 12 separate test runs. Writing 12 job definitions is tedious and error-prone. A matrix creates all 12 automatically from just a few lines of configuration.
How It Works
You define a strategy.matrix with key-value arrays. GitHub Actions creates a job instance for every combination:
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node: [14, 16, 18]
steps:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
This creates 9 parallel jobs (3 OS × 3 Node versions). You access values using ${{ matrix.key }} in your workflow.
Matrix builds leverage the parallel execution capabilities you learned earlier, automatically distributing work across available runners.
Key Takeaway: Matrix builds parameterize jobs to test multiple configurations in parallel from a single definition, eliminating repetitive workflow code and ensuring comprehensive coverage across platforms and versions.