Dynamically changing build numbers by branch in TeamCity

We use feature branches, and build all of them on our TeamCity CI setup. Every build can be deployed on our test servers, but it’s useful to be able to quickly distinguish which ones came from master and which came from other branches. I took a script from here and edited it to simply append -alpha to the end of the version number for any non-master build:

$branch = "%teamcity.build.branch%"

if ($branch.Contains("/")) 
{
  $branch = $branch.substring($branch.lastIndexOf("/") + 1)
}

Write-Host "Building from $branch branch"

if ($branch -ne "master") 
{
  $buildNumber = "%build.number%-alpha"
  Write-Host "Appending alpha to build number"
  Write-Host "##teamcity[buildNumber '$buildNumber']"
}
else
{
  Write-Host "Leaving build number as-is"
}

This makes it really obvious when a build has come from a feature (or other) branch, to avoid accidentally deploying it etc. You could probably easily extend this to pull a version number from the branch name as well, or similar.

To use this across multiple builds, I just set up a build template with the script stored in the first build step, which you can then apply to builds either individually or across a project.

Magic Commands – cleaning up ASP.NET temporary files

ASP.NET temporary files can quickly mount up and eat up disk space, especially if you deploy multiple times a day. This website provides a handy command that you can run to delete any unnecessary files:

Get-ChildItem "C:\Windows\Microsoft.NET\Framework*\v*\Temporary ASP.NET Files" -Recurse | Remove-Item -Recurse

You’ll get a few errors when it hits files in use, but apart from that it works a treat. You can run this as a scheduled task if you want to avoid having to do it manually all the time, or get your deployment tool to run it on every deployment to tidy up as you go (Octopus Deploy has its own library task to do something similar, for example).

How to time how long something takes to run in Windows

It can be useful to time how long certain commands take to run. With Powershell, you can do this easily using the Measure-Command cmdlet:

Measure-Command { ping google.com | Out-Default }

Simply replace ping google.com with whatever you want to time, and off you go. You’ll get a readout similar to this when your command finishes executing with details of how long it took:

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 3
Milliseconds      : 116
Ticks             : 31167663
TotalDays         : 3.60736840277778E-05
TotalHours        : 0.000865768416666667
TotalMinutes      : 0.051946105
TotalSeconds      : 3.1167663
TotalMilliseconds : 3116.7663