Sub topic 11
What happens to jobs that depend on skipped jobs
This chapter assumes you can write a change-detection job with outputs and gate jobs with if, as covered earlier. It explains what GitHub Actions does with jobs downstream of a skipped job, because the default behavior surprises many people.
The default: skips propagate
A job has one of four results: success, failure, cancelled, or skipped. A job whose if evaluates to false gets skipped.
A job that lists a skipped job in needs is skipped too. GitHub adds an implicit success() check to every job that has no status function in its own if. A skipped dependency is not a success, so the check fails. The skip then passes down the whole chain.
Seeing the results
Each entry in the needs context has a result field. You can read it directly, which makes it easy to see what happened.
name: needs-demo
on:
pull_request:
jobs:
always-skipped:
if: false
runs-on: ubuntu-latest
steps:
- run: echo "never runs"
report:
needs: always-skipped
if: ${{ always() }}
runs-on: ubuntu-latest
steps:
- run: echo "result is ${{ needs.always-skipped.result }}"The report job prints result is skipped. Without if: ${{ always() }}, report would be skipped as well.
Running despite a skip
To run a job when some dependencies were skipped, give it an if that contains a status function. Once you write your own status function, the implicit success() is gone, so you must say what you want.
deploy:
needs: [backend-test, frontend-test]
if: ${{ !cancelled() && !contains(needs.*.result, 'failure') }}
runs-on: ubuntu-latest
steps:
- run: echo "deploying"This job runs when every dependency succeeded or was skipped. It does not run when any dependency failed, or when the workflow run was cancelled.
If you use always() instead of !cancelled(), the job also runs after the run is cancelled. That is rarely what you want for deploy steps.
Which condition to choose
| Goal | Condition |
|---|---|
| Run only if all needed jobs succeeded | no `if`, or `success()` |
| Run if none failed, skips allowed | `!cancelled() && !contains(needs.*.result, 'failure')` |
| Run no matter what, for example reporting | `always()` |
| Run only after a failure | `failure()` |
Common mistakes
- Writing
if: needs.changes.outputs.backend == 'true'on a deploy job and expecting it to run when only the frontend changed. It runs only when the backend changed. - Using only
always()and so running deploy after an upstream failure. - Forgetting that skips cascade through
needschains. A job two steps downstream is skipped even if its direct dependency ran.
Exercise
Job lint is skipped because its if is false. Job package has needs: lint and no if. Job notify has needs: package and if: ${{ always() }}. What is the result of each job, and what does needs.package.result show inside notify?
Show answer
lint is skipped. package is skipped, because the implicit success() check fails on a skipped dependency. notify runs, because always() replaces the implicit check, and needs.package.result is skipped.