Skip to content

Commit 721a338

Browse files
committed
Use GITHUB_REF only for CI builds on branches
1 parent 12e23f7 commit 721a338

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void GetCurrentBranchShouldHandleTags()
8383
var result = this.buildServer.GetCurrentBranch(false);
8484

8585
// Assert
86-
result.ShouldBe("refs/tags/1.0.0");
86+
result.ShouldBeNull();
8787
}
8888

8989
[Test]
@@ -96,7 +96,7 @@ public void GetCurrentBranchShouldHandlePullRequests()
9696
var result = this.buildServer.GetCurrentBranch(false);
9797

9898
// Assert
99-
result.ShouldBe("refs/pull/1/merge");
99+
result.ShouldBeNull();
100100
}
101101

102102
[Test]

src/GitVersion.Core/BuildAgents/GitHubActions.cs

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

53-
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("GITHUB_REF");
53+
public override string? GetCurrentBranch(bool usingDynamicRepos)
54+
{
55+
// https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
56+
// GITHUB_REF must be used only for "real" branches, not for tags and pull requests.
57+
// Bug fix for https://github.com/GitTools/GitVersion/issues/2838
58+
string? githubRef = Environment.GetEnvironmentVariable("GITHUB_REF");
59+
if (githubRef != null && githubRef.StartsWith("refs/heads/"))
60+
{
61+
return githubRef;
62+
}
63+
return null;
64+
}
5465

5566
public override bool PreventFetch() => true;
5667
}

0 commit comments

Comments
 (0)