@Novikov 's answer is exactly what you need, I also thought at first that it worked only manually, but it works automatically. I add some precision that is not in his answer.
I had a similar utility as you.
- Repo-1 contains the application code.
- Repo-2 contains the terraform code to deploy/destroy the infrastructure of the application.
Repo-1 pipeline triggers a pipeline on my repo-2 that will deploy the application and so my need was that deleting a branch in repo-1 triggers the repo-2 pipeline to delete the environment.
So i used the environment
feature, the gitlab documentation explain how to use it.
deploy:
stage: deploy
script:
- {...}
environment:
name: deployment-${CI_COMMIT_BRANCH}
on_stop: destroy
destroy:
stage: deploy
variables:
GIT_STRATEGY: none
script:
- {...}
environment:
name: deployment-${CI_COMMIT_BRANCH}
action: stop
when: manual
IMPORTANT
- I know that we use
when: manual
but it work automaticaly when a
branch is deleted/merged.
- Both jobs (deploy - destroy) need have the same stage.
- GIT_STRATEGY variable need to be set to none otherwise it will try to fetch the repo from a branch that no longer exists and the job will fail.
- Use a dynamic variable for the environment name
otherwise it won't work, each environment must have its own name.
- It is also important to precise that
environment
feature does not
work with the trigger
feature, so if you want to trigger another pipeline you need to use the API.
Source:
Gitlab Documentation