Sub topic 11
Matrix Jobs and Status Conditions
This chapter assumes you can read a workflow file, know the difference between a job and a step, and can open a failed run in the Actions tab. It explains why a run can show several failed, cancelled, or skipped jobs, and how to read that pattern.
How a matrix expands into jobs
A matrix lets one job definition run several times with different values. Each combination becomes its own job with its own log. The job name includes the matrix values, for example test (18) and test (20).
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node: [18, 20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm testThis produces three jobs. When one is red, open that job and read its log as usual. Then open the others and compare. If only test (18) fails, the cause is probably tied to that version. If all three fail with the same error, the cause is in the code or the workflow, not in the matrix value.
Why other matrix jobs get cancelled
By default, fail-fast is true. When one matrix job fails, GitHub cancels the matrix jobs that are still running or queued. Those jobs show as cancelled, not failed. Their logs often end abruptly with a message about the run being cancelled.
The cancelled jobs are not the problem. Find the job with the red X and read its log. Cancelled jobs may hide real failures, though. A version that would have failed never finished. To see every result, set fail-fast: false.
strategy:
fail-fast: false
matrix:
node: [18, 20, 22]Status conditions
Each job and step has an implicit condition, success(). A step runs only if all earlier steps succeeded. A job runs only if every job it lists in needs succeeded. When the condition is false, GitHub marks the step or job as skipped.
You can change this with an if: expression and one of the status functions.
| Function | True when |
|---|---|
| `success()` | All previous steps (or needed jobs) succeeded. This is the default. |
| `failure()` | Any previous step (or needed job) failed. |
| `always()` | Always, even if the run was cancelled. |
| `cancelled()` | The run was cancelled. |
A skipped job is therefore usually a symptom. Look at its needs list and its if: line, then find the upstream job that failed.
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: npm test
report:
needs: test
if: always()
runs-on: ubuntu-latest
steps:
- run: echo "Test job result is ${{ needs.test.result }}"Here report runs whether test passes or fails. The needs.test.result value is one of success, failure, cancelled, or skipped.
continue-on-error
Setting continue-on-error: true on a step or job stops its failure from failing the workflow. The step still shows an error in the log, but the run can stay green. This can hide real problems. If a run is green but a step log contains an error, check for this setting.
On a matrix, continue-on-error: ${{ matrix.experimental }} is a common way to allow certain combinations to fail without blocking the rest.
Reading the pattern
Use this order when you see a mix of statuses:
- Find the job marked failed. Ignore cancelled and skipped jobs at first.
- Read that job's log to its first real error.
- If several jobs failed, compare the logs to see what they share.
- If the run is green but something looks wrong, check for
continue-on-error.
Exercise
A run shows test (18) failed, test (20) cancelled, and deploy skipped. Which job's log do you read first, and why were the other two not run to completion?
Show answer
Read test (18), the only failed job. test (20) was cancelled because fail-fast defaults to true, so GitHub cancelled it after the first failure. deploy was skipped because it needs test, and the default success() condition was false.
Exercise
You want a cleanup step to run only when an earlier step in the same job failed, not when the run is cancelled. Which condition do you use?
Show answer
if: failure(). It is true only when a previous step failed. always() would also run after cancellation.