Skip to content

Commit 032ecff

Browse files
Add extra logic to handle tag refs in GithubActions
1 parent afd3e0a commit 032ecff

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

Lines changed: 7 additions & 2 deletions
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,26 +76,30 @@ 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]
8890
public void GetCurrentBranchShouldHandlePullRequests()
8991
{
9092
// Arrange
93+
this.environment.SetEnvironmentVariable("GITHUB_EVENT_NAME", "pull_request");
94+
this.environment.SetEnvironmentVariable("GITHUB_HEAD_REF", "some-branch");
95+
this.environment.SetEnvironmentVariable("GITHUB_BASE_REF", MainBranch);
9196
this.environment.SetEnvironmentVariable("GITHUB_REF", "refs/pull/1/merge");
9297

9398
// Act
9499
var result = this.buildServer.GetCurrentBranch(false);
95100

96101
// Assert
97-
result.ShouldBe("refs/pull/1/merge");
102+
result.ShouldBe("some-branch");
98103
}
99104

100105
[Test]

src/GitVersion.BuildAgents/Agents/GitHubActions.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,28 @@ 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+
var refType = Environment.GetEnvironmentVariable("GITHUB_REF_TYPE") ?? "";
57+
var eventName = Environment.GetEnvironmentVariable("GITHUB_EVENT_NAME") ?? "";
58+
// https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
59+
// GITHUB_REF must be used only for "real" branches, not for tags.
60+
// Bug fix for https://github.com/GitTools/GitVersion/issues/2838
61+
62+
// pull_request or pull_request_target
63+
if (eventName.StartsWith("pull_request", StringComparison.OrdinalIgnoreCase))
64+
{
65+
return Environment.GetEnvironmentVariable("GITHUB_HEAD_REF");
66+
}
67+
68+
if (refType.Equals("tag", StringComparison.OrdinalIgnoreCase))
69+
{
70+
return null;
71+
}
72+
73+
return Environment.GetEnvironmentVariable("GITHUB_REF");
74+
}
5475

5576
public override bool PreventFetch() => true;
5677
}

0 commit comments

Comments
 (0)