Skip to content

feat(buildagents): read CI_MERGE_REQUEST_REF_PATH in GitLabCi#5008

Merged
mergify[bot] merged 1 commit into
GitTools:mainfrom
JDanRibeiro:feature/gitlab-detect-merge-request
Jul 2, 2026
Merged

feat(buildagents): read CI_MERGE_REQUEST_REF_PATH in GitLabCi#5008
mergify[bot] merged 1 commit into
GitTools:mainfrom
JDanRibeiro:feature/gitlab-detect-merge-request

Conversation

@JDanRibeiro

@JDanRibeiro JDanRibeiro commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Description

This PR adds support for GitLab CI Merge Request pipelines by reading CI_MERGE_REQUEST_REF_PATH in the GitLabCi build agent.

In MR pipelines, GitLab sets CI_MERGE_REQUEST_REF_PATH (for example refs/merge-requests/15/head or refs/merge-requests/15/merge). Previously, GitVersion used only CI_COMMIT_REF_NAME, which contains the source branch name (for example developer or feature/foo). Version calculation therefore followed feature/develop configuration instead of pull-request.

The change is limited to GitVersion.BuildAgents. In GetCurrentBranch(), precedence is now:

  1. CI_COMMIT_TAGnull (tag pipeline)
  2. CI_MERGE_REQUEST_REF_PATH → merge request ref (pass-through, unchanged)
  3. CI_COMMIT_REF_NAME → branch name

This 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>/head or merge-requests/<iid>/merge. Users must extend the pull-request regex in GitVersion.yml for GitLab's namespace, because the default pull-requests|pull|pr pattern does not match:

workflow: GitFlow/v1
branches:
  pull-request:
    regex: ^merge-requests/(?<Number>\d+)/(head|merge)$
    label: PullRequest{Number}

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-request configuration.

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, while CI_COMMIT_REF_NAME still points at the source branch. Without reading CI_MERGE_REQUEST_REF_PATH, GitVersion normalised the repository against the wrong branch and calculated the wrong semantic version (for example 0.1.0-alpha.5 on developer instead of a PullRequest pre-release label).

An earlier contribution added GitLab-specific ref handling in Core; review feedback requested keeping CI/vendor logic in GitVersion.BuildAgents only. This revision addresses that feedback: detection stays in GitLabCi, with documentation and tests for the MR workflow.

How Has This Been Tested?

  • Unit tests (GitLabCiTests): branch resolution precedence (CI_COMMIT_TAGCI_MERGE_REQUEST_REF_PATHCI_COMMIT_REF_NAME); MR refs refs/merge-requests/<iid>/head and /merge; tag overrides MR; fallback when CI_MERGE_REQUEST_REF_PATH is empty.
  • Integration tests (PullRequestInBuildAgentTest.VerifyGitLabCIPullRequest): end-to-end run with GITLAB_CI and CI_MERGE_REQUEST_REF_PATH set and CI_COMMIT_REF_NAME=FeatureBranch (ignored); asserts FullSemVer 1.0.4-PullRequest5.3 for both head and merge refs using a GitVersion.yml with the GitLab pull-request regex.
  • Local: dotnet test on GitVersion.BuildAgents.Tests (GitLabCiTests) and GitVersion.App.Tests (VerifyGitLabCIPullRequest), net10.0.
  • Manual: GitLab CI MR pipeline with diagnostic logging (/verbosity Diagnostic /l console); reproduced the pre-change behaviour (Branch from build environment: developer on gittools/gitversion:6.6.1 when CI_MERGE_REQUEST_REF_PATH was set).

Screenshots (if appropriate):

N/A

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Open with GitKraken

Copilot AI review requested due to automatic review settings July 2, 2026 03:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_TAGCI_MERGE_REQUEST_REF_PATHCI_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-request regex override for merge-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.

Comment on lines +152 to +157
private const string GitLabMergeRequestPullRequestConfig = """
workflow: GitFlow/v1
branches:
pull-request:
regex: ^merge-requests/(?<Number>\d+)/(head|merge)$
""";
Comment on lines +25 to +29
private static readonly string[] GitLabMergeRequestRefs =
[
"refs/merge-requests/5/head",
"refs/merge-requests/5/merge"
];
@arturcic

arturcic commented Jul 2, 2026

Copy link
Copy Markdown
Member

@JDanRibeiro thanks for taking a second shot, this looks much better, please rebase your branch onto main then I will have a look, thanks

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.
@JDanRibeiro JDanRibeiro force-pushed the feature/gitlab-detect-merge-request branch from 70b2161 to 65cbfc9 Compare July 2, 2026 12:11
@sonarqubecloud

sonarqubecloud Bot commented Jul 2, 2026

Copy link
Copy Markdown

@JDanRibeiro

Copy link
Copy Markdown
Contributor Author

