doc/ci/pipelines/merge_trains.md
{{< details >}}
{{< /details >}}
In projects with frequent merges to the default branch, changes in different merge requests might conflict with each other. Use merge trains to put merge requests in a queue. Each merge request is compared to the other, earlier merge requests, to ensure they all work together.
A merged results pipeline tests one merge request's changes combined with the target branch. A merged results pipeline does not account for other merge requests that merge around the same time. Two merge requests can each pass their own pipeline, but their combined changes can still conflict. If both merge, the target branch can break, even though every pipeline succeeded.
%%{init: { "fontFamily": "GitLab Sans" }}%%
graph LR
accTitle: Two merge requests that pass individually but conflict together
accDescr: Merge request A and merge request B each pass a pipeline that tests their changes combined with the target branch alone. When both merge, the combined changes break the target branch.
subgraph Without merge trains
target[Target branch] --> pipeline_a[Pipeline for A: passes]
target --> pipeline_b[Pipeline for B: passes]
pipeline_a --> merge_both[Both merge]
pipeline_b --> merge_both
merge_both -.-> broken[Target branch breaks]
end
Merge trains prevent this by testing each merge request against the combined changes of every merge request ahead of it in the queue. This catches conflicts before they reach the target branch.
Use merge trains if your project has:
A merge train starts when there are no merge requests waiting to merge and you select Merge or Set to auto-merge. GitLab starts a merge train pipeline that verifies that the changes can merge into the default branch. This first pipeline is the same as a merged results pipeline, which runs on the changes of the source and target branches combined together. The author of the internal merged result commit is the user that initiated the merge.
To queue a second merge request to merge immediately after the first pipeline completes, select Merge or Set to auto-merge to add it to the train. This second merge train pipeline runs on the changes of both merge requests combined with the target branch. Similarly, if you add a third merge request, that pipeline runs on the changes of all three merge requests merged with the target branch. The pipelines all run in parallel.
%%{init: { "fontFamily": "GitLab Sans" }}%%
graph LR
accTitle: Merge train pipelines test combined changes
accDescr: Pipeline 1 tests merge request A against the target branch. Pipeline 2 tests merge request A and B together against the target branch. Pipeline 3 tests merge request A, B, and C together against the target branch. The three pipelines run in parallel.
subgraph Merge train
target[Target branch] --> pipeline_1[Pipeline 1: A]
target --> pipeline_2[Pipeline 2: A + B]
target --> pipeline_3[Pipeline 3: A + B + C]
end
Each merge request merges into the target branch only after:
If a merge train pipeline fails, the merge request is not merged. GitLab removes that merge request from the merge train, and starts new pipelines for all the merge requests that were queued after it.
For example:
Three merge requests (A, B, and C) are added to a merge train in order, which
creates three merged results pipelines that run in parallel:
A combined with the target branch.A and B combined with the target branch.A, B, and C combined with the target branch.If the pipeline for B fails:
A) continues to run.B is removed from the train.C is canceled, and a new pipeline
starts for the changes from A and C combined with the target branch (without the B changes).If A then completes successfully, it merges into the target branch, and C continues
to run. Any new merge requests added to the train include the A changes now in
the target branch, and the C changes from the merge train.
<i class="fa-youtube-play" aria-hidden="true"></i> Watch this video for a demonstration on how parallel execution of merge trains can prevent commits from breaking the default branch.
GitLab CI/CD detects redundant pipelines, and cancels them to conserve resources.
Redundant merge train pipelines happen when:
In these cases, GitLab must create new merge train pipelines for some or all of the merge requests on the train. The old pipelines were comparing against the previous combined changes in the merge train, which are no longer valid, so these old pipelines are canceled.
Prerequisites:
To enable merge trains:
Prerequisites:
To start a merge train:
The merge request's merge train status displays under the pipeline widget with a
message similar to A new merge train has started and this merge request is the first of the queue. View merge train details.
You can select the link to view the merge train.
Other merge requests can now be added to the train.
{{< history >}}
{{< /history >}}
You can view the merge train to gain better insight into the order and status of merge requests in the queue. The merge train details page shows active merge requests in the queue and merged merge requests that were part of the train.
To access the merge train details from the list of merge requests:
You also access this view by selecting View merge train details from:
You can also remove ({{< icon name="close" >}}) a merge request from the merge train details view.
{{< history >}}
merge_when_checks_pass_merge_train. Disabled by default.merge_when_checks_pass_merge_train removed.{{< /history >}}
Prerequisites:
To add a merge request to a merge train:
The merge request's merge train status displays under the pipeline widget with a
message similar to This merge request is 2 of 3 in queue.
Each merge train can run a maximum number of pipelines in parallel. The default limit is 20. If you add more merge requests to the merge train than the limit, the extra merge requests are queued until a pipeline completes. The number of queued merge requests is unlimited.
After a merge request joins a merge train, new conversation threads do not remove it from the train or prevent the merge, even when all threads must be resolved is enabled. This behavior is intentional. For more information, see issue 220916.
When you remove a merge request from a merge train:
You can add the merge request to a merge train again later.
To remove a merge request from a merge train:
If you have a high-priority merge request, like a critical patch that must be merged urgently, you can select Merge immediately.
[!warning] Merging immediately can use a lot of CI/CD resources. Use this option only in critical situations.
When you merge a merge request immediately:
[!note] The merge immediately option may not be available if your project uses the fast-forward merge method and the source branch is behind the target branch. See issue 434070 for more details.
{{< details >}}
{{< /details >}}
{{< history >}}
merge_trains_skip_train. Disabled by default.{{< /history >}}
[!flag] On GitLab Self-Managed, by default this feature is available. To hide the feature, an administrator can disable the feature flag named
merge_trains_skip_train. On GitLab.com and GitLab Dedicated, this feature is available.
You can allow merge requests to be merged without completely restarting a running merge train. Use this feature to quickly merge changes that can safely skip the pipeline, for example minor documentation updates.
You cannot skip merge trains for fast-forward or semi-linear merge methods. For more information, see issue 429009.
Skipping merge trains is an experimental feature. It may change or be removed completely in future releases.
[!warning] You can use this feature to quickly merge security or bug fixes, but the changes in the merge request that skipped the train are not verified against any of the other merge requests in the train. If these other merge train pipelines complete successfully and merge, there is a risk that the combined changes are incompatible. The target branch could then require additional work to resolve the new failures.
Prerequisites:
To enable skipping the train without pipeline restarts:
To merge a merge request by skipping the merge train, use the merge requests merge API endpoint
to merge with the attribute skip_merge_train set to true.
The merge request merges, and the existing merge train pipelines are not canceled or restarted.
{{< history >}}
{{< /history >}}
By default, each merge train can run a maximum of 20 pipelines in parallel. When this limit is reached, additional merge requests are queued until a pipeline slot is available.
To modify this limit for your project:
1. A value of 1 processes merge requests sequentially with no parallelism.The project limit cannot exceed the instance limit.
You can also use the projects API, or the GraphQL API.
{{< history >}}
merge_train_enforcement. Disabled by default.merge_train_enforcement removed.{{< /history >}}
By default, if you have permission to merge, you can bypass the merge train. Enforcement requires every merge request to go through the train.
When enforcement is enabled:
Merge train enforcement has three levels:
Prerequisites:
To configure merge train enforcement:
If a merge request becomes unmergeable while a merge train pipeline is running, the merge train drops your merge request automatically. Common causes include:
You can find the reason the merge request was dropped from the merge train in the system
notes. Check the Activity section in the Overview tab for a message similar to:
User removed this merge request from the merge train because ...
You cannot use auto-merge (formerly Merge when pipeline succeeds) to skip the merge train, when merge trains are enabled. See issue 12267 for more information.
When a merge train pipeline fails, the merge request is dropped from the train and the pipeline can't be retried after it fails. Merge train pipelines run on the merged result of the changes in the merge request and changes from other merge requests already on the train. If the merge request is dropped from the train, the merged result is out of date and the pipeline can't be retried.
You can:
retry keyword to the job if it fails intermittently.
If it succeeds after a retry, the merge request is not removed from the merge train.When Pipelines must succeed is enabled, but the latest pipeline failed:
The pipeline for this merge request failed. Please retry the job or push a new commit to fix the failure.Before you can re-add a merge request to a merge train, you can try to:
For more information, see issue 35135.
If merge train enforcement is enabled, any tool that calls the
merge requests API without auto_merge=true
receives a 405 Method Not Allowed response. This includes scripts, CI/CD jobs, and bots.
To resolve this, update the tool to pass auto_merge=true, which adds the merge request to
the merge train instead of merging it directly. For example, if you use
Renovate, enable the
platformAutomerge configuration option.