Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save derekgates/4675761 to your computer and use it in GitHub Desktop.
Save derekgates/4675761 to your computer and use it in GitHub Desktop.
TeamCity psake script that sets BuildNumber from an existing AssemblyInfo in source. This is useful as it allows devs to control the version number without needing to change parameters in TeamCity for the project configuration. https://github.com/psake/psake/wiki/How-can-I-integrate-psake-with-Team-City%3F
task default -depends GetBuildNumber
task GetBuildNumber {
Assert ($base_directory -ne $null) '$base_directory should not be null. Try %system.teamcity.build.checkoutDir%'
Assert ($solution_name -ne $null) '$solution_name should not be null'
$version = gc $base_directory\$solution_name\Properties\AssemblyInfo.cs | select-string -pattern "AssemblyVersion" | Out-String
$version | Foreach-Object { Write-Host $_ }
$version -imatch '\[assembly: Assembly(File)?Version\("(?<major>[0-9]+)\.(?<minor>[0-9]+)\.(?<revision>[0-9]+)\.(?<build>[0-9]+)"\)\]' | Out-Null
Assert ($matches -ne $null) 'Unable to find AssemblyVersion string!'
#allow script to take in a build value to use, if the assembly info should be overriden to auto increment
if ($buildvalue -eq $null) { $buildvalue = $matches["build"] }
if ($forcezeros -eq "true") { "##teamcity[buildNumber '{0}.{1}.{2}.{3}']" -f "0","0","0",$buildvalue }
elseif ($insertbuildnumber -eq "true") { "##teamcity[buildNumber '{0}.{1}.{2}.{3}']" -f $matches["major"],$matches["minor"],$matches["revision"],$buildvalue }
else { "##teamcity[buildNumber '{0}.{1}.{2}']" -f $matches["major"],$matches["minor"],$matches["revision"] }
}
# Usage in TeamCity (ScriptSource):
& {.\psake -parameters @{base_directory="%system.teamcity.build.checkoutDir%"; solution_name="MyApp"; buildvalue="%build.number%"; insertbuildnumber= "%version.insertbuildnumber%"; forcezeros= "%version.forcezeros%"} C:\psake\assemblyinfo_buildnumber.ps1} ; if ($psake.build_success -eq $false) { exit 1 } else { exit 0 };
@derekgates
Copy link
Author

To provide a build value (the last part of the Version string), you can have TeamCity provide it using the %build.number% parameter. The Build Number format MUST be {0} so that the build counter is used.

@derekgates
Copy link
Author

$forcezeros is set to true when we are doing an ALPHA build without a known version (0.0.0.567)
$insertbuildnumber is set to false when we are doing a release (1.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment