How do you run cleanup or notification logic that must always run at the end of a job?
BeginnerConceptJobs, Steps, Matrix & Concurrency
Answer
Put it in a final step with if: always() (or !cancelled() if it should not run on cancel):
- name: Tear down test env
if: always()
run: terraform destroy -auto-approve
- name: Notify on failure
if: failure()
run: curl -fsS -X POST "$SLACK_WEBHOOK" -d '{"text":"build failed"}'
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
JavaScript actions can also define post: scripts that run at job end automatically (that is how actions/cache saves).
More on Jobs, Steps, Matrix & Concurrency
How does `needs` work, and what happens to a job whose dependency failed or was skipped?BeginnerWhat is the implicit `success()` in a job `if`, and why is `always()` usually the wrong fix?IntermediateWhat values can `needs.<job>.result` take?BeginnerHow does a matrix expand, and how are job names generated?BeginnerHow do `include` and `exclude` work in a matrix?IntermediateWhat do `fail-fast` and `max-parallel` do?Beginner