Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zero pad the build counter in TeamCity

I'm trying to follow some guidance from this article which describes NuGet and SemVer best practices.

Point #3 states that I should "Use leading zeros in the numerical suffix to auto-increment prereleases" but I am struggling working out how I can zero pad the build.counter parameter in TeamCity so that I get 0025 instead of 25.

Does anyone have a mechanism to handle this?

like image 942
starskythehutch Avatar asked Sep 18 '14 12:09

starskythehutch


2 Answers

You could write a powershell script like:

function Update-BuildNumber([string]$buildNumber)
{
    $VersionComponents = $buildNumber.Split(".")
    $buildNumber = ""
    foreach($VersionComponent in $VersionComponents)
    {
        $index = [array]::IndexOf($VersionComponents, $VersionComponent)
        if (($index + 1) -eq $VersionComponents.Count)
        {
            $buildNumber += "00" + $VersionComponent
        }
        else
        {
            $buildNumber += $VersionComponent + "."
        }
    }
    Write-Output "##teamcity[buildNumber '$buildNumber']"
}

And call it from a Teamcity build step and pass the parameter %build.number% something like:

Update-BuildNumber -buildNumber %build.number%
like image 145
Mohammad Nadeem Avatar answered Nov 16 '22 01:11

Mohammad Nadeem


If I were you, I would make use of GitVersion. It includes an option to use a LegacySemVerPadded version of the generated version number. There are various other alternatives of the generated version number as well.

There is a TeamCity Meta Runner for it here.

GitVersion does the work of calculating the new Semantic Version Number for you, based on the current state of your repository.

Failing that, yeah, do the work elsewhere, in PowerShell, and then use TeamCity Service Messages to change the build number in TeamCity. You can find a PowerShell Module here.

That provides some helper functions for doing just that.

like image 40
Gary Ewan Park Avatar answered Nov 15 '22 23:11

Gary Ewan Park