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

5

11

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.

Pretypify answered 10/1, 2022 at 15:6 Comment(1)
Does this answer your question? Gitlab run a pipeline job when a merge request is mergedMysticism
K
9

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 == "main"'
  1. Enable Branch protection to disable direct pushes to main and only allow MRs: enter image description here
Kerk answered 12/1, 2022 at 15:55 Comment(6)
Can you please add more details here...Fordo
I've added more details. Hope it helps.Kerk
if: '$CI_PIPELINE_SOURCE == "push" && $CI_BUILD_REF_NAME == "master"' doesn't work. Nothing is triggered for me. Did I miss something?Imaimage
@LucasWillems the following should work. It works for me on GitLab 15.1, at least. if: $CI_COMMIT_BRANCH == "main"Inbreeding
will this fail the merge if the pipeline breaks? How does it relates to docs.gitlab.com/ee/ci/pipelines/merged_results_pipelines.html ?Nestornestorian
None of these solutions worked for me. The job is not triggerred. Adding the condition results in pipeline running, but all jobs run EXCEPT those with condition.Rosellaroselle
B
3

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
Berwick answered 4/11, 2022 at 5:18 Comment(5)
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.Centenary
Is the Syntax for the usage of this Rule correct ?Familial
@RadheshKhanna Yes, the rule is correct.Berwick
Hey @Centenary according to the question the user wants to run pipeline after merge request approval.Berwick
The first sentence says 'I want GitLab CI to run a job after a merge request is merged'.Centenary
Z
1

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

Zandrazandt answered 28/4, 2023 at 20:55 Comment(2)
Creative solution! I suspect they make it difficult because "merge result pipelines" is premiumDarindaring
„Merge result pipeline“ sadly doesn’t do what is asked here, I’ve fallen for this too. It‘s a single checkbox in project settings that will make the existing merge request pipelines run on a merged version of the code, but you are still not able to trigger/do something after someone actually clicks on „merge“.Kerk
F
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.

Flummox answered 27/7, 2023 at 12:11 Comment(0)
B
0

Gitlab can fire a webhook when a merge request is approved or updated and it is possible to use that webhook to start a ci job with a pipeline trigger token.

  • First you have to create a pipeline trigger token. (Settings > CI/CD > Pipeline trigger tokens)
  • Then you need to create the webhook. (Settings > Webhooks).
    • Set the URL to your pipeline trigger token, without the ref part.
    • Configure the webhook trigger to be Merge request events (not to be confused with merge_request_event in the CI_PIPELINE_SOURCE 🙊 )
    • Configure a custom webhook template to pass detials of the webhook events as CI variables.
    {
      "variables": {
        "event_type":"{{event_type}}",
        "merge_request_id":"{{object_attributes.id}}",
        "merge_request_iid":"{{object_attributes.iid}}",
        "action":"{{object_attributes.action}}",
        "detailed_merge_status":"{{object_attributes.detailed_merge_status}}"
      },
      "ref":"{{object_attributes.source_branch}}"
    }
    
  • Now you can add rules to your CI job such as:
    rules:
      - if: '$CI_PIPELINE_SOURCE == "trigger" && $event_type = "merge_request" && $action == "approved" && $merge_request_id == $CI_MERGE_REQUEST_ID'
    
Backer answered 22/8 at 21:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.