How to zero pad the build counter in TeamCity
Asked Answered
T

2

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?

Tetrahedral answered 18/9, 2014 at 12:41 Comment(0)
G
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%
Godolphin answered 18/9, 2014 at 13:2 Comment(3)
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-HostCrocein
Where would you put this one line in the build process to work?Yajairayajurveda
Six years later and I used this answer to do exactly this :D I've now changed the correct answer to this one.Tetrahedral
O
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.

Ogg answered 18/9, 2014 at 13:21 Comment(2)
Thanks - I'll have a look at GitVersion.Tetrahedral
@Tetrahedral it is what we are using in-house, and what I will be making of use of in this Project, github.com/chocolatey/ChocolateyGUI, so feel free to ask any questions :-)Ogg

© 2022 - 2024 — McMap. All rights reserved.