Git
matrix
Learn multi-version testing with the matrix strategy.
What is the matrix strategy?
In GitHub Actions, strategy.matrix allows you to run the same job across multiple variable combinations in parallel. For example, instead of defining separate jobs to test on Node.js 18 and 20, you define a single matrix. GitHub Actions automatically spins up an independent virtual runner for each version.
strategy:
matrix:
node-version: [18, 20, 22]
This lets you test multi-platform and multi-version support with minimal code duplication.
Multi-dimensional matrix and fail-fast
When multiple variables are defined in a matrix, all combinations are executed via cartesian product. For example, specifying 2 operating systems and 3 language versions creates 6 parallel jobs. Setting fail-fast: false ensures other matrix jobs continue running even if one fails.
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20]
This allows you to see test results across all platforms even if one fails.
Exercise Create matrix configuration
Add the matrix: definition with node-version: [18, 20] under the strategy block in the workflow.
Show example solution
cat << 'EOF' > .github/workflows/ci.yml
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18, 20]
EOF
cat .github/workflows/ci.yml
Test yourself
-
What is the primary purpose of using strategy.matrix in GitHub Actions workflows?
- Running the same job in parallel across different versions or operating systems
- Summarizing the entire commit history of the repo in one line
- Encrypting secret API keys to store them in the repository
- Automatically resolving branching merge conflicts
Show answer
Running the same job in parallel across different versions or operating systems — matrix executes the job in parallel and independently for each item in the defined variable lists.
-
Define matrix strategy
strategy:
____:
node: [18, 20]
Show answer
strategy:
matrix:
node: [18, 20]
-
Matrix concepts
- strategy.matrix
- Multi-version and platform matrix
- fail-fast: false
- Keeps other jobs running on failure
- Kartezyen çarpım
- Pairs all matrix variables together
- matrix.<degisken>
- Reads matrix value inside a step
Next lesson: cache