Skip to content

Commit 6331ced

Browse files
authored
Merge pull request #3033 from ThomasPiskol/main
Use GITHUB_REF only for CI builds on branches
2 parents 456e270 + 535f99b commit 6331ced

11 files changed

+120
-17
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,43 @@ public void AzurePipelinesBuildNumberWithSemVer(string buildNumberFormat, string
8080
var logMessage = this.buildServer.GenerateSetVersionMessage(vars);
8181
logMessage.ShouldBe(logPrefix + expectedBuildNumber);
8282
}
83+
84+
[Test]
85+
public void GetCurrentBranchShouldHandleBranches()
86+
{
87+
// Arrange
88+
this.environment.SetEnvironmentVariable("BUILD_SOURCEBRANCH", $"refs/heads/{MainBranch}");
89+
90+
// Act
91+
var result = this.buildServer.GetCurrentBranch(false);
92+
93+
// Assert
94+
result.ShouldBe($"refs/heads/{MainBranch}");
95+
}
96+
97+
[Test]
98+
public void GetCurrentBranchShouldHandleTags()
99+
{
100+
// Arrange
101+
this.environment.SetEnvironmentVariable("BUILD_SOURCEBRANCH", "refs/tags/1.0.0");
102+
103+
// Act
104+
var result = this.buildServer.GetCurrentBranch(false);
105+
106+
// Assert
107+
result.ShouldBeNull();
108+
}
109+
110+
[Test]
111+
public void GetCurrentBranchShouldHandlePullRequests()
112+
{
113+
// Arrange
114+
this.environment.SetEnvironmentVariable("BUILD_SOURCEBRANCH", "refs/pull/1/merge");
115+
116+
// Act
117+
var result = this.buildServer.GetCurrentBranch(false);
118+
119+
// Assert
120+
result.ShouldBeNull();
121+
}
83122
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void GetCurrentBranchShouldHandlePullRequests()
7272
var result = this.buildServer.GetCurrentBranch(false);
7373

7474
// Assert
75-
result.ShouldBe("refs/pull/55/head");
75+
result.ShouldBeNull();
7676
}
7777

7878
[Test]

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,30 @@ private void AssertVariablesAreWrittenToFile(string file)
102102
props.ShouldContain("GitVersion_Major=1");
103103
props.ShouldContain("GitVersion_Minor=2");
104104
}
105+
106+
[Test]
107+
public void GetCurrentBranchShouldHandleTags()
108+
{
109+
// Arrange
110+
this.environment.SetEnvironmentVariable("CODEBUILD_WEBHOOK_HEAD_REF", "refs/tags/1.0.0");
111+
112+
// Act
113+
var result = this.buildServer.GetCurrentBranch(false);
114+
115+
// Assert
116+
result.ShouldBeNull();
117+
}
118+
119+
[Test]
120+
public void GetCurrentBranchShouldHandlePullRequests()
121+
{
122+
// Arrange
123+
this.environment.SetEnvironmentVariable("CODEBUILD_SOURCE_VERSION", "refs/pull/1/merge");
124+
125+
// Act
126+
var result = this.buildServer.GetCurrentBranch(false);
127+
128+
// Assert
129+
result.ShouldBeNull();
130+
}
105131
}

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.Tests/BuildAgents/SpaceAutomationTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using GitVersion;
21
using GitVersion.BuildAgents;
32
using GitVersion.Core.Tests.Helpers;
43
using Microsoft.Extensions.DependencyInjection;
54
using NUnit.Framework;
65
using Shouldly;
76

8-
namespace GitVersionCore.Tests.BuildAgents;
7+
namespace GitVersion.Core.Tests.BuildAgents;
98

109
[TestFixture]
1110
public class SpaceAutomationTests : TestBase
@@ -71,7 +70,7 @@ public void GetCurrentBranchShouldHandleTags()
7170
var result = this.buildServer.GetCurrentBranch(false);
7271

7372
// Assert
74-
result.ShouldBe("refs/tags/1.0.0");
73+
result.ShouldBeNull();
7574
}
7675

