Skip to content

Commit d762e0f

Browse files
authored
Merge pull request #3768 from david-driscoll/fix/github-actions-tag-build
Re-fixed github actions tag handling
2 parents a6b0d92 + 6e7b08e commit d762e0f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/GitVersion.BuildAgents.Tests/Agents/GitHubActionsTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public void SetUp()
1818
this.environment = sp.GetRequiredService<IEnvironment>();
1919
this.buildServer = sp.GetRequiredService<GitHubActions>();
2020
this.environment.SetEnvironmentVariable(GitHubActions.EnvironmentVariableName, "true");
21+
this.environment.SetEnvironmentVariable("GITHUB_REF_TYPE", "branch");
2122

2223
this.githubSetEnvironmentTempFilePath = Path.GetTempFileName();
2324
this.environment.SetEnvironmentVariable(GitHubActions.GitHubSetEnvTempFileEnvironmentVariableName, this.githubSetEnvironmentTempFilePath);
@@ -75,13 +76,14 @@ public void GetCurrentBranchShouldHandleBranches()
7576
public void GetCurrentBranchShouldHandleTags()
7677
{
7778
// Arrange
79+
this.environment.SetEnvironmentVariable("GITHUB_REF_TYPE", "tag");
7880
this.environment.SetEnvironmentVariable("GITHUB_REF", "refs/tags/1.0.0");
7981

8082
// Act
8183
var result = this.buildServer.GetCurrentBranch(false);
8284

8385
// Assert
84-
result.ShouldBe("refs/tags/1.0.0");
86+
result.ShouldBeNull();
8587
}
8688

8789
[Test]

src/GitVersion.BuildAgents/Agents/GitHubActions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,16 @@ public override void WriteIntegration(Action<string?> writer, GitVersionVariable
5050
}
5151
}
5252

53-
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("GITHUB_REF");
53+
54+
public override string? GetCurrentBranch(bool usingDynamicRepos)
55+
{
56+
// https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
57+
// GITHUB_REF must be used only for "real" branches, not for tags.
58+
// Bug fix for https://github.com/GitTools/GitVersion/issues/2838
59+
60+
var refType = Environment.GetEnvironmentVariable("GITHUB_REF_TYPE") ?? "";
61+
return refType.Equals("tag", StringComparison.OrdinalIgnoreCase) ? null : Environment.GetEnvironmentVariable("GITHUB_REF");
62+
}
5463

5564
public override bool PreventFetch() => true;
5665
}

0 commit comments

Comments
 (0)