My Profile Photo

punker76


another private blog with developer stuff and some thoughts


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:

"{0:D3}" -f 2

Expected result:

002

But on TeamCity I got:

2

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

$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.0.0-ALPHA001

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

Hope this helps!

comments powered by Disqus