Skip to content

Fix tag normalization for Azure Pipelines #4400

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
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.BuildAgents.Tests/Agents/AzurePipelinesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,43 @@ public void AzurePipelinesBuildNumberWithSemVer(string buildNumberFormat, string
var logMessage = this.buildServer.SetBuildNumber(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.ShouldBe("refs/pull/1/merge");
}
}
20 changes: 18 additions & 2 deletions src/GitVersion.BuildAgents/Agents/AzurePipelines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,24 @@ public override string[] SetOutputVariables(string name, string? value) =>
$"##vso[task.setvariable variable=GitVersion.{name};isOutput=true]{value}"
];

public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("GIT_BRANCH")
?? Environment.GetEnvironmentVariable("BUILD_SOURCEBRANCH");
public override string? GetCurrentBranch(bool usingDynamicRepos)
{
var gitBranch = Environment.GetEnvironmentVariable("GIT_BRANCH");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does this variable come from? I can only find references that refer to Jenkins

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an environment variable we've adopted in GitVersion as a way for the build environment to indicate which branch is being built. But as you can see from #3049, support for the variable is not fully implemented.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any updates on this? It's annoying to have versions like 1.1.3-tags-1-1-1.1 created.

if (gitBranch is not null)
return gitBranch;

var sourceBranch = Environment.GetEnvironmentVariable("BUILD_SOURCEBRANCH");

// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml
// BUILD_SOURCEBRANCH must be used only for "real" branches, not for tags.
// Azure Pipelines sets BUILD_SOURCEBRANCH to refs/tags/<tag> when the pipeline is triggered for a tag.
if (sourceBranch?.StartsWith("refs/tags", StringComparison.OrdinalIgnoreCase) == true)
{
return null;
}

return sourceBranch;
}

public override bool PreventFetch() => true;

Expand Down