Forked from drlongnecker/psake task for getting assembly version for TeamCity
Last active
December 11, 2015 23:18
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 }; |
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.
$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
The big difference between my fork and the original is the Out-String command which forces gc to return a large string instead of an array. This makes the regex work correctly (I could not get the original to work).
I also added sanity checks into the code as well as support for AssemblyFileVersion in case you utilize that tag.