Sub topic 7
Status check functions: success, failure, always, cancelled
This chapter assumes you can read a workflow file (on, jobs, steps), know the difference between a job and a step, and have used a basic if: on a step.
The default: an implicit success()
A step without an if runs only while every earlier step in the job has succeeded. GitHub Actions treats such a step as if it had if: success(). When a step fails, the job skips the remaining steps that have no status function.
Four status check functions change this behavior:
| Function | True when |
|---|---|
| `success()` | All previous steps succeeded (the default). |
| `failure()` | Any previous step in the job failed. |
| `always()` | Always, even if the workflow was cancelled. |
| `cancelled()` | The workflow run was cancelled. |
Running steps after a failure
Use failure() for work that only matters when something broke, such as saving logs. Use always() for cleanup that must happen every time.
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
run: ./run-tests.sh
- name: Upload logs on failure
if: failure()
uses: actions/upload-artifact@v4
with:
name: test-logs
path: logs/
- name: Cleanup
if: ${{ always() }}
run: ./cleanup.shIf Run tests fails, the upload and the cleanup both run. If it passes, the upload is skipped and the cleanup still runs.
Combining status functions with other conditions
A status function can be combined with other expressions. Once an if contains a status function, the implicit success() is no longer added. Write the status check yourself when you need it.
- name: Notify on main failures
if: failure() && github.ref == 'refs/heads/main'
run: echo "Main is broken"A bare condition such as github.ref == 'refs/heads/main' still gets the implicit success(). So it would not run after a failure.
Status functions across jobs
Status functions also apply to jobs. A job with needs runs only if all the jobs it depends on succeeded. To run a job when a dependency fails, use always() and check the result.
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"When test fails, deploy is skipped. notify still runs, because always() overrides the default and needs.test.result is 'failure'.
Common errors
- Using
always()for cleanup that should respect cancellation.always()runs even after a cancel and can keep a stuck run alive. Usesuccess() || failure()when you want to skip cancelled runs. - Expecting
failure()in a later job to see an earlier job's failure. Inside a job,failure()reflects the dependency chain created byneeds. Checkneeds.<job_id>.resultto be explicit. - Forgetting that a status function replaces the implicit
success().if: always() && github.ref == 'refs/heads/main'runs on main whatever happened before.
Exercise
A cleanup step must run after tests pass or fail, but must not run when the workflow is cancelled. Which if expression do you write?
Show answer
if: success() || failure(). It is false only when the run is cancelled, whereas always() would also run on cancellation.