Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of a TeamCity meta-runner that updates the build.version variable and provides other variants of version variables, such as a special version for NuGet
<?xml version="1.0" encoding="UTF-8"?>
<meta-runner name="Meta (CaioProiete): Initialize build and set custom parameters">
<description>Initialize the build and set the value of some custom parameters that can be used during the build, such as version, timestamp, and others</description>
<settings>
<parameters>
<param name="mr.initialize_build.releaseBranch" value="%build.releaseBranch%" spec="text label='Release Branch:' description='The name of the branch from where production releases are generated' display='normal' validationMode='not_empty'" />
<param name="mr.initialize_build.verbose" value="SilentlyContinue" spec="checkbox checkedValue='Continue' description='Log verbose messages?' display='normal' label='Verbose:' uncheckedValue='SilentlyContinue'" />
</parameters>
<build-runners>
<runner name="Initialize build and set custom parameters" type="jetbrains_powershell">
<parameters>
<param name="jetbrains_powershell_bitness" value="x86" />
<param name="jetbrains_powershell_errorToError" value="true" />
<param name="jetbrains_powershell_execution" value="PS1" />
<param name="jetbrains_powershell_minVersion" value="4.0" />
<param name="jetbrains_powershell_noprofile" value="true" />
<param name="jetbrains_powershell_script_code"><![CDATA[
trap
{
Write-Error $_
Invoke-Exit 1
}
function Invoke-Exit {
param([Int]$ExitCode)
[System.Environment]::Exit($ExitCode)
}
$global:DebugPreference = "%mr.initialize_build.verbose%"
$global:VerbosePreference = "%mr.initialize_build.verbose%"
Write-Verbose $("teamcity.build.branch: '{0}'" -f "%teamcity.build.branch%")
Write-Verbose $("teamcity.build.branch.is_default: '{0}'" -f "%teamcity.build.branch.is_default%")
Write-Verbose $("vcsroot.branch: '{0}'" -f "%vcsroot.branch%")
# This is just to force TeamCity to create the parameter
$currentBranch = "%build.currentBranch%"
if ("%teamcity.build.branch.is_default%" -eq "true") {
$currentBranch = "%vcsroot.branch%"
}
else {
$currentBranch = "%teamcity.build.branch%"
}
Write-Host "##teamcity[setParameter name='build.currentBranch' value='$currentBranch']"
$majorVersion = "%build.majorVersion%"
if ([String]::IsNullOrEmpty($majorVersion) -eq $true) {
$majorVersion = "1"
}
Write-Verbose $("build.majorVersion: '{0}'" -f "$majorVersion")
$minorVersion = "%build.minorVersion%"
if ([String]::IsNullOrEmpty($minorVersion) -eq $true) {
$minorVersion = "0"
}
Write-Verbose $("build.minorVersion: '{0}'" -f "$minorVersion")
$patchVersion = "%build.patchVersion%"
if ([String]::IsNullOrEmpty($patchVersion) -eq $true) {
$patchVersion = "%build.counter%"
}
Write-Verbose $("build.patchVersion: '{0}'" -f "$patchVersion")
$version = "%build.version%"
if ([String]::IsNullOrEmpty($version) -eq $true) {
$version = "$majorVersion.$minorVersion.$patchVersion.0"
}
Write-Verbose $("build.version: '{0}'" -f "$version")
$semanticVersion = "%build.semanticVersion%"
if ([String]::IsNullOrEmpty($semanticVersion) -eq $true) {
$semanticVersion = "$majorVersion.$minorVersion.$patchVersion"
}
Write-Verbose $("build.semanticVersion: '{0}'" -f "$semanticVersion")
$semanticVersionSpecial = "%build.semanticVersionSpecial%"
if (([String]::IsNullOrEmpty($semanticVersionSpecial) -eq $true) -and ([String]::IsNullOrEmpty($currentBranch) -eq $false)) {
$semanticVersionSpecial = [System.Text.RegularExpressions.Regex]::Replace($currentBranch, "[^0-9a-zA-Z\-]", "")
}
Write-Verbose $("build.semanticVersionSpecial: '{0}'" -f "$semanticVersionSpecial")
$semanticVersionSpecialForNuGet = $semanticVersionSpecial
if ([String]::IsNullOrEmpty($semanticVersionSpecialForNuGet) -eq $false) {
$semanticVersionSpecialForNuGet = $semanticVersionSpecialForNuGet.Substring(0, [System.Math]::Min(18, $semanticVersionSpecialForNuGet.Length))
}
# This is just to force TeamCity to create the parameter
$nugetVersionForDebug = "%build.nugetVersionForDebug%"
$nugetVersionForRelease = "%build.nugetVersionForRelease%"
if (([String]::IsNullOrEmpty($currentBranch) -eq $false) -and ($currentBranch -eq "%build.releaseBranch%")) {
$nugetVersionForDebug = "$semanticVersion-d"
$nugetVersionForRelease = "$semanticVersion"
}
else {
$nugetVersionForDebug = "$semanticVersion-d-$semanticVersionSpecialForNuGet"
$nugetVersionForRelease = "$semanticVersion-r-$semanticVersionSpecialForNuGet"
}
Write-Verbose $("build.nugetVersionForRelease: '{0}'" -f "$nugetVersionForRelease")
$semanticVersionFull = "%build.semanticVersionFull%"
if ([String]::IsNullOrEmpty($semanticVersionFull) -eq $true) {
if (([String]::IsNullOrEmpty($currentBranch) -eq $false) -and ($currentBranch -eq "%build.releaseBranch%")) {
$semanticVersionFull = "$semanticVersion"
}
else {
$semanticVersionFull = "$semanticVersion-$semanticVersionSpecial"
}
}
Write-Verbose $("build.semanticVersionFull: '{0}'" -f "$semanticVersionFull")
Write-Host "##teamcity[buildNumber '$semanticVersionFull']"
Write-Host "##teamcity[setParameter name='build.version' value='$version']"
Write-Host "##teamcity[setParameter name='build.semanticVersion' value='$semanticVersion']"
Write-Host "##teamcity[setParameter name='build.semanticVersionSpecial' value='$semanticVersionSpecial']"
Write-Host "##teamcity[setParameter name='build.semanticVersionFull' value='$semanticVersionFull']"
Write-Host "##teamcity[setParameter name='build.nugetVersionForDebug' value='$nugetVersionForDebug']"
Write-Host "##teamcity[setParameter name='build.nugetVersionForRelease' value='$nugetVersionForRelease']"
]]></param>
<param name="jetbrains_powershell_script_mode" value="CODE" />
<param name="teamcity.step.mode" value="default" />
</parameters>
</runner>
</build-runners>
</settings>
</meta-runner>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment