Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a pipeline cleanup automatically after a merge request was merged

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.

like image 918
kolaente Avatar asked Aug 28 '19 07:08

kolaente


People also ask

How do I run a pipeline after merge?

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.

How do you automate merge requests?

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.

Should I delete source branch when merge request is accepted?

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.

What happens when you close merge request?

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.


1 Answers

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:

  1. Create a feature branch
  2. Deploy a testing environment
  3. Create a merge request (Check the Delete source branch when merge request is accepted. checkbox) You can also check Delete source branch when merging a Merge Request.
  4. When the merge request is accepted, the feature branch will be deleted, and the corresponding gitlab environment will be stopped. This will trigger the deploy-dynamic-down step.
like image 78
cecunami Avatar answered Nov 16 '22 02:11

cecunami