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

[Bug] Building from tag fails to produce correct version on Azure DevOps #2231

Closed
alarobric opened this issue Apr 14, 2020 · 4 comments
Closed
Labels

Comments

@alarobric
Copy link

alarobric commented Apr 14, 2020

Describe the bug

When building in Azure DevOps, recently we've been getting some strange version numbers from GitVersion. We follow ReleaseFlow, and it's mostly working for us normally. We do feature branches and merge to master. Every so often when we are ready to release to customers, we branch a release branch "release/ProdName/v6.9". Builds here look like "6.9.2-rc.220" until we decide to git tag, at which point we get 6.9.2 as expected.
Recently though, we've been getting builds like "6.10.0-tags-ProdName-v6-9-2.1+220" instead. We couldn't work out what was going on or what was causing. It seemed to only be affecting one product within the monorepo at first, and removing and recreating the release branch seemed to have fixed it.

We now see it again, but I've noticed today that it's tied to the build trigger. If I trigger a build on the branch release/ProdName/v6.9, where the latest commit is tagged "ProdName/v6.9.2", I'll get the expected result of a version 6.9.2. If instead I trigger a build on the tag, it builds the exact same commit but gives a version 6.10.0-tags-ProdName-v6-9-2.1+220

Expected Behavior

Version 6.9.2

Actual Behavior

Version 6.10.0-tags-ProdName-v6-9-2.1+220

Possible Fix

Found in the GitVersion source that the part "Branch from build environment: xxx" comes from the Azure Pipelines env variable BUILD_SOURCEBRANCH. In each build, the git checkout is actually the same, and specifies only the commit, no branches. On the tag one it's refs/tags/ProdName/v6.9.2 and this then results in "No branch configuration found for branch tags/ProdName/v6.9.2

My understanding is that a tag on a commit should always override any branch info, but that doesn't seem to be happening.

Steps to Reproduce

As above, trigger build from release branch, or from tag. In both cases the commit being built is the same, all steps are the same.
Agent parameters are same.
Git commands run by Azure DevOps are the same. Tag is present in both. Commit is the same.

Tag build says:
Git step title in Azure Devops is "Checkout RepoName@refs/tags/ProdName/v6.9.2 to s"
command used is >git checkout --progress --force 24257159c4cf9db433be7c50fa12b38d670834c2
sourceBranch=refs/tags/ProdName/v6.9.2
Finishing: Checkout RepoName@refs/tags/ProdName/v6.9.2 to s

Release branch build says:
Git step title in Azure Devops is "Checkout RepoName@release/ProdName/v6.9 to s"
command used is >git checkout --progress --force 24257159c4cf9db433be7c50fa12b38d670834c2
sourceBranch=refs/heads/release/ProdName/v6.9
Finishing: Checkout RepoName@release/ProdName/v6.9 to s

After the checkout step, a powershell script is run with following
"GitVersion.exe /output buildserver /nofetch"

Tag build produces this:

INFO [04/13/20 16:39:34:87] Branch from build environment: refs/tags/ProdName/v6.9.2
INFO [04/13/20 16:39:34:87] Begin: Normalizing git directory for branch 'refs/tags/ProdName/v6.9.2'
  INFO [04/13/20 16:39:34:91] One remote found (origin -> 'https://xxx@dev.azure.com/xxx/yyy/_git/yyy').
  INFO [04/13/20 16:39:34:91] Skipping fetching, if GitVersion does not calculate your version as expected you might need to allow fetching or use dynamic repositories
  INFO [04/13/20 16:39:34:92] Creating local branch refs/heads/tags/ProdName/v6.9.2 pointing at 24257159c4cf9db433be7c50fa12b38d670834c2
