Search code examples
azure-devopscakebuildgitversion

How to get a unqiue build number into Azure DevOps and Cake?


I am using Cake Build script to build our .NET software and I need a unique build number to be baked into the DLLs primarily because we need to know the exact binaries we used in case we need to debug dump files with the binaries.

First thing, here is my current Cake version task:

Task("Version")
    .Does(() => 
    {
        Information("Versioning software for configuration {0} of {1}...", configuration, solution);
        var gitVersionSettings = new GitVersionSettings {
            UpdateAssemblyInfo = true
        };
        var Version = GitVersion(gitVersionSettings);
    });

Very simple but it updates the assembly files with the correct FileVersion and ProductVersion and thus updates the DLLs.

Secondly, I found the Build Numbers in Azure DevOps to be all over the place. Here are some of the examples:

2.1.0-PullRequest0019.7
2.0.1-beta.1+4
2.1.0-alpha.4
2.1.0-PullRequest0017.4
2.0.1-beta.1+2
2.0.0
0.1.0+70

These are the last 7 build numbers and they are from builds from different branches including merges of hotfixes into master and then in dev. I notice for this build pipeline, the build number is empty so I think it is just dumping whatever into the build pipeline kind of using my last set of tags (only tagged 2.0.0 and 2.0.1 so far).

There are three issues:

  1. How do I get the unique build number into my Azure DevOps pipelines (I have 3 and each build, no matter the pipeline, needs to be unique)?
  2. How do I get the unique build number into my semantic versioning and to respect my tags in Master?
  3. How do I get the build number into my Cake script for when I build both on Azure DevOps build server but also when I build locally on my machine (to debug)?

EDIT

I figured out you can get the unique build number produced by Azure DevOps using $(BuildId) so that answers question 1. I also think I can pass in the build number from Azure DevOps to my Cake script through an argument which is fine. It is more now an issue with the local builds and how to get unique build numbers for them. Also, what format should save my versioning. I am trying to use a 1.0.0 format for tags so should I have a fourth number as the always increasing build number? And how should that be saved to the AssemblyInfo.cs file?

Edit from my first edit... after running a successful build, the build number changed from the build id to something else "2.1.0-alpha.7" and I don't know why because I specified to use the build id in my pipeline.


Solution

  • I would suggest that you do the following:

    GitVersion(new GitVersionSettings {
        OutputType = GitVersionOutput.BuildServer,
    });
    
    var gitVersionSettings = new GitVersionSettings {
        UpdateAssemblyInfo = true
    };
    
    var Version = GitVersion(gitVersionSettings);
    

    i.e. run GitVersion twice. The first uses the BuildServer outputtype, which should cause GitVersion to update the Azure DevOps Build Number to match the asserted version number for the repository.

    The second execution allows you to grab the Version number into your local variable, as you are currently doing.

    The reason that this is necessary is that GitVersion can't both update the build server version and output the JSON file that is being used to generate the asserted version numbers which are being returned in the second call.

    NOTE: You will likely only want to do this when you are actually running on Azure DevOps, so you would put the first execution in it's own if block.

    I use a similar approach to what I am suggesting in this question in my Cake.Recipe project here:

    https://github.com/cake-contrib/Cake.Recipe/blob/develop/Cake.Recipe/Content/gitversion.cake#L56