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
How to Use Dynamic Child Pipelines in GitLab CIGenerate a GitLab CI config at runtime and run it as a child pipeline with trigger:include:artifact, so the p…
How to Cache node_modules in GitLab CICache node_modules in GitLab CI keyed on package-lock.json so npm ci reuses dependencies across jobs and pipe…