Automatically generating SemVer release numbers for your Continuous Delivery Pipeline with GitVersion and TeamCity

Following on from my posts Continuous Delivery with Sitecore Part 1 – TeamCity Setup and Continuous Delivery with Sitecore Part 2 – Octopus Deploy Setup, I’ve added another step to achieve Semantic Versioning (SemVer) in my deployment pipeline, as we are already using GitFlow  for branching we only had some configuration changes to make.

I’ve added GitVersion to our release process, they claim

Versioning when using git, solved. GitVersion looks at your git history and works out the semantic version (semver.org) of the commit being built.

This enables my release numbers to be automatically generated using semver.org and this number can be used to version assemblies,  add source control tags and for any NuGet packages or other artifacts your build process generates.

Setup

  • You will need to generate a GitVersionConfig.yaml config file and add it to the root of your solution mine is below, GitVersion will then use this configuration and your repository history to work out the next build number, for more on the configuration file see the documentation – https://gitversion.readthedocs.io/en/latest/configuration/

I use Team City and Octopus Deploy for build and deployment currently but GitVersion Supports a wide range of build tools.

GitVersion Build Server Support {#build-server-support}

GitVersion has support for quite a few build servers out of the box. Currently we support:

Setup for TeamCity

  • Create new System Parameters to act as placeholders for the values generated by GitVersion, I have created system.GitVersion.FullSemVer, system.GitVersion.NuGetVersion and system.GitVersion.SemVer. You can view the Variables generated by GitVersion here – https://gitversion.readthedocs.io/en/latest/more-info/variables/

TeamCityParameters

 

Set the Build number format of your Team City Build to _%system.GitVersion.SemVer%  _the placeholder we previously created.

 

 

TeamCityParameters

 

 

Add a new Command Line step to your build, after your NuGet Packages have been restored and before your Build and OctoPack step. The Command Executable is GitVersion and the command parameters are ****. /updateAssemblyInfo /output buildserver****

 

 

TeamCityStep

 

Add the new system variable in the 

 

 

OctoPackVersion

 

http://docs.octopusdeploy.com/display/OD/TeamCity#TeamCity-InstallPluginInstallingtheplugin

On your Create Release and Develop Release steps when using the Octopus TeamCity plugin set the Release number field to **%system.GitVersion.NuGetVersion%**

 

 

OctopusCreateReleaseBuildsNumber

 

That is all the setup required, when you next Run a new build on TeamCity you should see the results:-

Results

TeamCity builds with SemVer build numbers.

TeamCityBuilds

TDS packages with NuGetVersion version generated by GitVersion

TDSPacakges

Octopus Deploy release numbers using NuGetVersion

OctopusBuilds

Assemblies stamped with the same SemVer release number.

Assembly