8

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
    Does this answer your question? [Gitlab run a pipeline job when a merge request is merged](https://stackoverflow.com/questions/63893431/gitlab-run-a-pipeline-job-when-a-merge-request-is-merged) – Sty Jan 10 '22 at 16:41

4 Answers4

7

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
Josh Correia
  • 3,807
  • 3
  • 33
  • 50
uupascal
  • 531
  • 4
  • 6
2

You can run the pipeline after merge by using Gitlab ci predefined variable $CI_MERGE_REQUEST_APPROVED this will return true after merge has been done and available from gitlab v14.1.

you can add the rule like this in your job.

rules:
  - if: $CI_MERGE_REQUEST_APPROVED
  • 1
    Using $CI_MERGE_REQUEST_APPROVED is not what should be used for this question. $CI_MERGE_REQUEST_APPROVED becomes true if someone approves a merge request, not if the MR is merged. An MR can be approved and not yet merged. – mdailey77 Jun 28 '23 at 14:10
0

Not sure why this isn't mentioned before, but I was able to achieve what I wanted by leveraging the Environment on_stop event.

  1. Set up the pipeline to only run under the MR context
  2. Have an environment that's tied to the MR
  3. Set up on_stop event for the environment for the MR to run a stage, e.g. "OnStop"
  4. Put whatever you want to be triggered in the "OnStop" stage

https://docs.gitlab.com/ee/ci/yaml/#environmenton_stop

Jonny Lin
  • 727
  • 4
  • 11
0

This might be the rules to control your job

job:
   rules:
     - if : '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_COMMIT_BRANCH '
       when: never
     - if: $CI_COMMIT_TITLE =~ /Merge branch.*/
       when: on_success
   script:
     - echo "the merge request which has done has the ID $CI_MERGE_REQUEST_IID"
     # other scripts to run your job

the value of CI_MERGE_REQUEST_IID in format of !number will be shown in merge requests left-side menu in ui gitlab, as well as jobs and pipelines list. I guess you can use this id number to verify which merge request has done successfully.

foad322
  • 71
  • 10