...
...
INFO [04/13/20 16:39:39:75] HEAD points at branch 'refs/heads/tags/ProdName/v6.9.2'.
  INFO [04/13/20 16:39:39:76] End: Normalizing git directory for branch 'refs/tags/ProdName/v6.9.2' (Took: 4,885.91ms)
  INFO [04/13/20 16:39:39:76] Project root is: D:\a\1\s\
  INFO [04/13/20 16:39:39:76] DotGit directory is: D:\a\1\s\.git
  INFO [04/13/20 16:39:39:81] Begin: Loading version variables from disk cache
    INFO [04/13/20 16:39:39:82] Cache file D:\a\1\s\.git\gitversion_cache\CBCB265AD7F0967CA735B87FE6496A4CBD84218D.yml not found.
  INFO [04/13/20 16:39:39:82] End: Loading version variables from disk cache (Took: 3.11ms)
  INFO [04/13/20 16:39:39:97] Using latest commit on specified branch
  INFO [04/13/20 16:39:39:99] No branch configuration found for branch tags/ProdName/v6.9.2, falling back to default configuration
  INFO [04/13/20 16:39:39:99] Begin: Attempting to inherit branch configuration from parent branch
    INFO [04/13/20 16:39:40:02] HEAD is merge commit, this is likely a pull request using master as base

Release branch build produces this:

INFO [04/13/20 19:26:26:84] Branch from build environment: refs/heads/release/ProdName/v6.9
INFO [04/13/20 19:26:26:84] Begin: Normalizing git directory for branch 'refs/heads/release/ProdName/v6.9'
  INFO [04/13/20 19:26:26:88] One remote found (origin -> 'https://xxx@dev.azure.com/xxx/yyy/_git/yyy').
  INFO [04/13/20 19:26:26:88] Skipping fetching, if GitVersion does not calculate your version as expected you might need to allow fetching or use dynamic repositories
  INFO [04/13/20 19:26:26:90] Creating local branch refs/heads/release/ProdName/v6.9
...
INFO [04/13/20 19:26:32:23] HEAD points at branch 'refs/heads/release/ProdName/v6.9'.
  INFO [04/13/20 19:26:32:25] End: Normalizing git directory for branch 'refs/heads/release/ProdName/v6.9' (Took: 5,404.82ms)
  INFO [04/13/20 19:26:32:25] Project root is: D:\a\1\s\
  INFO [04/13/20 19:26:32:25] DotGit directory is: D:\a\1\s\.git
  INFO [04/13/20 19:26:32:30] Begin: Loading version variables from disk cache
    INFO [04/13/20 19:26:32:31] Cache file D:\a\1\s\.git\gitversion_cache\9BA9A688F479E76BB5CBBC3C4AD2917538E6D017.yml not found.
  INFO [04/13/20 19:26:32:31] End: Loading version variables from disk cache (Took: 3.57ms)
  INFO [04/13/20 19:26:32:46] Using latest commit on specified branch
  INFO [04/13/20 19:26:32:52] Running against branch: release/ProdName/v6.9 (24257159c4cf9db433be7c50fa12b38d670834c2)
  INFO [04/13/20 19:26:32:52] Current commit is tagged with version 6.9.2, version calculation is for metadata only.

I tried but can't reproduce this locally because it's tied to the buildserver code.
Branch from build environment: refs/tags/ProdName/v6.9.2 coming from the environment variables on Azure DevOps.

I'm looking at reproducing in a integration test, but not sure exactly how.

It is similar to issue #2206 but without timing or process discussion. Seems only related to Azure DevOps environment.

Context

When building in Azure DevOps, recently we've been getting some strange version numbers from GitVersion. We follow ReleaseFlow, and it's mostly working for us normally. We do feature branches and merge to master. Every so often when we are ready to release to customers, we branch a release branch "release/ProdName/v6.9". Builds here look like "6.9.2-rc.220" until we decide to git tag, at which point we get 6.9.2 as expected.
Recently though, we've been getting builds like "6.10.0-tags-ProdName-v6-9-2.1+220" instead. We couldn't work out what was going on or what was causing. It seemed to only be affecting one product within the monorepo at first, and removing and recreating the release branch seemed to have fixed it.

