Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab run a pipeline job when a merge request is merged

I have a gitlab pipeline where there are two stages, one is build and the other one is deployed. The build stage is run when a commit is made. I want a way to run the deploy job when the merge request is merged to master. I tried several things but no luck. Can anyone help?

stages:
  - build
  - deploy

dotnet:
script: "echo This builds!"
stage: build


production:
script: "echo This deploys!"
stage: deploy

only:
  refs:
    - master
like image 853
Hussain Moiz Avatar asked Sep 15 '20 00:09

Hussain Moiz


People also ask

What triggers GitLab pipeline?

GitLab CI/CD pipelines, by default, are executed automatically when new commits are pushed to a repository and do not require any intervention once created.


2 Answers

Try using the gitlab-ci.yml "rules" feature to check for the merge request event.

Your current gitlab-ci.yml will run your "dotnet" job every commit, merge request, schedule, and manually triggered pipeline.

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

dotnet:
  script: "echo This builds!"
  stage: build
  rules:
    - if: '$CI_BUILD_REF_NAME != "master" && $CI_PIPELINE_SOURCE == "push" || $CI_PIPELINE_SOURCE == "merge_request_event"'

production:
  script: "echo This deploys!"
  stage: deploy
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_BUILD_REF_NAME == "master"'
like image 171
amBear Avatar answered Sep 21 '22 00:09

amBear


If you want your job run on only after merging the merge request, then you can trigger a job based on the commit message, as shown below.

rules: - if: '$CI_COMMIT_MESSAGE =~ /See merge request/'

Basically, all the merge request comes with a "See merge request" commit message so you can depend on that message to trigger your job.

like image 28
sri05 Avatar answered Sep 20 '22 00:09

sri05