How to run pipeline after merge request approved in Gitlab CI?

Viewed 3174

I want GitLab CI to run a job after a merge request is merged. I don't want it to be run on CREATING a new merge request and also I don't want it to be run whenever target branch is updated. (Since it's possible to commit directly to target branch and the job should not be run in that situation.)

Is that possible?

If yes, I also want to know informations about the merge request which triggered the job.

(Actually I want to update my project management system, when a merge request is merged. Thus I need to know which merge request is merged (or approved).)

Thanks in advance.

1 Answers

I want GitLab CI to run a job after a merge request is merged.

Unfortunately, GitLab does not offer a "merge request merged" trigger.

What you can do, is to make the pipeline run for any push in a certain branch and use branch-protection to make sure pushs can only come from merge requests. To do that:

  1. Set your pipeline to only run e.g. for branch main:
# if you want to use "only":
my_job: 
  only:
    - main

# or alternatively if you want to use rules you can do the same with: 
my_job:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_REF_NAME == "master"'
  1. Enable Branch protection to disable direct pushes to main and only allow MRs: enter image description here
Related