Script Valley
CI/CD with GitHub Actions
Docker and Container WorkflowsLesson 3.5

How to use matrix strategy to test across multiple environments

matrix strategy, matrix variables, node-version matrix, os matrix, matrix include/exclude, matrix expansion, parallel matrix jobs, fail-fast

What is Matrix Strategy?

Matrix strategy job expansion grid

Matrix strategy runs a job multiple times with different variable combinations. Instead of duplicating job definitions, you define the variables and GitHub generates one job per combination.

Node Version Matrix

jobs:
  test:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        node-version: ['18', '20', '22']
        os: [ubuntu-latest, windows-latest]
      fail-fast: false
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci
      - run: npm test

This creates 6 parallel jobs (3 Node versions × 2 OS). fail-fast: false allows all combinations to complete even if one fails — useful for compatibility testing where you want to see all results, not just the first failure.

Excluding Combinations

strategy:
  matrix:
    include:
      - node-version: '22'
        os: ubuntu-latest
        experimental: true
    exclude:
      - node-version: '18'
        os: windows-latest

exclude removes specific combinations. include adds extra variables or new combinations not generated by the base matrix.