feat(buildagents): read CI_MERGE_REQUEST_REF_PATH in GitLabCi#5008
Conversation
There was a problem hiding this comment.
Pull request overview
Adds GitLab CI Merge Request pipeline support by preferring CI_MERGE_REQUEST_REF_PATH when determining the current branch in the GitLabCi build agent, enabling GitVersion to apply pull-request branch configuration for MR refs after normalization.
Changes:
- Update
GitLabCi.GetCurrentBranch()precedence:CI_COMMIT_TAG→CI_MERGE_REQUEST_REF_PATH→CI_COMMIT_REF_NAME - Add/adjust unit + integration tests to cover MR ref-path behavior and precedence
- Document GitLab MR pipeline behavior and the required
pull-requestregex override formerge-requests/<iid>/(head|merge)
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/GitVersion.BuildAgents/Agents/GitLabCi.cs | Prefer GitLab MR ref-path env var for branch detection, with tag override behavior. |
| src/GitVersion.BuildAgents.Tests/Agents/GitLabCiTests.cs | Extend unit tests to cover MR ref-path precedence, tag override, and fallback behavior. |
| src/GitVersion.App.Tests/PullRequestInBuildAgentTest.cs | Add end-to-end integration coverage for GitLab MR refs using a GitLab-specific pull-request regex config. |
| docs/input/docs/reference/build-servers/gitlab.md | Document MR pipeline variables, resolution order, and recommended config regex/label. |
| private const string GitLabMergeRequestPullRequestConfig = """ | ||
| workflow: GitFlow/v1 | ||
| branches: | ||
| pull-request: | ||
| regex: ^merge-requests/(?<Number>\d+)/(head|merge)$ | ||
| """; |
| private static readonly string[] GitLabMergeRequestRefs = | ||
| [ | ||
| "refs/merge-requests/5/head", | ||
| "refs/merge-requests/5/merge" | ||
| ]; |
|
@JDanRibeiro thanks for taking a second shot, this looks much better, please rebase your branch onto |
In GitLab MR pipelines, CI_COMMIT_REF_NAME is the source branch name, not the merge request ref. Resolve the current branch with precedence: CI_COMMIT_TAG → CI_MERGE_REQUEST_REF_PATH → CI_COMMIT_REF_NAME. Keeps GitLab CI logic in GitVersion.BuildAgents only (no Core changes), following the same pass-through pattern as Azure Pipelines and GitHub Actions. Add unit and integration tests for the MR workflow and document the required pull-request regex for GitLab's merge-requests/ namespace.
70b2161 to
65cbfc9
Compare
|
thanks, the rebase has been executed. |
|
Queued — the merge queue status continues in this comment ↓. |
|
@Mergifyio queue |
Merge Queue Status
This pull request spent 38 seconds in the queue, including 3 seconds running CI. Required conditions to merge
|
|
Thank you @JDanRibeiro for your contribution! |



Description
This PR adds support for GitLab CI Merge Request pipelines by reading
CI_MERGE_REQUEST_REF_PATHin theGitLabCibuild agent.In MR pipelines, GitLab sets
CI_MERGE_REQUEST_REF_PATH(for examplerefs/merge-requests/15/headorrefs/merge-requests/15/merge). Previously, GitVersion used onlyCI_COMMIT_REF_NAME, which contains the source branch name (for exampledeveloperorfeature/foo). Version calculation therefore followedfeature/developconfiguration instead ofpull-request.The change is limited to
GitVersion.BuildAgents. InGetCurrentBranch(), precedence is now:CI_COMMIT_TAG→null(tag pipeline)CI_MERGE_REQUEST_REF_PATH→ merge request ref (pass-through, unchanged)CI_COMMIT_REF_NAME→ branch nameThis follows the same pass-through pattern as Azure Pipelines (
BUILD_SOURCEBRANCH) and GitHub Actions (GITHUB_REF). No changes to Core.After repository normalisation, the friendly branch name becomes
merge-requests/<iid>/headormerge-requests/<iid>/merge. Users must extend thepull-requestregex inGitVersion.ymlfor GitLab's namespace, because the defaultpull-requests|pull|prpattern does not match:Affected areas:
GitVersion.BuildAgents,GitVersion.BuildAgents.Tests,GitVersion.App.Tests,docs/input/docs/reference/build-servers/gitlab.md.Backward compatibility: Other build agents are unchanged. GitLab branch and tag pipelines behave as before. GitLab MR pipelines gain correct branch detection; they require the regex extension above to apply
pull-requestconfiguration.Related Issue
Resolves #5007
Motivation and Context
In GitLab Merge Request pipelines, the runner checks out a detached HEAD at
refs/merge-requests/<iid>/head, whileCI_COMMIT_REF_NAMEstill points at the source branch. Without readingCI_MERGE_REQUEST_REF_PATH, GitVersion normalised the repository against the wrong branch and calculated the wrong semantic version (for example0.1.0-alpha.5ondeveloperinstead of aPullRequestpre-release label).An earlier contribution added GitLab-specific ref handling in Core; review feedback requested keeping CI/vendor logic in
GitVersion.BuildAgentsonly. This revision addresses that feedback: detection stays inGitLabCi, with documentation and tests for the MR workflow.How Has This Been Tested?
GitLabCiTests): branch resolution precedence (CI_COMMIT_TAG→CI_MERGE_REQUEST_REF_PATH→CI_COMMIT_REF_NAME); MR refsrefs/merge-requests/<iid>/headand/merge; tag overrides MR; fallback whenCI_MERGE_REQUEST_REF_PATHis empty.PullRequestInBuildAgentTest.VerifyGitLabCIPullRequest): end-to-end run withGITLAB_CIandCI_MERGE_REQUEST_REF_PATHset andCI_COMMIT_REF_NAME=FeatureBranch(ignored); assertsFullSemVer1.0.4-PullRequest5.3for bothheadandmergerefs using aGitVersion.ymlwith the GitLabpull-requestregex.dotnet testonGitVersion.BuildAgents.Tests(GitLabCiTests) andGitVersion.App.Tests(VerifyGitLabCIPullRequest),net10.0./verbosity Diagnostic /l console); reproduced the pre-change behaviour (Branch from build environment: developerongittools/gitversion:6.6.1whenCI_MERGE_REQUEST_REF_PATHwas set).Screenshots (if appropriate):
N/A
Checklist: