Trigger pipeline on Merge Request Approval

*Dear Forum, im triying to trigger a pipeline after the merge request gets approved.

Is that possible?

Thanks for any input

Hello Palumbo,

Is it possible with native .gitlab-ci.yml syntax? Unlikely.

Is it possible? Yes.

I’m unsure of what your use case is for this. Shouldn’t a MR reviewer/approver only approve a code change if it is passing a pipeline?

Are you expecting an external pipeline (not from your existing project) to be triggered when an MR gets approved?

Potential High Level View of a Solution

(This only is valid if you are talking about actual “approvals” and not :+1: added to MRs)

  1. Create a generic/bot user with an API token which has access to trigger pipelines for the respective projects.
  2. Deploy a webhook service that validates an MR event and approval and performs the desired triggering of a pipeline. (See a sample snippet of a golang server that adds comments when there is an approval here and in action here)
  3. Register the Webhook on your project with a Trigger for Merge Request Events.
  4. Profit? :dollar: :chart_with_upwards_trend:

This may not be what you need/is very complicated. (however it was fun making a webhook)

Cheers!
– tmos22

1 Like

It’s now 2023 guys. Not sure if we still need a webhook handler server now.
My use case is after reviewers approve my merge request, i need to trigger automated regression tests.
This regression test is very time and resource consuming so i can’t afford to have it run on every git push but only when the merge request is approved. If the tests passed, developer is allowed to merge the code in main branch.

I know this is probably not what you’re asking, but we have certain tasks in our pipeline that should run when MR is merged (not approved)… we do this by checking $CI_COMMIT_TITLE and $CI_COMMIT_MESSAGE using regex.

  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_COMMIT_TITLE =~ /^Merge branch/

This job will only run from the main branch and only if this commit relates to a merge request. Then, within the script we extract MR ID from the $CI_COMMIT_MESSAGE using bash regex.

1 Like

I have this exact same use case. Once an MR is approved by dev I want automated regression tests to run in a pipeline that is automatically generated.

Is there any update on this?

2 Likes