Skip to content

Commit

Permalink
Merge pull request #3768 from david-driscoll/fix/github-actions-tag-b…
Browse files Browse the repository at this point in the history
…uild

Re-fixed github actions tag handling
  • Loading branch information
asbjornu committed Nov 17, 2023
2 parents a6b0d92 + 6e7b08e commit d762e0f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
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;
}

0 comments on commit d762e0f

Please sign in to comment.