Sub topic 8
Skipping Whole Jobs
This chapter assumes you know the layout of a workflow file (on, jobs, steps), the difference between a job and a step, and basic YAML. You should also know how a push or pull request starts a workflow run.
Job-level if
A job accepts its own if key, at the same level as runs-on and needs. When the condition is false, GitHub skips the job. It does not start a runner, and none of the job's steps run. The job appears in the run as "skipped", not "failed".
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.shOn a feature branch, test runs and deploy is skipped. On main, both run.
What a job-level condition can read
The job-level if is evaluated before the job gets a runner. Only some contexts are available at that point:
| Context | Available in job `if` |
|---|---|
| `github` | Yes |
| `needs` | Yes |
| `vars` | Yes |
| `inputs` | Yes |
| `env` | No, because job and step `env` values are not set yet |
| `steps` | No, because no step has run |
| `runner` | No, because no runner is assigned |
The most common mistake is reading steps.<id>.outputs or env in a job if. To use a step's result in a job condition, pass it out as a job output and read it through needs in the next job.
Job-level versus step-level
Use a job-level if when the whole job should be skipped. This saves runner time and keeps the run graph clear. Use a step-level if when most of the job should still run.
- A job-level
ifskips the runner start-up and all steps. - A step-level
ifstill starts the runner and runs the earlier steps. - Job conditions cannot see step outputs. Step conditions can.
Jobs that depend on skipped jobs
By default, a job with needs runs only if every job it needs succeeded. A skipped job counts as not successful. So if deploy is skipped, a job that has needs: deploy is skipped too, and so is everything further down the chain.
To run a job even when its dependencies fail or are skipped, add a status check function such as always() to its if. Then check needs.<job_id>.result yourself. The result is one of success, failure, cancelled or skipped.
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. notify still runs because always() overrides the default and needs.test.result is failure. Without always(), notify would be skipped along with deploy.
Common errors
- Reading
stepsorenvin a jobif. The workflow fails validation or the value is empty. Use job outputs andneedsinstead. - Forgetting
always()in a downstream job. The job is skipped silently when an earlier job is skipped. - Using only
needs.x.result == 'failure'. Without a status function, GitHub applies the implicitsuccess()check first, so the job is skipped anyway. - Treating a skipped job as a failure. Branch protection can treat skipped required checks as passing, so a skip is not a safe way to block a merge.
Exercise
A workflow has jobs build, deploy (needs build, with if: github.ref == 'refs/heads/main') and cleanup (needs [build, deploy], with no if). You push to a feature branch and build succeeds. What happens to cleanup, and what change makes it run?
Show answer
deploy is skipped, so cleanup is skipped too, because a job runs by default only when all its needed jobs succeeded. Add if: ${{ always() }}, or a more specific condition such as always() && needs.build.result == 'success', to cleanup.