We are in the process of adding dynamic deploys to a testing environment where each merge request gets its own url, to be able to manually do frontend testing for people who are not developers.
These instances are deployed with docker-compose
and GitLab CI/CD which works well.
I now want to automatically shut down these instances, once the merge request is merged.
So far, I have this extra step to manually shut down the testing instance:
deploy-dynamic-down:
tags:
- docker
stage: deploy-dynamic
script:
- docker-compose down
when: manual
This works well, but requires a manual step which will almost certainly be forgotten every now and then, requiring us to clean our runner every now and then.
So, my question is: can I trigger this pipeline step to run only when a merge request is merged?
I can't just limit this pipeline to certain branches like master
or develop
, because we don't always create merge requests to only these branches. I don't know which branch the merge request will target beforehand.
Configuring pipelines for merge requests To configure pipelines for merge requests, add the only: [merge_requests] parameter to the jobs that you want to run only for merge requests. Then, when developers create or update merge requests, a pipeline runs every time a commit is pushed to GitLab.
You can use push options to automatically create a merge-request in GitLab, like so: $ git push -o merge_request. create ... The current branch will be pushed, it will be followed locally, a merge request based on that branch will be created, and the option to "Remove source branch" after merge checked on GitLab.
Enable the delete source branch when merge request is accepted option to keep your repository clean. Enable the squash commits when merge request is accepted option to combine all the commits into one before merging, thus keep a clean commit history in your repository.
A closed merge request is one that has been put aside or considered irrelevant. It is therefore not merged into the code base. Therefore, you only merge MRs when you're happy with the changes and close them if you think the changes are not worthy of being integrated into the code base ever.
You can use Gitlab Environments to achieve this.
You can configure a dynamic gitlab environment to be created for each one of your testing environments.
You can set your deploy-dynamic-down step to be executed when the testing environment is stopped.
The key part here is that the environment will be automatically stopped when the source branch is deleted.
Workflow:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With