- Test on Multiple Node Versions
- Run tests on Node 18, 20, and 22 simultaneously
- See 3 jobs running in parallel
- Understand matrix strategy
- node-version: [18, 20, 22] → Creates 3 jobs
- All run in parallel (at the same time)
- Uses ${{ matrix.node-version }} to reference the current value
- Test on multiple Node versions (like we just did)
- Test on multiple OS (Ubuntu, Windows, macOS)
- Test with different dependency versions
- Test different configurations
Test your application across multiple Node.js versions simultaneously to ensure compatibility.
- Creates 3 parallel jobs (one for each Node version)
- Tests on Node.js 18, 20, and 22
- All jobs run at the same time
- Shows which versions pass/fail
name: Matrix CI
on:
push:
branches: [ master, feature/github-actions-practice ]
pull_request:
branches: [ master ]
workflow_dispatch:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18, 20, 22]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test -- --passWithNoTests --watchAll=false
- name: Build project
run: npm run build
- name: Display Node version
run: |
echo "Testing on Node.js ${{ matrix.node-version }}"
node --versionstrategy:
matrix:
node-version: [18, 20, 22]This single configuration creates 3 jobs:
- Job 1: Node.js 18
- Job 2: Node.js 20
- Job 3: Node.js 22
All run in parallel!
node-version: ${{ matrix.node-version }}- Access current matrix value using
${{ matrix.variable-name }} - Can use in step names, commands, or action inputs
- Makes workflows DRY (Don't Repeat Yourself)
Multiple Node versions:
strategy:
matrix:
node-version: [16, 18, 20, 22]Multiple Operating Systems:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}Multiple Dimensions:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [18, 20, 22]This creates 6 jobs (2 OS × 3 Node versions)!
With Include (add specific combinations):
strategy:
matrix:
node-version: [18, 20]
include:
- node-version: 22
experimental: trueWith Exclude (remove specific combinations):
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [18, 20, 22]
exclude:
- os: windows-latest
node-version: 18Answer:
- Matrix strategy allows running the same job with different configurations
- Creates multiple jobs from a single job definition
- All matrix jobs run in parallel for faster feedback
- Common use cases:
- Testing across multiple Node/Python versions
- Testing on different operating systems
- Testing with different dependency versions
- Cross-platform compatibility testing
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [16, 18, 20]Answer:
- 9 jobs (3 OS × 3 Node versions)
- ubuntu + Node 16, ubuntu + Node 18, ubuntu + Node 20
- windows + Node 16, windows + Node 18, windows + Node 20
- macos + Node 16, macos + Node 18, macos + Node 20
- All run in parallel
Answer:
- By default, other jobs continue running
- The overall workflow is marked as failed
- You can change this behavior with
fail-fast:
strategy:
fail-fast: false # Continue all jobs even if one fails
matrix:
node-version: [18, 20, 22]- With
fail-fast: true(default), remaining jobs are cancelled when one fails
Answer:
Use the expression syntax: ${{ matrix.variable-name }}
Examples:
# In step names
- name: Test on Node ${{ matrix.node-version }}
# In with parameters
with:
node-version: ${{ matrix.node-version }}
# In run commands
run: echo "Testing on ${{ matrix.os }}"Answer: Use matrix builds when you need to:
- Test compatibility across multiple versions (Node, Python, etc.)
- Ensure cross-platform compatibility (Linux, Windows, Mac)
- Test with different configurations or feature flags
- Validate against multiple dependency versions
Don't use for:
- Simple single-version projects
- When you only support one platform
- When parallel jobs would be wasteful (same test 5 times)