Skip to content

Multiple Pipelines in Gitlab

Posted on:October 7, 2024

I’ve been struggling with duplicate pipelines in GitLab.

If you get duplicate pipelines in merge requests, your pipeline might be configured to run for both branches and merge requests at the same time. Adjust your pipeline configuration to avoid duplicate pipelines. You can add workflow:rules to switch from branch pipelines to merge request pipelines. After a merge request is open on the branch, the pipeline switches to a merge request pipeline.

A few places suggest using a workflow and not starting a branch build if a MR is open, however there is a timing issue here when the MR is first opened. Instead, we now use the following workflow:

workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: "$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS"
      when: never
    - if: '$CI_COMMIT_BRANCH =~ /^mr-\.*/'
      when: never
    - if: "$CI_COMMIT_BRANCH || $CI_COMMIT_TAG"

This will create a pipeline: