Skip to content

Instantly share code, notes, and snippets.

@tillig
Created April 13, 2017 18:01
Show Gist options
  • Save tillig/ded1362b7af7ba2137d173bdee9efb37 to your computer and use it in GitHub Desktop.
Save tillig/ded1362b7af7ba2137d173bdee9efb37 to your computer and use it in GitHub Desktop.
Use semantic versioning in TeamCity builds (master vs. develop)
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="All" InitialTargets="_SetVersion" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<!--
Version is controlled through two settings:
- $(SemanticVersion) holds the logical three-segment semantic version.
- An optional $(SemanticQualifier) environment variable/property
indicates if the build is pre-release. Use the form "-beta{0}" like
String.Format(); the current build date/time gets inserted as arg 0.
-->
<SemanticVersion>1.0.0</SemanticVersion>
</PropertyGroup>
<Import Project="$(MSBuildProjectDirectory)\toolset\msbuildcommunitytasks\MSBuild.Community.Tasks.Targets"/>
<Target Name="_SetVersion">
<PropertyGroup>
<DateNow>$([System.DateTime]::UtcNow.ToString(yyyyMMddHHmmss))</DateNow>
<QualifiedSemanticVersion Condition="'$(SemanticQualifier)' == ''">$(SemanticVersion)</QualifiedSemanticVersion>
<QualifiedSemanticVersion Condition="'$(SemanticQualifier)' != ''">$(SemanticVersion)$([System.String]::Format($(SemanticQualifier), $(DateNow)))</QualifiedSemanticVersion>
<BuildLabel Condition="'$(BuildLabel)' == '' and '$(BUILD_VCS_NUMBER)' != '' and '$(SemanticQualifier)' == ''">$(QualifiedSemanticVersion).$(DateNow)</BuildLabel>
<BuildLabel Condition="'$(BuildLabel)' == '' and '$(BUILD_VCS_NUMBER)' != '' and '$(SemanticQualifier)' != ''">$(QualifiedSemanticVersion)</BuildLabel>
<BuildLabel Condition="'$(BuildLabel)' == '' and '$(BUILD_VCS_NUMBER)' == ''">0.0.0.0</BuildLabel>
</PropertyGroup>
<Message Text="##teamcity[buildNumber '$(BuildLabel)']" Condition="'$(BUILD_VCS_NUMBER)' != ''" />
<AssemblyInfo
AssemblyCompany="My Company"
AssemblyConfiguration="$(Configuration) $(BUILD_VCS_NUMBER)"
AssemblyCopyright="Copyright YYYY My Company"
AssemblyFileVersion="$(SemanticVersion).$([MSBuild]::Modulo($(DateNow), 65535))"
AssemblyInformationalVersion="$(QualifiedSemanticVersion)"
AssemblyProduct="My Product"
AssemblyVersion="$(SemanticVersion).0"
CodeLanguage="CS"
OutputFile="$(MSBuildProjectDirectory)\VersionInfo.cs"
Condition="'$(BUILD_VCS_NUMBER)' != ''" />
</Target>
</Project>
# Run this in a build step and the %teamcity.build.branch.is_default%
# gets subbed in with "True" or "False"
if(-not $%teamcity.build.branch.is_default%)
{
Write-Host "##teamcity[setParameter name='env.SemanticQualifier' value='-beta{0}']"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment