Start Jobs / Pipline After MR has been Merged

Hi all,

For a project me & my team are working on we need to implement the following with Gitlab CI:

After a MR has been merged on a release branch a pipeline should trigger a build job and automatically start deploy job that deploys to our dev environment. A manual action is available for deploying to staging.

For production this would be the same case as with the release but then for master.

Currently I have not found a way to start certain jobs or a pipeline directly after a MR has been merged.

Current setup:

build:
  stage: build
  image: docker:stable
  services:
    - docker:dind
  rules:
    - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == /^release\/.*$/'
      when: on_success
  <<: [*build_before, *build_script, *build_after]

However this part runs when the MR is created I need this to be created after the MR has been merged.

Anyone idea’s or tips to be able to achieve this would be very helpfull!

Hi there,

The most simplest way is to use the only keyword and specify the branches as per your requirement

Sample: If you would like to run specific job only on the MR’s then you can specify only: merge_requests
Once the MR is merged - eventually the master or the branch where the merge occurred will be updated - as a result the pipeline will be triggered, you can specify the custom jobs to run post merge using the only: master


build:
  stage: build
  script: ./build
  only:
  - master

test:
  stage: test
  script: ./test
  only:
  - merge_requests

deploy:
  stage: deploy
  script: ./deploy
  only:
  - master

Kindly have a look at the documentation for more information and features here

1 Like

Thanks for your feedback! I have finally found a way to accomplish this together with your feedback.

I’ve a db migrations repo in the branch DEV-23-Q4(Branch for Dev Database Changes). I want to run a flyway job as soon as some scripts are MERGED into the branch. flyway parameters are :
host: dev-host
dbname: - has to get from the changes that are merged into the branch’s repo. If changes are pushed for multiple databases, multiple jobs has to trigger one after the other.
schemaname: same as db name
username : same as db name
password: should be encoded
extra params : -outOfOrder=true,
action= repair, info, migrate

anyone who has done this , can you please guide me