Run job when pipeline is cancelled or partially skipped
Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.
Description
With the finishing of #15603 (closed), we've lost one of the original feature proposals that was closed as a duplicate of that issue.
This feature would allow running a job when a pipeline is cancelled. With multi-stage pipelines, sometimes infrastructure is stood up at an earlier stage that needs to be torn down at the end. There is a too-inviting cancel pipeline button that we cannot train people not to press that results in infrastructure hanging around and the need for a pre-clean job to attempt to clean up infrastructure left hanging around from previous failed pipelines. This is fragile as the original merge request could have created infrastructure that the new merge request pre-clean job doesn't know about.
We don't want to turn these pipelines into a single stage as there is a need for different tooling at each stage and understanding failures becomes a lot harder.
Proposal
Shamelessly stolen from gitlab-foss#34861 (closed). I'm not too wedded to the syntax.
Add when: on_cancel or similar feature to .gitlab-ci.yml.
[...]
cleanup:
stage: cleanup
script:
- docker-compose down -v
- rm -rf tmp/
when: on_cancel
It's important that this cancelling job is called whether the whole pipeline is cancelled through the cancel pipeline button, or half the pipeline is cancelled due to a step being cancelled resulting in the rest of the pipeline being skipped.