Skip to content

Instantly share code, notes, and snippets.

@dealproc
Last active August 29, 2015 14:23
Show Gist options
  • Save dealproc/10ca3ff00c609622b903 to your computer and use it in GitHub Desktop.
Save dealproc/10ca3ff00c609622b903 to your computer and use it in GitHub Desktop.
SemVer Script

#Semantec Versioning Script for TeamCity

This script is a derrivative work from the Octopus Deploy team's theory of how they handle versioning their system. Modifications were necessary to support the concept of setting the assembly's version info to be in a series, where the file's version would be more specific, to support installation updates, etc.

You're going to need the MSBuild Community Tasks project in order to update the AssemblyInfo.cs files within the solution.

Within TeamCity, the Build files cleaner (Swarba) Build Feature should be activated, and it is up to the administrator whether to set it to run before the next build starts, or at the end of the build process completing. Do make sure the Clean Checkout flag is set, and it seemed to be that the Locking processes setting could be set to <Do Not Detect>

Verbose output is probably based on personal preference.

https://octopusdeploy.com/blog/teamcity-version-numbers-based-on-branches http://semver.org/ http://nvie.com/posts/a-successful-git-branching-model/

# These are project build parameters in TeamCity
# Depending on the branch, we will use different major/minor versions
$majorMinorVersionMaster = "%system.MajorMinorVersion.Master%"
$majorMinorVersionDevelop = "%system.MajorMinorVersion.Develop%"
$assemblyVersion = ""
# TeamCity's auto-incrementing build counter; ensures each build is unique
$buildCounter = "%build.counter%"
# This gets the name of the current Git branch.
$branch = "%teamcity.build.branch%"
# Sometimes the branch will be a full path, e.g., 'refs/heads/master'.
# If so we'll base our logic just on the last part.
if ($branch.Contains("/"))
{
$branch = $branch.substring($branch.lastIndexOf("/")).trim("/")
}
Write-Host "Branch: $branch"
if ($branch -eq "master")
{
$buildNumber = "${majorMinorVersionMaster}.${buildCounter}"
$assemblyVersion = "$buildNumber.0"
$assemblyFileVersion = "${majorMinorVersionMaster}.0.0"
}
elseif ($branch -eq "develop")
{
$buildNumber = "${majorMinorVersionDevelop}.${buildCounter}"
$assemblyVersion = "$buildNumber.0"
$assemblyFileVersion = "${majorMinorVersionDevelop}.0.0"
}
elseif ($branch -match "release-.*")
{
$specificRelease = ($branch -replace 'release-(.*)','$1')
$buildNumber = "${specificRelease}.${buildCounter}"
$assemblyVersion = "$buildNumber.0"
$assemblyFileVersion = "${buildNumber}.0"
}
else
{
# If the branch starts with "feature-", just use the feature name
$branch = $branch.replace("feature-", "")
$buildNumber = "${majorMinorVersionDevelop}.${buildCounter}-${branch}"
$assemblyVersion = "$buildNumber"
$assemblyFileVersion = "${majorMinorVersionDevelop}.0.0"
}
Write-Host "##teamcity[buildNumber '$buildNumber']"
Write-Host "##teamcity[setParameter name='system.Version' value='$assemblyVersion']"
Write-Host "##teamcity[setParameter name='system.AssemblySeries' value='$assemblyFileVersion']"
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildCommunityTasksPath>$(MSBuildProjectDirectory)\.build</MSBuildCommunityTasksPath>
</PropertyGroup>
<Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.targets"/>
<!-- Version Number -->
<PropertyGroup>
<AssemblySeries Condition=" '$(AssemblySeries)' == ''">1.0.0.0</AssemblySeries>
<Version Condition=" '$(Version)' == ''">1.2.0.0</Version>
</PropertyGroup>
<ItemGroup>
<AssemblyInfoFiles Include="$(MSBuildProjectDirectory)\source\**\AssemblyInfo.cs" />
</ItemGroup>
<PropertyGroup Condition=" '$(BuildConfiguration)' == '' ">
<BuildConfiguration>Release</BuildConfiguration>
</PropertyGroup>
<Target Name="Clean">
<DeleteTree Directories="**\obj\**;**\bin\**" />
</Target>
<Target Name="Version">
<Time>
<Output TaskParameter="Year" PropertyName="Year" />
</Time>
<Message Text="Build Number: '$(buildNumber)'"/>
<Message Text="Assembly Series: '$(AssemblySeries)'"/>
<FileUpdate Files="@(AssemblyInfoFiles)"
Regex="Version\(&quot;(\d+)\.(\d+)(\.(\d+)\.(\d+)|\.*)&quot;\)"
ReplacementText="Version(&quot;$(AssemblySeries)&quot;)">
</FileUpdate>
<FileUpdate Files="@(AssemblyInfoFiles)"
Regex="AssemblyFileVersion\(&quot;(\d+)\.(\d+)(\.(\d+)\.(\d+)|\.*)&quot;\)"
ReplacementText="AssemblyFileVersion(&quot;$(Version)&quot;)">
</FileUpdate>
<FileUpdate Files="@(AssemblyInfoFiles)"
Regex="AssemblyInformationalVersion\(&quot;(\d+)\.(\d+)(\.(\d+)\.(\d+)|\.*)&quot;\)"
ReplacementText="AssemblyInformationalVersion(&quot;$(Version)&quot;)">
</FileUpdate>
</Target>
<!-- Projects to Build -->
<ItemGroup>
<ProjectFiles Include="$(MSBuildProjectDirectory)\**\*.sln">
<Properties>Configuration=$(BuildConfiguration)</Properties>
</ProjectFiles>
</ItemGroup>
<Target Name="Compile" DependsOnTargets="Clean;Version">
<!--<MSBuild Projects="@(ProjectFiles)" />-->
</Target>
<Target Name="Build">
<CallTarget Targets="Compile" />
</Target>
<Target Name="Rebuild">
<CallTarget Targets="Compile" />
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment