Skip to main content
Photo by Mila Tovar via Unsplash

Fixing Double Ci Runs When Pushing to Gitlab Branches

A while back I realized, that every time I pushed some commits to a branch on GitLab two separate CI pipelines started. That soon took up lots of free CI-minutes and became a problem. After some research I found out that this is, while it’s to be expected due to the design of the system, avoidable with a specific configuration addition.

The reason for the double run is, that CI pipelines run for several events in a repository, the two here are push events to a branch and push events to an existing merge request.

The solution sounds as logical as you would expect: just™ tell the pipeline the following:

  • if this CI run event is for a branch push and no merge request exists, then run it
  • if this CI run event is for a branch push and a merge request exists, then don’t run it
  • if this CI run event is for a merge request push then run it

or in GitLab’s CI YAML format:

1WorkflowName:
2  rules:
3    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
4    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
5      when: never
6    - if: $CI_COMMIT_BRANCH

or with an existing rule:

1WorkflowName:
2  rules:
3    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS && $CI_PIPELINE_SOURCE == "push"
4      when: never
5    - if: $YOUR_OWN_RULES               # add your own rule here

While it looks complicated it’s an addition to my existing workflow that is easy to handle. And it saves CI minutes.

Back to top
Back Forward