7776
[Test]
@@ -84,7 +83,7 @@ public void GetCurrentBranchShouldHandlePullRequests()
8483
var result = this.buildServer.GetCurrentBranch(false);
8584

8685
// Assert
87-
result.ShouldBe("refs/pull/1/merge");
86+
result.ShouldBeNull();
8887
}
8988

9089
[Test]

src/GitVersion.Core/BuildAgents/AzurePipelines.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ public override string[] GenerateSetParameterMessage(string name, string value)
2121
$"##vso[task.setvariable variable=GitVersion.{name};isOutput=true]{value}"
2222
};
2323

24-
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("BUILD_SOURCEBRANCH");
24+
public override string? GetCurrentBranch(bool usingDynamicRepos)
25+
{
26+
// https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables
27+
// BUILD_SOURCEBRANCH does not contain the branch name if the build was triggered by a tag or pull request.
28+
string? branchName = Environment.GetEnvironmentVariable("BUILD_SOURCEBRANCH");
29+
if (branchName != null && branchName.StartsWith("refs/heads/"))
30+
{
31+
return branchName;
32+
}
33+
return null;
34+
}
2535

2636
public override bool PreventFetch() => true;
2737

src/GitVersion.Core/BuildAgents/BuildKite.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public override string[] GenerateSetParameterMessage(string name, string value)
3030
}
3131
else
3232
{
33-
// For pull requests BUILDKITE_BRANCH refers to the head, so adjust the
34-
// branch name for pull request versioning to function as expected
35-
return string.Format("refs/pull/{0}/head", pullRequest);
33+
// To align the behavior with the other BuildAgent implementations
34+
// we return here also null.
35+
return null;
3636
}
3737
}
3838

src/GitVersion.Core/BuildAgents/CodeBuild.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ public override string[] GenerateSetParameterMessage(string name, string value)
2525

2626
public override string? GetCurrentBranch(bool usingDynamicRepos)
2727
{
28-
var currentBranch = Environment.GetEnvironmentVariable(WebHookEnvironmentVariableName);
29-
30-
return currentBranch.IsNullOrEmpty() ? Environment.GetEnvironmentVariable(SourceVersionEnvironmentVariableName) : currentBranch;
28+
string? branchName = Environment.GetEnvironmentVariable(WebHookEnvironmentVariableName);
29+
if (string.IsNullOrEmpty(branchName))
30+
{
31+
branchName = Environment.GetEnvironmentVariable(SourceVersionEnvironmentVariableName);
32+
}
33+
34+
if (branchName != null && branchName.StartsWith("refs/heads/"))
35+
{
36+
return branchName;
37+
}
38+
return null;
3139
}
3240

3341
public override void WriteIntegration(Action<string?> writer, VersionVariables variables, bool updateBuildNumber = true)

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
}

src/GitVersion.Core/BuildAgents/GitLabCi.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public override string[] GenerateSetParameterMessage(string name, string value)
2222
$"GitVersion_{name}={value}"
2323
};
2424

25-
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("CI_COMMIT_REF_NAME");
25+
// According to https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
26+
// the CI_COMMIT_BRANCH environment variable must be used.
27+
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("CI_COMMIT_BRANCH");
2628

2729
public override bool PreventFetch() => true;
2830

src/GitVersion.Core/BuildAgents/SpaceAutomation.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ public SpaceAutomation(IEnvironment environment, ILog log) : base(environment, l
1313

1414
protected override string EnvironmentVariable => EnvironmentVariableName;
1515

16-
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("JB_SPACE_GIT_BRANCH");
16+
public override string? GetCurrentBranch(bool usingDynamicRepos)
17+
{
18+
string? branchName = Environment.GetEnvironmentVariable("JB_SPACE_GIT_BRANCH");
19+
if (branchName != null && branchName.StartsWith("refs/heads/"))
20+
{
21+
return branchName;
22+
}
23+
return null;
24+
}
1725

1826
public override string[] GenerateSetParameterMessage(string name, string value) => Array.Empty<string>();
1927

0 commit comments

Comments
 (0)