Skip to content

Commit

Permalink
Merge pull request #3085 from GitTools/revert-3033-main
Browse files Browse the repository at this point in the history
Revert "Use GITHUB_REF only for CI builds on branches"
  • Loading branch information
arturcic authored Apr 14, 2022
2 parents 3892733 + 4cdea84 commit b17d33e
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 120 deletions.
39 changes: 0 additions & 39 deletions src/GitVersion.Core.Tests/BuildAgents/AzurePipelinesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,43 +80,4 @@ 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.ShouldBeNull();
result.ShouldBe("refs/pull/55/head");
}

[Test]
Expand Down
26 changes: 0 additions & 26 deletions src/GitVersion.Core.Tests/BuildAgents/CodeBuildTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,30 +102,4 @@ 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.ShouldBeNull();
result.ShouldBe("refs/tags/1.0.0");
}

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

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

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

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

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

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

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

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

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

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 string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("BUILD_SOURCEBRANCH");

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
{
// To align the behavior with the other BuildAgent implementations
// we return here also null.
return null;
// 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);
}
}

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

public override string? GetCurrentBranch(bool usingDynamicRepos)
{
string? branchName = Environment.GetEnvironmentVariable(WebHookEnvironmentVariableName);
if (string.IsNullOrEmpty(branchName))
{
branchName = Environment.GetEnvironmentVariable(SourceVersionEnvironmentVariableName);
}

if (branchName != null && branchName.StartsWith("refs/heads/"))
{
return branchName;
}
return null;
var currentBranch = Environment.GetEnvironmentVariable(WebHookEnvironmentVariableName);

return currentBranch.IsNullOrEmpty() ? Environment.GetEnvironmentVariable(SourceVersionEnvironmentVariableName) : currentBranch;
}

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

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 string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("GITHUB_REF");

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

// 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 string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("CI_COMMIT_REF_NAME");

public override bool PreventFetch() => true;

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

protected override string EnvironmentVariable => EnvironmentVariableName;

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? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("JB_SPACE_GIT_BRANCH");

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

Expand Down

0 comments on commit b17d33e

Please sign in to comment.