Run environment cleanup before new deployment
Release notes
Problem to solve
Sometimes a job deploys an environment which depends on specific details which aren’t available in follow-up deployments. This environment may be needed to do cleanups as well.
For example, let’s say we have the following .gitlab-ci.yml
:
start review:
rules:
- if: $CI_MERGE_REQUEST_ID
environment:
name: review/$CI_MERGE_REQUEST_IID
on_stop: stop review
script:
- helm install "$CI_COMMIT_SHORT_SHA" .
stop review:
rules:
- if: $CI_MERGE_REQUEST_ID
when: manual
allow_failure: true
environment:
name: review/$CI_MERGE_REQUEST_IID
action: stop
script:
- helm delete "$CI_COMMIT_SHORT_SHA"
This will deploy a review environment for each merge request. Each time a new commit is pushed to the merge request branch, a new deployment is made. The stop review
job will only be triggered if the merge request is closed, but not for all older environments.
Proposal
I propose to add a way to cleanup an environment before a next one is deployed. Something along the lines of:
start review:
rules:
- if: $CI_MERGE_REQUEST_ID
environment:
name: review/$CI_MERGE_REQUEST_IID
+ on_replace: stop review
on_stop: stop review
script:
- helm install "$CI_COMMIT_SHORT_SHA" .
or:
start review:
rules:
- if: $CI_MERGE_REQUEST_ID
environment:
name: review/$CI_MERGE_REQUEST_IID
+ stop_before_replace: true
on_stop: stop review
script:
- helm install "$CI_COMMIT_SHORT_SHA" .
This would run the stop review
job using the context of the old deployment before creating replacing the old deployment.