Skip to content
Latchkey

How to Run Merge Request Pipelines in GitLab CI/CD

A merge request pipeline runs in the MR context and exposes MR variables, which branch pipelines do not.

Add rules:if: $CI_PIPELINE_SOURCE == "merge_request_event" so the job runs in MR pipelines, and avoid duplicate branch-plus-MR pipelines by structuring rules carefully.

Steps

  • Add a rule matching $CI_PIPELINE_SOURCE == "merge_request_event".
  • Read MR context via $CI_MERGE_REQUEST_* variables.
  • Add a branch rule too if you also want branch pipelines, avoiding duplicates.

Pipeline

.gitlab-ci.yml
test:
  script: npm test
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_COMMIT_BRANCH == "main"'

Gotchas

  • Without an MR rule, a job never runs in merge request pipelines and $CI_MERGE_REQUEST_* stays empty.
  • Matching both branch and MR sources can create duplicate pipelines; use workflow:rules to pick one.

Related guides

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