Skip to content

Use GITHUB_REF only for CI builds on branches #3033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/GitVersion.Core.Tests/BuildAgents/AzurePipelinesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,43 @@ public void AzurePipelinesBuildNumberWithSemVer(string buildNumberFormat, string
var logMessage = this.buildServer.GenerateSetVersionMessage(vars);
logMessage.ShouldBe(logPrefix + expectedBuildNumber);
}

[Test]
public void GetCurrentBranchShouldHandleBranches()
{
// Arrange
this.environment.SetEnvironmentVariable("BUILD_SOURCEBRANCH", $"refs/heads/{MainBranch}");

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

// Assert
result.ShouldBe($"refs/heads/{MainBranch}");
}

[Test]
public void GetCurrentBranchShouldHandleTags()
{
// Arrange
this.environment.SetEnvironmentVariable("BUILD_SOURCEBRANCH", "refs/tags/1.0.0");

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

// Assert
result.ShouldBeNull();
}

[Test]
public void GetCurrentBranchShouldHandlePullRequests()
{
// Arrange
this.environment.SetEnvironmentVariable("BUILD_SOURCEBRANCH", "refs/pull/1/merge");

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

// Assert
result.ShouldBeNull();
}
}
2 changes: 1 addition & 1 deletion src/GitVersion.Core.Tests/BuildAgents/BuildKiteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void GetCurrentBranchShouldHandlePullRequests()
var result = this.buildServer.GetCurrentBranch(false);

// Assert
result.ShouldBe("refs/pull/55/head");
result.ShouldBeNull();
}

[Test]
Expand Down
26 changes: 26 additions & 0 deletions src/GitVersion.Core.Tests/BuildAgents/CodeBuildTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,30 @@ private void AssertVariablesAreWrittenToFile(string file)
props.ShouldContain("GitVersion_Major=1");
props.ShouldContain("GitVersion_Minor=2");
}

[Test]
public void GetCurrentBranchShouldHandleTags()
{
// Arrange
this.environment.SetEnvironmentVariable("CODEBUILD_WEBHOOK_HEAD_REF", "refs/tags/1.0.0");

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

// Assert
result.ShouldBeNull();
}

[Test]
public void GetCurrentBranchShouldHandlePullRequests()
{
// Arrange
this.environment.SetEnvironmentVariable("CODEBUILD_SOURCE_VERSION", "refs/pull/1/merge");

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

// Assert
result.ShouldBeNull();
}
}
4 changes: 2 additions & 2 deletions src/GitVersion.Core.Tests/BuildAgents/GitHubActionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void GetCurrentBranchShouldHandleTags()
var result = this.buildServer.GetCurrentBranch(false);

// Assert
result.ShouldBe("refs/tags/1.0.0");
result.ShouldBeNull();
}

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

// Assert
result.ShouldBe("refs/pull/1/merge");
result.ShouldBeNull();
}

[Test]
Expand Down
7 changes: 3 additions & 4 deletions src/GitVersion.Core.Tests/BuildAgents/SpaceAutomationTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using GitVersion;
using GitVersion.BuildAgents;
using GitVersion.Core.Tests.Helpers;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using Shouldly;

namespace GitVersionCore.Tests.BuildAgents;
namespace GitVersion.Core.Tests.BuildAgents;

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

// Assert
result.ShouldBe("refs/tags/1.0.0");
result.ShouldBeNull();
}

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

// Assert
result.ShouldBe("refs/pull/1/merge");
result.ShouldBeNull();
}

[Test]
Expand Down
12 changes: 11 additions & 1 deletion src/GitVersion.Core/BuildAgents/AzurePipelines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ public override string[] GenerateSetParameterMessage(string name, string value)
$"##vso[task.setvariable variable=GitVersion.{name};isOutput=true]{value}"
};

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

public override bool PreventFetch() => true;

Expand Down
6 changes: 3 additions & 3 deletions src/GitVersion.Core/BuildAgents/BuildKite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public override string[] GenerateSetParameterMessage(string name, string value)
}
else
{
// For pull requests BUILDKITE_BRANCH refers to the head, so adjust the
// branch name for pull request versioning to function as expected
return string.Format("refs/pull/{0}/head", pullRequest);
// To align the behavior with the other BuildAgent implementations
// we return here also null.
return null;
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/GitVersion.Core/BuildAgents/CodeBuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@ public override string[] GenerateSetParameterMessage(string name, string value)

public override string? GetCurrentBranch(bool usingDynamicRepos)
{
var currentBranch = Environment.GetEnvironmentVariable(WebHookEnvironmentVariableName);

return currentBranch.IsNullOrEmpty() ? Environment.GetEnvironmentVariable(SourceVersionEnvironmentVariableName) : currentBranch;
string? branchName = Environment.GetEnvironmentVariable(WebHookEnvironmentVariableName);
if (string.IsNullOrEmpty(branchName))
{
branchName = Environment.GetEnvironmentVariable(SourceVersionEnvironmentVariableName);
}

if (branchName != null && branchName.StartsWith("refs/heads/"))
{
return branchName;
}
return null;
}

public override void WriteIntegration(Action<string?> writer, VersionVariables variables, bool updateBuildNumber = true)
Expand Down
13 changes: 12 additions & 1 deletion src/GitVersion.Core/BuildAgents/GitHubActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,18 @@ public override void WriteIntegration(Action<string?> writer, VersionVariables v
}
}

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 and pull requests.
// Bug fix for https://github.com/GitTools/GitVersion/issues/2838
string? githubRef = Environment.GetEnvironmentVariable("GITHUB_REF");
if (githubRef != null && githubRef.StartsWith("refs/heads/"))
{
return githubRef;
}
return null;
}

public override bool PreventFetch() => true;
}
4 changes: 3 additions & 1 deletion src/GitVersion.Core/BuildAgents/GitLabCi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public override string[] GenerateSetParameterMessage(string name, string value)
$"GitVersion_{name}={value}"
};

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

public override bool PreventFetch() => true;

Expand Down
10 changes: 9 additions & 1 deletion src/GitVersion.Core/BuildAgents/SpaceAutomation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ public SpaceAutomation(IEnvironment environment, ILog log) : base(environment, l

protected override string EnvironmentVariable => EnvironmentVariableName;

public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("JB_SPACE_GIT_BRANCH");
public override string? GetCurrentBranch(bool usingDynamicRepos)
{
string? branchName = Environment.GetEnvironmentVariable("JB_SPACE_GIT_BRANCH");
if (branchName != null && branchName.StartsWith("refs/heads/"))
{
return branchName;
}
return null;
}

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

Expand Down