yamladvanced

GitHub Actions — Advanced Matrix Strategy

Complex matrix strategies with include/exclude, fail-fast control, and dynamic matrix generation.

yaml
name: Advanced Matrix CI

on: [push, pull_request]

jobs:
  test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        node: [18, 20, 22]
        exclude:
          - os: macos-latest
            node: 18
          - os: windows-latest
            node: 18
        include:
          - os: ubuntu-latest
            node: 22
            experimental: true
            coverage: true

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}
      - run: npm ci
      - run: npm test
      - name: Upload coverage
        if: matrix.coverage
        uses: actions/upload-artifact@v4
        with:
          name: coverage-${{ matrix.os }}-${{ matrix.node }}
          path: coverage/
    continue-on-error: ${{ matrix.experimental == true }}

  # Dynamic matrix from a script
  generate-matrix:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - uses: actions/checkout@v4
      - id: set-matrix
        run: |
          echo "matrix=$(ls packages/*/package.json | \
            jq -R -s '{package: split("\n") | map(select(. != "")) }')" \
            >> $GITHUB_OUTPUT

Sponsored

GitHub

Use Cases

  • Cross-platform and multi-version testing
  • Dynamic CI matrices from project structure
  • Selective platform testing with exclude rules

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.