diff --git a/src/GitVersion.BuildAgents.Tests/Agents/GitHubActionsTests.cs b/src/GitVersion.BuildAgents.Tests/Agents/GitHubActionsTests.cs index 9472bb1104..b5fd21fd0f 100644 --- a/src/GitVersion.BuildAgents.Tests/Agents/GitHubActionsTests.cs +++ b/src/GitVersion.BuildAgents.Tests/Agents/GitHubActionsTests.cs @@ -18,6 +18,7 @@ public void SetUp() this.environment = sp.GetRequiredService(); this.buildServer = sp.GetRequiredService(); this.environment.SetEnvironmentVariable(GitHubActions.EnvironmentVariableName, "true"); + this.environment.SetEnvironmentVariable("GITHUB_REF_TYPE", "branch"); this.githubSetEnvironmentTempFilePath = Path.GetTempFileName(); this.environment.SetEnvironmentVariable(GitHubActions.GitHubSetEnvTempFileEnvironmentVariableName, this.githubSetEnvironmentTempFilePath); @@ -75,13 +76,14 @@ public void GetCurrentBranchShouldHandleBranches() public void GetCurrentBranchShouldHandleTags() { // Arrange + this.environment.SetEnvironmentVariable("GITHUB_REF_TYPE", "tag"); this.environment.SetEnvironmentVariable("GITHUB_REF", "refs/tags/1.0.0"); // Act var result = this.buildServer.GetCurrentBranch(false); // Assert - result.ShouldBe("refs/tags/1.0.0"); + result.ShouldBeNull(); } [Test] diff --git a/src/GitVersion.BuildAgents/Agents/GitHubActions.cs b/src/GitVersion.BuildAgents/Agents/GitHubActions.cs index 380d216862..55ee3a5915 100644 --- a/src/GitVersion.BuildAgents/Agents/GitHubActions.cs +++ b/src/GitVersion.BuildAgents/Agents/GitHubActions.cs @@ -50,7 +50,16 @@ public override void WriteIntegration(Action writer, GitVersionVariable } } - public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("GITHUB_REF"); + + public override string? GetCurrentBranch(bool usingDynamicRepos) + { + // https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables + // GITHUB_REF must be used only for "real" branches, not for tags. + // Bug fix for https://github.com/GitTools/GitVersion/issues/2838 + + var refType = Environment.GetEnvironmentVariable("GITHUB_REF_TYPE") ?? ""; + return refType.Equals("tag", StringComparison.OrdinalIgnoreCase) ? null : Environment.GetEnvironmentVariable("GITHUB_REF"); + } public override bool PreventFetch() => true; }