Sub topic 9
How skipped jobs affect dependent jobs
This chapter assumes you can read a workflow file and know that needs makes one job wait for another. It also assumes you know that a job-level if can skip a whole job. Here you will see what happens to the jobs downstream of a skipped job.
The default: skipped means skipped
When a job lists another job in needs, it runs only if every needed job succeeded. A skipped job does not count as a success. So a job that depends on a skipped job is skipped too, and so is everything that depends on that job.
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: ./run-tests.sh
deploy:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- run: ./deploy.sh
announce:
needs: deploy
runs-on: ubuntu-latest
steps:
- run: echo "Deployed"On a feature branch, deploy is skipped because its if is false. announce has no if, but it is skipped as well because deploy did not succeed. The skip spreads down the chain.
Reading the result with needs
Each needed job exposes its outcome as needs.<job_id>.result. The value is one of success, failure, cancelled or skipped. You can compare it in an if, but the default skip rule still applies first. A job whose needs did not all succeed is skipped before your condition is even considered, unless the condition contains a status check function.
Running a job anyway
Add always(), failure() or cancelled() to the job's if to turn off the implicit success check. You then decide yourself which results are acceptable.
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: ./run-tests.sh
deploy:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- run: ./deploy.sh
notify:
needs: [test, deploy]
if: ${{ always() && needs.test.result == 'failure' }}
runs-on: ubuntu-latest
steps:
- run: echo "Tests failed"If test fails, deploy is skipped. Without always(), notify would be skipped too. With it, notify runs, and the second half of the condition limits it to a failed test.
Running after an optional job
Sometimes a job is skipped on purpose and a later job should still run. Accept both outcomes explicitly:
report:
needs: [test, deploy]
if: ${{ always() && needs.test.result == 'success' && (needs.deploy.result == 'success' || needs.deploy.result == 'skipped') }}
runs-on: ubuntu-latest
steps:
- run: echo "Pipeline finished cleanly"Here report runs when test passed and deploy either ran successfully or was skipped, and it does not run if deploy failed.
Common mistakes
- Using
if: needs.test.result == 'failure'withoutalways()orfailure(). The job is skipped before the condition matters. - Writing
if: always()alone and then publishing.always()also runs after failures and cancellations, so it is a poor guard for a deploy. - Forgetting that skipping is transitive. If a middle job is skipped, a job two steps downstream is skipped too, even if you listed only the first job in its
needs.
Exercise
A workflow has jobs build, deploy (needs build, runs only on main) and cleanup (needs deploy, no if). On a pull request, what happens to cleanup, and what change makes it run whenever build finished, whether or not deploy was skipped?
Show answer
deploy is skipped because its if is false, so cleanup is skipped too. To make it run, list both jobs in needs: [build, deploy] and add if: ${{ always() && needs.build.result == 'success' }}. The always() call turns off the implicit success requirement on the skipped deploy.