Sub topic 8
Adding and removing combinations with include and exclude
This chapter assumes you can read a workflow file, know how strategy.matrix expands variables into jobs, and have used ${{ matrix.* }} expressions in a job. Here you will learn how exclude removes combinations, how include adds variables or whole jobs, and the order in which GitHub Actions applies them.
Why a plain matrix is not enough
A matrix is a cross product. With two operating systems and three Node.js versions, you get six jobs. Real projects rarely want every combination. Node.js 18 may not be supported on Windows for your project. One experimental version may be allowed to fail. One extra configuration, such as macOS on a single Node.js version, may be worth testing without paying for the full macOS column.
Two keys inside matrix cover these cases:
excluderemoves combinations from the cross product.includeadds extra variables to existing combinations, or adds new combinations.
Both keys sit next to your own variable names, so avoid using include or exclude as variable names.
The processing order
GitHub Actions builds the final job list in three stages.
The order has one important consequence. exclude only sees the cross product of the original variables. It cannot remove a job that an include entry creates, because includes are processed afterward.
Removing combinations with exclude
Each exclude entry is a partial description of a combination. A job is removed when every key in the entry matches the job's value for that key. Keys you leave out match anything.
name: Exclude example
on: [push]
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20, 22]
exclude:
- os: windows-latest
node: 18
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: node --versionThe cross product has six jobs. The entry removes only windows-latest with Node.js 18, so five jobs remain.
An entry with a single key removes more. This entry removes every Windows job, which is three of the six:
exclude:
- os: windows-latestTwo details cause most exclude bugs:
- A key that does not exist in the matrix never matches. Writing
nod: 18instead ofnode: 18removes nothing, and no error is reported. After changing anexclude, check the job list in the run summary. - Values are compared as written. Keep the type consistent with how you defined the variable. If the matrix has
node: [18, 20, 22]as numbers, writenode: 18, notnode: "18".
Adding to combinations with include
An include entry can do two different things. Which one happens depends on whether the entry conflicts with the original matrix values.
For each include entry, in order, GitHub Actions checks every job that exists so far:
- If the entry can be added to the job without overwriting any value from the original matrix variables, the entry's keys are added to that job. This happens for every job that qualifies, not just the first.
- If the entry could not be added to any job, it becomes a new job of its own.
Here is a workflow that uses both outcomes.
name: Include example
on: [push]
jobs:
test:
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental == true }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20, 22]
exclude:
- os: windows-latest
node: 18
include:
- os: ubuntu-latest
node: 22
experimental: true
- os: macos-latest
node: 20
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: node --versionWalk through it:
- The cross product gives six jobs, and
excludeleaves five. - The first
includeentry hasos: ubuntu-latestandnode: 22. Those match the existingubuntu-latest/ 22 job, so nothing is overwritten. The job gainsexperimental: true. The job count stays at five. - The second entry has
os: macos-latest. Every existing job has a differentos, and using this entry would overwrite it. So the entry becomes a new job, and the total is six.
The continue-on-error line reads matrix.experimental. For jobs without that variable, the expression evaluates to an empty value, so the comparison with true is false and failures still fail the run. Comparing with == true is safer than using the bare variable.
What different include entries do
| Include entry | Result with the matrix above |
|---|---|
| `{os: ubuntu-latest, node: 22, experimental: true}` | Adds `experimental` to the existing ubuntu/22 job. No new job. |
| `{node: 22, coverage: true}` | Adds `coverage` to every job whose `node` is 22, here ubuntu/22 and windows/22. |
| `{experimental: false}` | Adds `experimental` to every job, because it names no original key. |
| `{os: macos-latest, node: 20}` | Creates a new job, because it would overwrite `os` everywhere. |
| `{os: ubuntu-latest, node: 23}` | Creates a new job. Node.js 23 is not in the original list, so it conflicts with every job's `node`. |
An entry with only new keys is added to all jobs. That is a handy way to attach a shared variable, but you can usually set it in env instead.
Added values can be overwritten, original values cannot
The no-overwrite rule applies only to the original matrix variables. Keys introduced by earlier include entries can be overwritten by later ones. In this example, the second entry changes the label that the first entry set:
strategy:
matrix:
node: [20, 22]
include:
- node: 22
label: stable
- node: 22
label: candidateBoth entries match the node 22 job, and the later one wins. The node 22 job ends up with label: candidate. Because include order matters, put general entries first and specific ones after them.
Building a matrix from include alone
If a matrix has no original variables and only include, each entry becomes its own job. This is a clean way to list a few unrelated configurations, each with different variables:
name: Explicit jobs
on: [push]
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- os: ubuntu-latest
node: 20
script: test
- os: windows-latest
node: 22
script: test:smoke
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm run ${{ matrix.script }}This produces exactly two jobs. Nothing is crossed.
Re-adding an excluded combination
Since include runs after exclude, you can exclude a combination and then add it back with extra data. The combination no longer exists when the include is processed, and the entry cannot merge into another job without overwriting an original value. So it becomes a new job.
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20]
exclude:
- os: windows-latest
node: 18
include:
- os: windows-latest
node: 18
experimental: trueThe result is four jobs. The Windows / 18 job is back, now marked experimental. This pattern is useful when you want a combination to stay visible but not block the run.
Limits and checks
- A matrix can produce at most 256 jobs per workflow run. Because
includecan add jobs, count after bothexcludeandincludeare applied. - Each matrix job appears in the run summary with its variable values in the name. Use that list to confirm your rules did what you expected.
- Jobs created by
includeget the same step definitions as the rest. They differ only in their variable values. Anything you want to vary, such asexperimentalor a script name, must be read through${{ matrix.* }}.
Exercises
Exercise
A matrix has os: [ubuntu-latest, windows-latest] and node: [18, 20, 22]. It also has exclude: [{os: windows-latest}] and include: [{os: windows-latest, node: 20}]. How many jobs run, and which are they?
Show answer
Four jobs. The cross product has six jobs. The exclude removes all three Windows jobs, leaving ubuntu with 18, 20, and 22. The include runs afterward. No remaining job has os: windows-latest, so the entry cannot merge into one without overwriting os. It becomes a new job: windows-latest with Node.js 20.
Exercise
You want only the Node.js 22 jobs to receive a variable coverage: true, without creating any new jobs. Which include entry does this, and why does it not create a job?
Show answer
include: [{node: 22, coverage: true}]. The entry's only original key is node: 22, which matches the node 22 jobs. Adding coverage overwrites no original value, so it is merged into every job where node is 22, and no new job is created.
Exercise
A teammate writes exclude: [{operating-system: windows-latest}], but the matrix variable is named os. What happens?
Show answer
Nothing is removed and no error is reported. A key that is not a matrix variable never matches, so all Windows jobs still run. The fix is to use the real variable name, os, and to confirm the job list in the run summary.