Sub topic 11
Matrix Jobs and Status Conditions
This chapter explains why some jobs in a run fail, some are skipped, and some still run after a failure. It assumes you can read a workflow file, find a failed step on the run page, and tell a root-cause error from a follow-on error. Earlier chapters covered those skills.
How a matrix expands into jobs
A matrix is a list of values that GitHub Actions turns into several jobs. Each combination of values becomes its own job, with its own runner, its own logs, and its own result.
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm testThis workflow defines one job called test, but the run contains four jobs. On the run page, the left sidebar lists them by name, such as test (ubuntu-latest, 18) and test (windows-latest, 20). The values in parentheses are the matrix values, in the order the keys appear in the file.
When you debug a matrix run, the first question is which combinations failed. A single red job among four green ones points to something specific to that combination. Four red jobs usually point to a problem shared by all of them, such as a broken test or a missing file.
Reading the pattern of failures
The pattern of red and green jobs is a diagnostic clue before you open a single log.
| Pattern | Likely cause |
|---|---|
| Every job fails at the same step | A shared problem: workflow syntax, a missing file, a failing test, a bad secret |
| Only one operating system fails | Path separators, line endings, shell differences, or a tool missing on that runner |
| Only one language version fails | A dependency or syntax feature unsupported on that version |
| Failures appear and disappear between runs | A flaky test, a network timeout, or a race condition |
Windows runners use PowerShell by default for run steps, while Linux and macOS runners use bash. A step such as run: export FOO=1 works on Linux and fails on Windows. You can set shell: bash on a step, or in defaults.run.shell, to make the behavior consistent.
fail-fast and why jobs are cancelled
By default, a matrix has fail-fast: true. When one matrix job fails, GitHub cancels the matrix jobs that are still running or queued. Cancelled jobs show a grey icon and a message such as "The operation was canceled." That message is not an error in your code. It records that the job was stopped because a sibling failed.
This matters when you read the run page. If you see three cancelled jobs and one failed job, the failed job holds the root cause. Open that one and ignore the cancelled logs.
To let every combination finish, set fail-fast: false:
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm testWith fail-fast: false, you see the full failure pattern in one run. That is usually what you want while diagnosing, because the pattern in the table above is only visible when every job completes.
Adding, excluding, and tolerating combinations
Two keys adjust the combinations the matrix produces.
excluderemoves a combination from the matrix.includeadds a combination, or adds extra values to existing combinations.
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20]
exclude:
- os: windows-latest
node: 18
include:
- os: ubuntu-latest
node: 22
experimental: trueThis produces four jobs: the three remaining original combinations plus one new ubuntu-latest and Node 22 combination that carries an extra experimental value.
You can use that extra value with continue-on-error at the job level:
jobs:
test:
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental == true }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
node: [18, 20]
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 testFor the experimental job, a failure does not make the workflow run fail. The job still shows a red mark in its own log, so a green run can contain a failed job. Keep this in mind when you read a run that appears healthy.
Status conditions on steps and jobs
By default, a step runs only if every earlier step in the job succeeded. After a failure, the remaining steps are skipped. Status check functions change this behavior in an if: condition.
| Function | The step or job runs when |
|---|---|
| `success()` | All previous steps succeeded. This is the default when no status function is used. |
| `failure()` | A previous step or a needed job failed. |
| `always()` | Regardless of status, including when the run was cancelled. |
| `cancelled()` | The run was cancelled. |
The example below uses two of them in one job:
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm test
- name: Upload test report
if: always()
uses: actions/upload-artifact@v4
with:
name: test-report
path: reports/
- name: Show diagnostics
if: failure()
run: ls -laThe upload step runs whether the tests pass or fail, so you can still download the report from a failing run. The diagnostics step runs only after a failure.
A common mistake with if
An if: expression that does not include a status function gets an implicit success() added. This is why a cleanup step written as if: github.ref == 'refs/heads/main' is skipped after a failure, even on main. To make it run, include a status function:
- name: Cleanup
if: ${{ always() && github.ref == 'refs/heads/main' }}
run: ./cleanup.shPrefer failure() or always() over if: true, because if: true still carries the implicit success() check in the way described above.
Steps with continue-on-error
You can also set continue-on-error: true on a single step. If that step fails, the job treats it as passed and moves on to the next step.
steps:
- run: npm run lint
continue-on-error: true
- run: npm testThe lint step can fail without stopping the tests. The failed step shows a red mark in the log, and the job shows green. When you read logs from such a workflow, a visible error does not mean that step caused the job to fail. Check which step set the job's final status.
Job dependencies and skipped jobs
The needs key makes a job wait for other jobs. If a needed job fails, the dependent job is skipped by default. A skipped job shows a grey icon and the label "Skipped". It did not fail. It never started.
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm test
deploy:
needs: test
runs-on: ubuntu-latest
steps:
- run: echo "Deploying"
notify:
needs: [test, deploy]
if: failure()
runs-on: ubuntu-latest
steps:
- run: echo "Something failed"If test fails, then:
deployis skipped, because its dependency failed and it has the implicitsuccess()condition.notifyruns, becausefailure()is true when any needed job failed.
If notify had no if:, it would also be skipped. A skipped job is a signal to look upstream, not to debug the skipped job.
When a job needs a matrix job, it depends on the whole matrix. The dependent job runs only when every matrix combination succeeds, unless you use a status function in its if:.
Putting it together: a diagnosis walkthrough
Suppose a run shows these jobs:
test (ubuntu-latest, 18): successtest (ubuntu-latest, 20): successtest (windows-latest, 18): failedtest (windows-latest, 20): cancelleddeploy: skipped
Work through it in order:
- The cancelled job is a result of
fail-fast. Do not read its log. deployis skipped becausetestdid not succeed. Do not read it.- Only
test (windows-latest, 18)holds a root cause. Open it and find the first error. - Both Ubuntu jobs passed, so the cause is probably Windows-specific. Suspect the shell, paths, or line endings.
- To confirm, set
fail-fast: falseand re-run. Ifwindows-latest, 20also fails, the cause is OS-specific rather than version-specific.
Exercise
A run contains build (ubuntu-latest), which failed, build (macos-latest), which was cancelled, and package, which declares needs: build and shows as skipped. Which job's log should you read first, and what single change to the matrix would let build (macos-latest) finish so you can compare results?
Show answer
Read the log of build (ubuntu-latest), because it is the only job that actually failed. The cancelled job was stopped by fail-fast, and package was skipped because its dependency did not succeed. Setting strategy.fail-fast: false lets build (macos-latest) run to completion.
Exercise
A cleanup step has if: github.ref == 'refs/heads/main'. The tests fail on main, and the cleanup step is skipped. Why, and how do you fix it?
Show answer
An if: condition without a status check function gets an implicit success(), so the step is skipped once an earlier step has failed. Change the condition to if: ${{ always() && github.ref == 'refs/heads/main' }}.