GitLab-CI: Why does a Merge Results pipeline build an unchanged result twice?

1.4k Views Asked by At

I'm trialing the "Ultimate" version of GitLab, in particular the Merge Results Pipeline feature, along with the related Merge Trains feature.

I have Merge Results pipeline & Merge Trains enabled for my project, Merge Method is set to Merge Commit, and my .gitlab-ci.yml has the following rules, as per the docs:

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'

This all seems to work nicely, but there's one thing I don't understand.

Imagine there is just one open Merge Request, and there have been no changes on the target branch main since the related branch was created. When I push to the MR branch, I expect a "merge results" pipeline to run, which as I understand it is a preemptive merge of the target branch with my MR branch. The pipeline succeeds, and the MR is now eligible to add to the Merge Train.

And if I do add it to the Merge Train, it builds again and I must wait for this to complete before it can be merged. Why is this necessary? It has already built the result of the merge, and nothing has changed since, and this is detectable, so why the inefficiency?

This leads to my team wanting to use the "Merge Immediately" button instead of adding to the Merge Train, to avoid the superfluous pipeline (and the resulting wait for the actual merge to happen). I don't want this to become a habit since it defeats the purpose of these two features.

Previously we just used FF-only branch pipelines and although it was painful to constantly rebase or merge the target, at least if the build was green and nothing had changed, the merge could happen immediately.

Perhaps I'm missing something with my configuration?

2

There are 2 best solutions below

4
On

This is because the merge result pipeline and merge train are two separate events. GitLab does automatically detect redundant pipelines in merge trains and automatically cancels those redundant pipelines.

However, if you add your MR to a merge train by saying it depends on another MR, what is being built is not the same as the "merged result" pipeline because the refs from the other MRs in the train must be added as well.

Based on your described scenario, the merge train pipeline is not actually redundant and therefore is not automatically cancelled.

It is also worth noting that Fast-forward merge support for merge trains is a planned feature for merge trains to make them more efficient.

4
On

the simplest case where there is only one open MR, targeted at main, and it has been built already as a "result" pipeline, and then it is added, unmodified, to a fresh, new Merge Train, as the only MR to be merged, and main hasn't changed either.

Check if GitLab 16.4 (September 2023) could help in your scenario, since it introduces:
(for Premium/Ultimate only)

Fast-forward merge support for merge trains

Fast-forward merge is a common and popular merge method which avoids merge commits, but requires more rebasing.
Separately, Merge Trains are a powerful tool to help with some of the greater challenges related to frequently merging into the main branch.

Unfortunately, before this release you could not use merge trains and fast-forward merge together.

In this release, self-managed admins can now enable both Fast-forward merge and merge trains in the same project. You can get all the benefits of merge trains, which ensure all your commits work together before merging, with the cleaner commit history of fast forward merges!

To enable the Fast-forward merge trains, locate the feature flag fast_forward_merge_trains_support, which has been disabled by default, and enable it.

https://about.gitlab.com/images/16_4/ff_merge_train.png -- Fast-forward merge support for merge trains

See Documentation and Epic.

May be said fast-forward merge would help avoid a new merge train, since no new commit would be detected.


GitLab 16.5 (October 2023) adds (again, for Premium/Ultimate only):

Fast-forward merge trains with semi-linear history

In 16.4, we released Fast-forward merge trains, and as a continuation, we want to ensure we support all merge methods. Now, if you want to ensure your semi-linear commit history is maintained you can use semi-linear fast-forward merge trains.

https://about.gitlab.com/images/16_5/ff-merge.png -- Fast-forward merge trains with semi-linear history

See Documentation and Issue.