Sub topic 8
Adding and removing combinations with include and exclude
This chapter assumes you can read a workflow file and know that strategy.matrix expands one job into several. If not, review the previous chapter on build matrices first.
A plain matrix builds every combination of its lists. Sometimes that is too many jobs, or you need one job with an extra setting. The exclude and include keys adjust the expanded list.
Starting point
This matrix has two operating systems and three Node.js versions, so it expands into six jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20, 22]Removing combinations with exclude
exclude is a list of partial combinations. GitHub removes every job whose values match all the keys in an entry. The entry does not need to name every matrix variable.
exclude:
- os: windows-latest
node: 18This removes one job. If the entry were only os: windows-latest, it would remove all three Windows jobs.
Adding data or jobs with include
include is also a list of entries. For each entry, GitHub checks the original matrix combinations. If the entry's keys do not overwrite any original value, GitHub adds the entry's keys to the matching combinations. If no combination matches, the entry becomes a new job.
include:
- os: ubuntu-latest
node: 22
experimental: trueHere os and node match the existing Ubuntu and Node.js 22 job. The job gains an experimental variable and stays one job. An entry such as os: macos-latest with node: 20 matches nothing, so it adds a seventh job.
Order of processing
GitHub applies all exclude entries first, then all include entries. An include entry can therefore bring back a combination you excluded, because it is evaluated against the matrix after removals.
A complete example
This workflow uses the extra variable to let the Node.js 22 job fail without failing the run.
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental == true }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20, 22]
exclude:
- os: windows-latest
node: 18
include:
- os: ubuntu-latest
node: 22
experimental: true
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm testThe matrix produces five jobs: six minus the excluded Windows and Node.js 18 job. Only Ubuntu with Node.js 22 has matrix.experimental set. In the other jobs the variable is empty, so == true is false.
Common errors
- An include entry that overwrites a value. If
node: 20is already in the lists and you write an include entry withnode: 21, the entry cannot attach to any combination. It becomes a new job. - Wrong type in an exclude entry.
node: "18"(a string) may not match18(a number) the way you expect. Use the same type as in the matrix list. - Expecting an exclude to remove an included job. Excludes run first, so they never remove jobs created by
include. - Referring to an include-only variable in every job. Check for it with a comparison such as
matrix.experimental == true, as shown above.
Exercise
A matrix has os: [ubuntu-latest, windows-latest] and node: [18, 20, 22]. It has an exclude entry with only os: windows-latest, and an include entry with os: macos-latest and node: 20. How many jobs run?
Show answer
Four. The exclude entry matches all three Windows jobs, which leaves three Ubuntu jobs. The include entry matches no remaining combination because macos-latest is not in the os list, so it adds one new job. That gives 3 + 1 = 4.