Skip to content
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

Re-fixed github actions tag handling #3768

Merged
merged 4 commits into from
Nov 17, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public void SetUp()
this.environment = sp.GetRequiredService<IEnvironment>();
this.buildServer = sp.GetRequiredService<GitHubActions>();
this.environment.SetEnvironmentVariable(GitHubActions.EnvironmentVariableName, "true");
this.environment.SetEnvironmentVariable("GITHUB_REF_TYPE", "branch");

this.githubSetEnvironmentTempFilePath = Path.GetTempFileName();
this.environment.SetEnvironmentVariable(GitHubActions.GitHubSetEnvTempFileEnvironmentVariableName, this.githubSetEnvironmentTempFilePath);
Expand Down Expand Up @@ -75,13 +76,14 @@ public void GetCurrentBranchShouldHandleBranches()
public void GetCurrentBranchShouldHandleTags()
{
// Arrange
this.environment.SetEnvironmentVariable("GITHUB_REF_TYPE", "tag");
this.environment.SetEnvironmentVariable("GITHUB_REF", "refs/tags/1.0.0");

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

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

[Test]
Expand Down
11 changes: 10 additions & 1 deletion src/GitVersion.BuildAgents/Agents/GitHubActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ public override void WriteIntegration(Action<string?> writer, GitVersionVariable
}
}

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.
// Bug fix for https://github.com/GitTools/GitVersion/issues/2838

var refType = Environment.GetEnvironmentVariable("GITHUB_REF_TYPE") ?? "";
return refType.Equals("tag", StringComparison.OrdinalIgnoreCase) ? null : Environment.GetEnvironmentVariable("GITHUB_REF");
}

public override bool PreventFetch() => true;
}