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