@JDanRibeiro thanks for taking a second shot, this looks much better, please rebase your branch onto main then I will have a look, thanks

thanks, the rebase has been executed.

@arturcic arturcic enabled auto-merge July 2, 2026 12:38
@mergify

mergify Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Queued — the merge queue status continues in this comment ↓.

@arturcic

arturcic commented Jul 2, 2026

Copy link
Copy Markdown
Member

@Mergifyio queue

@mergify

mergify Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • Entered queue2026-07-02 12:56 UTC · Rule: default · triggered by @arturcic with the @mergifyio queue command
  • Checks skipped · PR is already up-to-date
  • Merged2026-07-02 12:57 UTC · at 65cbfc9832d4692b74db89edf99a6f6ec82908a0 · merge

This pull request spent 38 seconds in the queue, including 3 seconds running CI.

Required conditions to merge
  • github-review-approved [🛡 GitHub repository ruleset rule main branch rule]
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @github-actions/DotNet Format
    • check-neutral = @github-actions/DotNet Format
    • check-skipped = @github-actions/DotNet Format
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @github-actions/Release
    • check-neutral = @github-actions/Release
    • check-skipped = @github-actions/Release
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @github-actions/Build & Test (new-cli)
    • check-neutral = @github-actions/Build & Test (new-cli)
    • check-skipped = @github-actions/Build & Test (new-cli)
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @github-actions/Build & Package / macos-26
    • check-neutral = @github-actions/Build & Package / macos-26
    • check-skipped = @github-actions/Build & Package / macos-26
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @github-actions/Build & Package / ubuntu-24.04
    • check-neutral = @github-actions/Build & Package / ubuntu-24.04
    • check-skipped = @github-actions/Build & Package / ubuntu-24.04
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @github-actions/Build & Package / windows-2025-vs2026
    • check-neutral = @github-actions/Build & Package / windows-2025-vs2026
    • check-skipped = @github-actions/Build & Package / windows-2025-vs2026
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @github-actions/Test / macos-26 - net10.0
    • check-neutral = @github-actions/Test / macos-26 - net10.0
    • check-skipped = @github-actions/Test / macos-26 - net10.0
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @github-actions/Test / macos-26 - net8.0
    • check-neutral = @github-actions/Test / macos-26 - net8.0
    • check-skipped = @github-actions/Test / macos-26 - net8.0
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @github-actions/Test / macos-26 - net9.0
    • check-neutral = @github-actions/Test / macos-26 - net9.0
    • check-skipped = @github-actions/Test / macos-26 - net9.0
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @github-actions/Test / ubuntu-24.04 - net10.0
    • check-neutral = @github-actions/Test / ubuntu-24.04 - net10.0
    • check-skipped = @github-actions/Test / ubuntu-24.04 - net10.0
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @github-actions/Test / ubuntu-24.04 - net8.0
    • check-neutral = @github-actions/Test / ubuntu-24.04 - net8.0
    • check-skipped = @github-actions/Test / ubuntu-24.04 - net8.0
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @github-actions/Test / ubuntu-24.04 - net9.0
    • check-neutral = @github-actions/Test / ubuntu-24.04 - net9.0
    • check-skipped = @github-actions/Test / ubuntu-24.04 - net9.0
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @github-actions/Test / windows-2025-vs2026 - net10.0
    • check-neutral = @github-actions/Test / windows-2025-vs2026 - net10.0
    • check-skipped = @github-actions/Test / windows-2025-vs2026 - net10.0
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @github-actions/Test / windows-2025-vs2026 - net8.0
    • check-neutral = @github-actions/Test / windows-2025-vs2026 - net8.0
    • check-skipped = @github-actions/Test / windows-2025-vs2026 - net8.0
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @github-actions/Test / windows-2025-vs2026 - net9.0
    • check-neutral = @github-actions/Test / windows-2025-vs2026 - net9.0
    • check-skipped = @github-actions/Test / windows-2025-vs2026 - net9.0
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-neutral = Mergify Merge Protections
    • check-skipped = Mergify Merge Protections
    • check-success = Mergify Merge Protections
  • any of [🛡 GitHub repository ruleset rule main branch rule]:
    • check-success = @sonarqubecloud/SonarCloud Code Analysis
    • check-neutral = @sonarqubecloud/SonarCloud Code Analysis
    • check-skipped = @sonarqubecloud/SonarCloud Code Analysis

@mergify mergify Bot added the queued label Jul 2, 2026
mergify Bot added a commit that referenced this pull request Jul 2, 2026
@mergify mergify Bot merged commit ca68290 into GitTools:main Jul 2, 2026
125 checks passed
@mergify mergify Bot removed the queued label Jul 2, 2026
@mergify

mergify Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Thank you @JDanRibeiro for your contribution!

This was referenced Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ISSUE]: GitLab CI: Merge Request pipelines use source branch instead of CI_MERGE_REQUEST_REF_PATH

3 participants