Post

Set NuGet pre-release build numbers with leading zeros on TeamCity

Sometimes it comes the day that NuGet doesn’t see a newer version of a pre-release package. So if you wanted to update to a newer version you must using the Package Manager Console with the -pre flag. That’s because NuGet makes a alpanumeric comparison, so e.g. ALPHA99 is greater than ALPHA115.

So for pre-releases we can use the Rule 3 from the Top 10 NuGet (Anti-) Patterns: Use leading zeros in the numerical suffix to auto-increment pre-releases.

And here comes my getting gray hairs part. The powershell build step doesn’t work with the allowed string number formatting like this:

1
"{0:D3}" -f 2

Expected result:

1
002

But on TeamCity I got:

1
2

So after trial and error I used this powershell script with the PadLeft function:

1
2
3
4
5
$buildNumber = $env:BUILD_NUMBER
$VersionComponents = $buildNumber.Split(".")
$counter = ("{0}" -f $VersionComponents[3]).PadLeft(3, "0")
$buildNumber = "{0}.{1}.{2}-ALPHA{3}" -f $VersionComponents[0] , $VersionComponents[1] , $VersionComponents[2] , $counter
Write-Host "##teamcity[setParameter name='env.BUILD_NUMBER' value='$buildNumber']"

This gives me e.g:

1
1.0.0-ALPHA001

Use this script to change the TeamCity build number before creating the NuGet package.

Hope this helps!

This post is licensed under CC BY 4.0 by the author.