From bae738e55cc7949d0635158a52e822e46de2d7c0 Mon Sep 17 00:00:00 2001 From: AlphaYankee <54741936+AlphaYankee@users.noreply.github.com> Date: Thu, 29 Aug 2024 23:53:18 +0200 Subject: [PATCH 1/2] Do not return the tag as branch name for GitLab CI In case of a tag pipeline, return null as branch name instead of the tag --- src/GitVersion.BuildAgents/Agents/GitLabCi.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/GitVersion.BuildAgents/Agents/GitLabCi.cs b/src/GitVersion.BuildAgents/Agents/GitLabCi.cs index 0956084ab7..23c0e912a1 100644 --- a/src/GitVersion.BuildAgents/Agents/GitLabCi.cs +++ b/src/GitVersion.BuildAgents/Agents/GitLabCi.cs @@ -21,7 +21,18 @@ public override string[] GenerateSetParameterMessage(string name, string? value) $"GitVersion_{name}={value}" ]; - public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("CI_COMMIT_REF_NAME"); + public override string? GetCurrentBranch(bool usingDynamicRepos) + { + // CI_COMMIT_REF_NAME can contain either the branch or the tag + // See https://docs.gitlab.com/ee/ci/variables/predefined_variables.html + + // CI_COMMIT_TAG is only available in tag pipelines, + // so we can exit if CI_COMMIT_REF_NAME would return the tag + if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CI_COMMIT_TAG"))) + return null; + + return Environment.GetEnvironmentVariable("CI_COMMIT_REF_NAME"); + } public override bool PreventFetch() => true; From e4cf4342a1cd5d3f126038af3f5eb1d4a4b51537 Mon Sep 17 00:00:00 2001 From: Bi0T1N Date: Fri, 31 Jan 2025 21:25:51 +0100 Subject: [PATCH 2/2] Add tests for GitLab CI to ignore branch name for tags --- .../Agents/GitLabCiTests.cs | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/src/GitVersion.BuildAgents.Tests/Agents/GitLabCiTests.cs b/src/GitVersion.BuildAgents.Tests/Agents/GitLabCiTests.cs index aa1e2ec6b0..2e5eba6ea1 100644 --- a/src/GitVersion.BuildAgents.Tests/Agents/GitLabCiTests.cs +++ b/src/GitVersion.BuildAgents.Tests/Agents/GitLabCiTests.cs @@ -9,16 +9,22 @@ namespace GitVersion.Agents.Tests; [TestFixture] public class GitLabCiTests : TestBase { - private GitLabCi buildServer; private IServiceProvider sp; + private GitLabCi buildServer; + private IEnvironment environment; [SetUp] public void SetUp() { this.sp = ConfigureServices(services => services.AddSingleton()); this.buildServer = this.sp.GetRequiredService(); + this.environment = this.sp.GetRequiredService(); + this.environment.SetEnvironmentVariable(GitLabCi.EnvironmentVariableName, "true"); } + [TearDown] + public void TearDown() => this.environment.SetEnvironmentVariable(GitLabCi.EnvironmentVariableName, null); + [Test] public void GenerateSetVersionMessageReturnsVersionAsIsAlthoughThisIsNotUsedByJenkins() { @@ -34,6 +40,55 @@ public void GenerateMessageTest() generatedParameterMessages[0].ShouldBe("GitVersion_name=value"); } + [TestCase("main", "main")] + [TestCase("dev", "dev")] + [TestCase("development", "development")] + [TestCase("my_cool_feature", "my_cool_feature")] + [TestCase("#3-change_projectname", "#3-change_projectname")] + public void GetCurrentBranchShouldHandleBranches(string branchName, string expectedResult) + { + this.environment.SetEnvironmentVariable("CI_COMMIT_REF_NAME", branchName); + + var result = this.buildServer.GetCurrentBranch(false); + + result.ShouldBe(expectedResult); + } + + [TestCase("main", "", "main")] + [TestCase("v1.0.0", "v1.0.0", null)] + [TestCase("development", "", "development")] + [TestCase("v1.2.1", "v1.2.1", null)] + public void GetCurrentBranchShouldHandleTags(string branchName, string commitTag, string? expectedResult) + { + this.environment.SetEnvironmentVariable("CI_COMMIT_REF_NAME", branchName); + this.environment.SetEnvironmentVariable("CI_COMMIT_TAG", commitTag); // only set in pipelines for tags + + var result = this.buildServer.GetCurrentBranch(false); + + if (!string.IsNullOrEmpty(expectedResult)) + { + result.ShouldBe(expectedResult); + } + else + { + result.ShouldBeNull(); + } + } + + [TestCase("main", "main")] + [TestCase("dev", "dev")] + [TestCase("development", "development")] + [TestCase("my_cool_feature", "my_cool_feature")] + [TestCase("#3-change_projectname", "#3-change_projectname")] + public void GetCurrentBranchShouldHandlePullRequests(string branchName, string expectedResult) + { + this.environment.SetEnvironmentVariable("CI_COMMIT_REF_NAME", branchName); + + var result = this.buildServer.GetCurrentBranch(false); + + result.ShouldBe(expectedResult); + } + [Test] public void WriteAllVariablesToTheTextWriter() {