Git versioning for .NET projects with TeamCity

Tags: Git, CI, Powershell, TeamCity, Tools

Categories: Git

Tags: CI, Git, Powershell, TeamCity, Tools

TeamCity has a nice feature which allows to create a build number automatically with every build. For example, MyLib.dll has version When configuring TeamCity in the “Build number format” the value specified like {0} will increase with every build. So if you set “Build number format” 1.2.3.{0} then the last number (revision number) will increase with every build.

For a .NET project if you provide the AssemblyInfo patcher feature for your build configuration then the AssemblyVersion and AssemblyFileVersion will be updated automatically with the build number.

This works well for the cases when only one number should be changed. But you might want to have two numbers updated with your build, build and revision values. With Subversion you can include the repository Revision number as part of the building number. Git doesn’t provide this number because each commit is represented by a SHA1 hash.

The solution will be to take advantage of the number of commits since the most recent tag. This is made available through the git describe command.

This will work only if you have at least one tag in your repository and every time you need to change you version you need to create a new tag. Also you need to push your tag to the remote repository as it is not pushed automatically.

Let’s add a new tag to your commit

git tag v1.2

And use describe command

git describe --tags --long

Will display


The --tags flag allows to use lightweight tag and --long takes care to display the long format – the tag, the number of commits and the abbreviated commit name.

The 0 number represent the number of commits.

The describe command works fine but what happens if a new tag is created and its purpose is not for versioning. To fix this will add the --match flag so your command will become:

git describe --tags –long --match v?.?

This will display only the tags which are in format v?.?, v1.2 for instance. If you want to keep your versioning tags differently, for example version1.2 then you need to change v?.? to version?.?

That is all good but how can we use it with TeamCity? We will create a Powershell script which parses the output and creates the build number.

In TeamCity “Runner type” is set as Powershell and in the “Script arguments” %build.number% is provided.

Will use the following script

Param ([string]$build_num)

$git_version = (git describe --tags --long --match v?.? | Select-String -pattern '(?<major>[0-9]+)\.(?<minor>[0-9]+)-(?<seq>[0-9]+)-(?<hash>[a-z0-9]+)').Matches[0].Groups

$git_describe = $git_version[0].Value

$version = [string]::Join('.', @(

Write-Host "##teamcity[buildNumber '$version']"


  • Line 1: reads the provided %build.number% from TeamCity
  • Line 3: uses the git describe command and creates an object with all version parts
  • Lines 7-12: create a version string based on the git tag and TeamCity
  • Line 14: uses TeamCity service messages to pass the information to TeamCity


This step should be added before the step where you compile your project.


comments powered by Disqus