Want to run a script when a previous job fails

Use Case

If a Job fails a script needs to be run to send a specific message to Pagerduty.
In this script is some additional information to find the problem…

Example gitlab-ci.yml

stages:

  • build
  • deploy
  • notify-deploy-failed
  • undeploy
  • notify-undeploy-failed

destroy:
stage: undeploy
dependencies:
- apply
only:
- master
when: manual
allow_failure: true
script:
- export TF_WARN_OUTPUT_ERRORS=1
- terraform destroy -force

notify_undeploy_failed:
stage: notify-undeploy-failed
when: on_failure
allow_failure: true
script:
- ./scripts/notify.sh

Problem

When the destroy fails the notify-undeploy-failed is not run.
From the pipeline the destroy is marked with a problem. But the notify-undeploy-failed is not triggerd.
Any suggestions or advice?

I believe setting allow_failure to true in the destroy job actually places that job in a different “state” than if it failed completely.

CI/CD Reference for allow_failure

When enabled and the job fails, the job will show an orange warning in the UI. However, the logical flow of the pipeline will consider the job a success/passed, and is not blocked.

If you remove that from the destroy job, the notify should run.