Interview Prep/GitHub Actions/Jobs, Steps, Matrix & Concurrency

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).

All GitHub Actions questions Roadmap: GitHub Actions

More on Jobs, Steps, Matrix & Concurrency