-
Notifications
You must be signed in to change notification settings - Fork 654
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
Use GITHUB_REF only for CI builds on branches #3033
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for this, @ThomasPiskol! However, looking a bit closer at this, I see that other build server tests have similar assertions to those of GitHub Actions (see review comments).
And then we have tests for Drone and Jenkins which assert something completely different (without /refs/heads/
):
GitVersion/src/GitVersion.Core.Tests/BuildAgents/DroneTests.cs
Lines 102 to 118 in 666bd37
[Test] | |
public void GetCurrentBranchShouldUseDroneBranchInCaseOfPullRequestAndEmptyDroneSourceBranchAndCiCommitRefSpec() | |
{ | |
// Arrange | |
const string droneBranch = "droneBranch"; | |
this.environment.SetEnvironmentVariable("DRONE_PULL_REQUEST", "1"); | |
this.environment.SetEnvironmentVariable("DRONE_SOURCE_BRANCH", ""); | |
this.environment.SetEnvironmentVariable("CI_COMMIT_REFSPEC", ""); | |
this.environment.SetEnvironmentVariable("DRONE_BRANCH", droneBranch); | |
// Act | |
var result = this.buildServer.GetCurrentBranch(false); | |
// Assert | |
result.ShouldBe(droneBranch); | |
} |
this.buildServer.GetCurrentBranch(true).ShouldBe($"origin/{MainBranch}"); |
So I'm not really sure why this is working for some build servers and not others. I'm also not sure what the correct expectation for GetCurrentBranch()
is supposed to be. But I think we need to harmonize the behavior here across all build server implementations and not just change the behavior of GitHub Actions.
Thoughts?
PS: I took the liberty to edit your PR description to add more related issues.
Thanks for changing the description and linking the other issues 🙏 I agree, the main question is what is the expected behaviour of the For me it seems that some additional "magic" happens in In the log file which I have created to reproducing #2838 I can see the following line:
And it seems that's the origin of this log message: GitVersion/src/GitVersion.Core/Core/GitPreparer.cs Lines 383 to 385 in 666bd37
So it seems that this line is doing some "magic" with the branch name. Before entering this method the branch name has the value GitVersion/src/GitVersion.Core/Core/GitPreparer.cs Lines 360 to 364 in 666bd37
This leads to the issue, that a branch with the name So I'm not sure, where we should fix this. On one side it seems correct, to change the On the other side it seems also feasible to change the behavior of So it seems less risky to change all the implementations of I'll update the PR in the coming days and until then, we can think about the impact of the changes 🙂 |
If you take a look at #2928 (comment), it seems like this problem actually exists in GitLab as well. Which gives me a bit confidence in that taking a build server agnostic approach to fixing this is the right thing to do.
I agree. I'm not 100% sure what behaviour that should be, but I can't think of a reason why |
I've adapted the build server implementations for
No changes required for the following implementations, because they don't have a custom implementation for There are some other implementations but I'm not familiar with them and the current implementations look ok from my point of view: |
One question: the default branch is now Is this ok or should I change the PR to target |
Please switch to 5.x, we will eventually merge that into main as well |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@asbjornu Can you restart the failed job? It looks like an issue with the agent who was executing the job (timeout) and I don't have the permission to restart the job. |
Thank you @ThomasPiskol for your contribution! |
@ThomasPiskol I had to revert this PR, if you have time please re-work this PR |
I've reproduced #3081 with an Azure DevOps pipeline. It's complicated, here's what I've observed so far. Azure DevOps creates a branch for any PR according to the naming scheme: Due the change in this PR we end up in this branch of the switch case: GitVersion/src/GitVersion.Core/Core/GitPreparer.cs Lines 235 to 238 in b17d33e
and we hit the exception "LibGit2Sharp.LibGit2SharpException: this remote has never connected" in LibGit2Sharp A first look shows that there many places in GitVersion which assume that any branch has a ref starting with I'll focus at first to reproduce this behaviour with an automated test. |
Thanks for taking the time to investigate |
Improve support for GitHub Actions and fix #2838
Description
In case GitVersion is running during GitHub Actions CI build, the environment variable
GITHUB_REF
is used by GitVersion to get the current branch.But GitHub Actions only provide branch name here in case of a "normal" CI build. If the GitHub Action was triggered because of a tag event or pull request event, the
GITHUB_REF
environment variable does not contain an existing branch name as described by GitHub:Related Issue
Fixes #2838.
Fixes #2852.
Fixes #2301.
Fixes #2928.
Fixes #2869.
Related to #2074.
Motivation and Context
Bug fix and align with GitHub Actions concept.
How Has This Been Tested?
Three GitHub Actions scenarios must be tested:
https://github.com/ThomasPiskol/gitversion-repro/actions
There's no change to the "normal" the Pull Request events, they create the same version string as before.
So this change should fix the issue without causing some issue for the other workflows.
Checklist: