Skip to content

Instantly share code, notes, and snippets.

@lAnubisl
Last active April 28, 2019 14:38
Show Gist options
  • Save lAnubisl/fea08fa24929512f1fd1e47b18aa8489 to your computer and use it in GitHub Desktop.
Save lAnubisl/fea08fa24929512f1fd1e47b18aa8489 to your computer and use it in GitHub Desktop.
TeamCity Step to trigger other builds that are dependent on current one
function TriggerBuilds($buildIds) {
foreach($buildId in $buildIds) {
try {
$request = [System.Net.WebRequest]::Create("http://localhost:8111/httpAuth/app/rest/buildQueue")
$request.PreAuthenticate = $true
$request.Method = "POST"
$request.ContentType = "application/xml"
$request.Credentials = new-object system.net.networkcredential("%TeamCity_UserName%", "%TeamCity_Password%")
$encoding = new-object System.Text.ASCIIEncoding
IF("%teamcity.build.branch%" -eq "master"){
$data = $encoding.GetBytes("<build><buildType id = `"" + $buildId + "`"/></build>")
} ELSE {
$data = $encoding.GetBytes("<build><buildType id = `"" + $buildId + "`"/><properties><property name = `"teamcity.build.branch`" value = `"%teamcity.build.branch%`"/></properties></build>")
}
$request.ContentLength = $data.Length;
$requestStream = $request.GetRequestStream()
$requestStream.Write($data, 0, $data.Length)
$requestStream.Close()
$response = $request.GetResponse().GetResponseStream()
$sr = New-Object System.IO.StreamReader($response)
$respTxt = $sr.ReadToEnd()
[System.Xml.XmlDocument] $result = $respTxt
[String] $rs = $result.DocumentElement.OuterXml
Write-Host "$($rs)";
} catch {
$errorStatus = "Exception Message: " + $_.Exception.Message;
Write-Host $errorStatus;
}
}
}
IF ("%SemiVersion%" -eq ""){
TriggerBuilds %Trigger_Builds%
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment