Skip to content
Latchkey

How to Set Up a Merge Request Pipeline in GitLab CI

workflow:rules tells GitLab to run a single pipeline in the merge request context instead of doubling up.

Without rules, a push to an open MR branch can trigger both a branch pipeline and an MR pipeline. The standard fix uses workflow:rules to pick one.

One pipeline per change

Run on merge requests, and on branches only when no MR is open, so you never get duplicate pipelines.

.gitlab-ci.yml
workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_COMMIT_BRANCH

test:
  script: npm test
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"

Notes

  • MR pipelines expose variables like CI_MERGE_REQUEST_TARGET_BRANCH_NAME for diff-aware logic.
  • The when: never rule suppresses the redundant branch pipeline while an MR is open.
  • Merge results pipelines go further by running against the post-merge result.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →