How to Skip a Matrix Leg Conditionally in GitHub Actions
A step-level if that reads the matrix context lets specific legs no-op without removing them from the matrix.
Guard the real work with if: referencing matrix values. The leg still starts (and reports success) but the expensive steps are skipped.
Steps
- Keep the full matrix so job status stays consistent.
- Add
if:on the heavy step referencing the matrix key. - Prefer
excludewhen you want the leg to disappear entirely.
Workflow
.github/workflows/ci.yml
jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
suite: [unit, e2e]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Run e2e only on Linux
if: matrix.suite != 'e2e' || matrix.os == 'ubuntu-latest'
run: npm run test:${{ matrix.suite }}Gotchas
- A skipped step still counts the leg as successful, which can hide missing coverage.
- For a truly absent combination, use
excludeso it never spins up a runner.
Related guides
How to Remove a Matrix Combination With exclude in GitHub ActionsDrop specific combinations from a GitHub Actions matrix with strategy.matrix.exclude, pruning pairs the cross…
How to Reference Matrix Values in a Step in GitHub ActionsRead strategy.matrix values inside a GitHub Actions step through the matrix context, using them in run comman…