Sub topic 5
What happens downstream when a job fails or is skipped
This chapter assumes you can read a workflow file and know the on, jobs, runs-on, steps, and needs keys. It covers what GitHub Actions does to downstream jobs when an upstream job does not succeed.
The default rule
A job with needs runs only if every job it needs finished with the result success. If any needed job failed, was skipped, or was cancelled, the dependent job is skipped. GitHub does not run it and does not mark it as failed.
The rule is implicit. Without an if key, GitHub treats the dependent job as if it had if: success(). The success() function here checks the jobs listed in needs, not the whole workflow.
Take the pipeline from the fan-out and fan-in chapter, where lint and test run in parallel, build needs both, and deploy needs build.
lint and test start together. If test fails, lint is not stopped and finishes on its own. build is skipped because one of its two needs did not succeed. deploy is skipped because build was skipped, so the skip spreads down the chain.
The four job results
Every job ends with one of four results. Later chapters use these values in if expressions.
| Result | Meaning |
|---|---|
| `success` | All steps passed, or the remaining steps were allowed to fail. |
| `failure` | A step failed and the job stopped. |
| `cancelled` | The run was cancelled, or a concurrency rule cancelled the job. |
| `skipped` | The job did not run, because of its `if` condition or an upstream result. |
A skipped job is not a failed job. The workflow run as a whole is reported as failed only when a job fails. A run in which test fails is therefore marked failed, with build and deploy shown as skipped.
Skips spread, failures do not
A failure in one job does not turn downstream jobs red. They are skipped. This matters when you read the run summary: the job to investigate is the one marked failed, and the skipped jobs after it only tell you what was prevented from running.
Jobs that are not connected to the failing job keep running. If you add a docs job with no needs, it runs to completion even when test fails, because nothing links it to the failure.
A job that is skipped by its own condition
A skip caused by the job's own if also affects dependents. Suppose integration runs only on the main branch:
name: CI
on: [push]
jobs:
integration:
if: ${{ github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
steps:
- run: echo "Running integration tests"
package:
needs: integration
runs-on: ubuntu-latest
steps:
- run: echo "Packaging"On a feature branch, integration is skipped by its condition. Because a skipped job is not a success, package is skipped too, even though nothing failed. This surprises many people. The usual fix is to give package an if that accepts a skipped dependency, as the next chapters on status functions show.
Checking what a dependent job sees
A dependent job can read the result of each job it needs through the needs context. This step prints the result of every needed job:
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: exit 1
report:
needs: build
if: ${{ always() }}
runs-on: ubuntu-latest
steps:
- run: echo "build result was ${{ needs.build.result }}"The always() condition makes report run even though build failed, and needs.build.result evaluates to failure. Without the if, report would be skipped and you would never see the value.
Common mistakes
- Expecting a failed
testjob to stoplint. Parallel jobs are independent, solintkeeps running. - Expecting dependent jobs to show as failed. They show as skipped.
- Forgetting that an optional job, one with its own
if, can silently skip everything that needs it. - Reading the first skipped job as the cause. Look upstream for the first job marked failed or cancelled.
Exercise
A workflow has jobs a, b, and c. Job c has needs: [a, b]. Job a succeeds, and job b is skipped because of its own if condition. What happens to c, and is the run marked failed?
Show answer
Job c is skipped, because the default rule requires every needed job to have the result success and a skipped job does not count. The run is not marked failed, because no job failed.