9

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?

starskythehutch
  • 3,328
  • 1
  • 25
  • 35

2 Answers2

6

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%
Mohammad Nadeem
  • 9,134
  • 14
  • 56
  • 82
  • 5
    If your build number is a simple counter (1, 2, 3, 4, ...), you could accomplish this using a PowerShell one liner in TeamCity: `"##teamcity[buildNumber '{0}']" -f ([Int32]%build.number%).ToString("0000") | Write-Host` – Emil G Apr 14 '15 at 13:58
  • Where would you put this one line in the build process to work? – MDV2000 Jan 08 '19 at 15:01
  • Six years later and I used this answer to do exactly this :D I've now changed the correct answer to this one. – starskythehutch Oct 13 '20 at 12:35
3

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.

Gary Ewan Park
  • 17,610
  • 5
  • 42
  • 60