Your Environment

  • Version Used: ??
  • Operating System and version (Windows 10, Ubuntu 18.04): Windows - Azure Pipeline build agent
  • Link to your project: Private
  • Link to your CI build (if appropriate): Sorry, private

GitVersion.yml

next-version: 6.9.0
mode: ContinuousDeployment # Default: ContinuousDelivery
assembly-informational-format: '{FullSemVer}' # Default: {InformationalVersion}
tag-prefix: 'ProdName/[vV]'
branches: 
  master:
    regex: master
    mode: ContinuousDeployment
    tag: beta
    increment: Minor
    prevent-increment-of-merged-branch-version: true
    track-merge-target: false
    tracks-release-branches: true
    is-release-branch: false
  feature:
    regex: features?[/-]
    mode: ContinuousDeployment
    tag: useBranchName
    increment: Patch
    prevent-increment-of-merged-branch-version: true
    track-merge-target: true
    tracks-release-branches: true
    is-release-branch: false
  release:
    regex: releases?[/-]
    mode: ContinuousDeployment
    tag: rc
    increment: Patch
    prevent-increment-of-merged-branch-version: true
    track-merge-target: false
    tracks-release-branches: false
    is-release-branch: true
ignore:
  sha: []
@alarobric alarobric added the bug label Apr 14, 2020
@alarobric
Copy link
Author

I tried to reproduce in a integration test, but it just passes. Tried several things, but I think the Azure part of this is not sticking, despite trying to follow one of the existing tests?

Hopefully it gives an idea though?

[Test]
        public void AlanTargetBranchTagDoesNotWork()
        {
            //var targetBranch = "refs/heads/release/ProdName/v6.9";
            //var targetBranch = "refs/tags/ProdName/v6.9.2";

            var targetBranch = "release/ProdName/v6.9"; // works
            var tagBranch = "tags/ProdName/v6.9.2";
            Commit commit = null;

            using var fixture = new RemoteRepositoryFixture(
                path =>
                {
                    Repository.Init(path);
                    Console.WriteLine("Created git repository at '{0}'", path);

                    var repo = new Repository(path);
                    repo.MakeCommits(5);

                    repo.CreateBranch(targetBranch);

                    Commands.Checkout(repo, targetBranch);
                    repo.MakeCommits(5);

                    commit = repo.MakeACommit();
                    repo.ApplyTag("v6.9.2");

                    return repo;
                });

            var gitVersionOptions = new GitVersionOptions
            {
                WorkingDirectory = fixture.LocalRepositoryFixture.RepositoryPath,
                RepositoryInfo =
                {
                    TargetBranch = tagBranch,
                    CommitId = commit.Sha
                },

                Settings =
                {
                    NoNormalize = false,
                    NoFetch = false
                }
            };
            var options = Options.Create(gitVersionOptions);
            var environment = new TestEnvironment();
            environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
            environment.SetEnvironmentVariable("BUILD_SOURCEBRANCH", tagBranch);

            var sp = ConfigureServices(services =>
            {
                services.AddSingleton(options);
                services.AddSingleton<IEnvironment>(environment);
            });

            var gitPreparer = sp.GetService<IGitPreparer>();

            gitPreparer.Prepare();

            fixture.AssertFullSemver("6.9.2");
            fixture.AssertFullSemver("6.9.2", repository: fixture.LocalRepositoryFixture.Repository, commitId: commit.Sha, targetBranch: tagBranch);
        }

@alarobric
Copy link
Author

I realized now this is similar to #2074, at least the second half of that ticket from @garoyeri

@garoyeri
Copy link

It's the same issue. #2074 started with building on TeamCity, and this one is Azure DevOps. In both cases, the root issue is that building from a tag from a detached head triggers the normalization, then the following build ends up generating a weird version name.

@arturcic
Copy link
Member

Closing as duplicate of #2074

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants