Skip to content

Gitlab :: How to avoid duplicate pipelines

When one has defined a CI pipeline in Gitlab that runs on every push to your repository and one that runs for merge requests, one may end up with duplicate pipelines running waisting money and resources

What happens is quite simple, a first pipeline is started when one pushes to the repository and, if the branch is part or a Merge Request, another pipeline is started when this merge request is updated.


There is a rather simple workflow rule to avoid this:

# .gitlab-ci.yaml

# ... stuff before

        - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
        - if: $CI_OPEN_MERGE_REQUESTS == null

# ... stuff after
  • if: $CI_PIPELINE_SOURCE == 'merge_request_event' makes sure the pipeline is triggered for every push to a Merge Request
  • if: $CI_OPEN_MERGE_REQUESTS == null makes sure the pipeline is triggered when there is no open merge request associated with the branch

With these two simple workflow lines, duplicate pipelines become impossible as you're either in the first or in the second scenario, but never in both.