GitVersion
GitVersion copied to clipboard
[Bug] branch changed when calling GitVersion from Gitlab
using GitVersion with NukeBuild in Gitlab
pipeline is started for the current branch (for example, dev), but for my task, I need to additionally clone the master branch. I clone it, but after GitVersion works out, the branch changed to the current branch, i.e. dev
After looking at the source code, I found that this happens in GitPreparer.cs in this method:
private string? ResolveCurrentBranch()
{
var gitVersionOptions = this.options.Value;
var targetBranch = gitVersionOptions.RepositoryInfo.TargetBranch;
var isDynamicRepository = !gitVersionOptions.RepositoryInfo.ClonePath.IsNullOrWhiteSpace();
var currentBranch = this.buildAgent.GetCurrentBranch(isDynamicRepository) ?? targetBranch;
this.log.Info("Branch from build environment: " + currentBranch);
return currentBranch;
}
The problem is in this line when getting the current branch from Gitlab:
var currentBranch = this.buildAgent.GetCurrentBranch(isDynamicRepository) ?? targetBranch;
for Gitlab, it is obtained from the CI_COMMIT_REF_NAME variable (GitLabCi.cs):
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("CI_COMMIT_REF_NAME");
If set $CI_COMMIT_REF_NAME=master before the call GitVersion, then the branch does not change, but I don't know what side effects this can lead to. How can I specify GitVersion so that it doesn't change the branch?
Which branch is being built determines what version GitVersion generates. Each branch has different configuration (by default, as well as through the possibility to add your own in GitVersion.yml) that results in a different version number. It is therefore important that the checked out branch matches that of the branch being built. To fetch master without switching to it, you can do:
git fetch origin master:master
This issue has been automatically marked as stale because it has not had recent activity. After 30 days from now, it will be closed if no further activity occurs.
This issue was closed because it has been stalled for 30 days with no activity. Thank you for your contributions