Sub topic 9
Status check functions: success, failure, always, cancelled
This chapter assumes you can read a workflow file (on, jobs, runs-on, steps) and that you know what an if condition is. Status check functions are the part of expressions that look at how the job or the earlier steps ended.
The default behavior
Every step and job has an implicit if: success(). If an earlier step fails, GitHub skips the later steps in that job. If a job listed in needs fails, the dependent job is skipped. Status check functions let you change this.
| Function | True when |
|---|---|
| `success()` | No previous step has failed or been cancelled. This is the default. |
| `failure()` | Any previous step of the job has failed. In a job with `needs`, it is true if any ancestor job failed. |
| `cancelled()` | The workflow run was cancelled. |
| `always()` | Always, even after a failure or a cancellation. |
A cleanup and notification example
name: status-checks
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Run tests
run: exit 1
- name: Runs only if everything so far passed
run: echo "Tests passed"
- name: Runs only after a failure
if: failure()
run: echo "Something failed, collecting logs"
- name: Runs when the run is cancelled
if: cancelled()
run: echo "Run was cancelled"
- name: Always clean up
if: always()
run: echo "Cleaning up"
notify:
needs: test
if: failure()
runs-on: ubuntu-latest
steps:
- run: echo "The test job failed"Here Run tests fails. Runs only if everything so far passed is skipped because its implicit condition is success(). The failure() step runs, and so does always(). The cancelled() step is skipped because the run was not cancelled. The notify job runs because test failed. Without its if, notify would be skipped.
Combining status functions with other conditions
When you write a status function inside a larger expression, you can combine it with && and ||.
- name: Upload logs on failure from main
if: ${{ failure() && github.ref == 'refs/heads/main' }}
run: echo "Uploading logs"Important: if your if expression contains no status check function, GitHub adds success() && in front of it. So if: github.ref == 'refs/heads/main' will not run after a failure. To run a step after a failure regardless of other conditions, include a status function such as always() or failure().
Common errors
- Using
if: github.ref == 'refs/heads/main'for a cleanup step and wondering why it skips after a failure. The implicitsuccess() &&is the cause. - Expecting
failure()in a dependent job to see only the direct dependency. It checks all ancestor jobs in theneedschain. - Using
always()for a job that needs outputs from a skipped job. The outputs are empty in that case.
Exercise
You want a step to upload test reports after the tests, whether they passed or failed, but not when someone cancels the run. Which if expression do you use, and why not always()?
Show answer
Use if: ${{ !cancelled() }}. It is true after success and after failure, and false when the run is cancelled. always() would also run after cancellation, which can delay the cancel.