Skip to content

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

Description

@JDanRibeiro

Prerequisites

  • I have written a descriptive issue title
  • I have searched issues to ensure it has not already been reported

GitVersion package

GitVersion.Tool

GitVersion version

6.6.1

Operating system

Linux

What are you seeing?

In GitLab Merge Request pipelines, GitVersion resolves the build branch from CI_COMMIT_REF_NAME (the source branch name, e.g. developer or feature/foo) and ignores CI_MERGE_REQUEST_REF_PATH (e.g. refs/merge-requests/15/head).

The GitLab runner correctly checks out detached HEAD at refs/merge-requests/<iid>/head; only GitVersion's branch resolution is wrong.

What is expected?

When CI_MERGE_REQUEST_REF_PATH is set, GitLabCi.GetCurrentBranch() should return that ref (pass-through), with precedence:

  1. CI_COMMIT_TAGnull (tag pipeline)
  2. CI_MERGE_REQUEST_REF_PATH → merge request ref
  3. CI_COMMIT_REF_NAME → branch name

This matches how other agents pass platform refs unchanged (Azure BUILD_SOURCEBRANCH, GitHub GITHUB_REF).

After repository normalisation, the friendly branch name should be merge-requests/<iid>/head or merge-requests/<iid>/merge, allowing pull-request configuration to apply when the user extends the regex for GitLab's namespace:

branches:
  pull-request:
    regex: ^merge-requests/(?<Number>\d+)/(head|merge)$

Implementation should stay in GitVersion.BuildAgents only — no GitLab-specific logic in Core.

Steps to Reproduce

  1. Create a GitLab project with a GitVersion.yml using default GitFlow and a pull-request branch configuration.
  2. Open a Merge Request (e.g. developermain).
  3. Run a pipeline job on the MR pipeline:
gitversion:
  rules:
    - if: $CI_MERGE_REQUEST_IID
  image: gittools/gitversion:6.6.1
  script:
    - dotnet-gitversion . /verbosity Diagnostic /l console /output json
  1. Observe that CI_MERGE_REQUEST_REF_PATH is set, but GitVersion logs Branch from build environment: <source-branch>.

References

RepositoryFixture Test

No response

Output log or link to your CI build (if appropriate).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions