Sub topic 8
Running jobs after failures with if and always()
This chapter assumes you can read a workflow file and already know how needs orders jobs. You should also know that each job runs on its own runner.
The default: skipped when a needed job fails
By default, a job runs only when every job in its needs list succeeded. If any of them failed, was cancelled, or was skipped, the dependent job is skipped. Jobs further down the chain are skipped too, because their own needed job did not succeed.
This is usually what you want for build and deploy. It is not what you want for a notification or a cleanup job, which must run precisely when something went wrong.
Overriding the default with if
The if key on a job is evaluated before the job starts. If you give a job an if that contains a status check function, GitHub no longer applies the implicit "all needed jobs succeeded" rule. The job runs whenever your expression is true.
| Function | True when |
|---|---|
| `success()` | All needed jobs succeeded. This is the implicit default. |
| `failure()` | At least one needed job failed. |
| `cancelled()` | The workflow run was cancelled. |
| `always()` | Always, even after a cancellation. |
A status function must appear in the expression for this override to happen. An if that only checks something else, such as a branch name, does not remove the implicit success requirement. The job is still skipped after a failure.
Checking individual results with needs.<job>.result
Inside if, the needs context holds the outcome of each needed job. The result value is one of success, failure, cancelled, or skipped. The expression needs.*.result gives you the results of all needed jobs as a list, and contains() searches it.
The next workflow has a build that fails on purpose. The notify job runs anyway, but only when some needed job failed.
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: exit 1
test:
runs-on: ubuntu-latest
steps:
- run: echo "tests ok"
notify:
needs: [build, test]
if: ${{ always() && contains(needs.*.result, 'failure') }}
runs-on: ubuntu-latest
steps:
- run: echo "A required job failed"Without the if, notify would be skipped because build failed. With always(), it runs after both jobs finish, whatever their outcome. The contains check limits it to runs where at least one result was failure. If both jobs pass, notify is skipped.
Choosing between always() and failure()
Both always() and failure() bypass the implicit success rule. They differ in two cases.
failure()is false when the run was cancelled or when all needed jobs succeeded. It suits alerts that should fire only on errors.always()is true even when someone cancels the run. It suits cleanup that must happen regardless, such as deleting temporary cloud resources.
Prefer failure() for notifications. A cancelled run is a deliberate action, and most teams do not want an alert for it.
Tolerating a skipped job
A job in needs can be skipped because its own if was false. The dependent job is then skipped as well. To continue anyway, accept both outcomes:
jobs:
lint:
if: ${{ github.event_name == 'push' }}
runs-on: ubuntu-latest
steps:
- run: echo "linting"
build:
needs: lint
if: ${{ !failure() && !cancelled() }}
runs-on: ubuntu-latest
steps:
- run: echo "building"On a pull request, lint is skipped. Because !failure() && !cancelled() is true, build still runs. If lint fails, build is skipped.
Common mistakes
- Writing
if: ${{ needs.build.result == 'failure' }}withoutalways()orfailure(). The implicit success check still applies, so the job is skipped. - Forgetting that
needs.*.resultonly includes jobs listed inneeds. - Using
always()on a deploy job.
Exercise
A cleanup job has needs: [build] and if: ${{ needs.build.result == 'failure' }}. The build job fails, but cleanup is skipped. Why, and how do you fix it?
Show answer
The if has no status check function, so GitHub still requires every needed job to succeed, and build did not. Add a status function, for example if: ${{ always() && needs.build.result == 'failure' }} or if: ${{ failure() }}.
Exercise
You want a report job to run after build and test whether they pass or fail, but not when the run is cancelled. Which expression do you use?
Show answer
if: ${{ !cancelled() }}. It is true after success and after failure, and false when the run is cancelled.