Created
July 8, 2015 12:59
-
-
Save JamesWoolfenden/69bb62df3ba9cb03d112 to your computer and use it in GitHub Desktop.
Calling Teamcity builds from Powershell
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
function Get-TeamcityBuildStatus | |
{ | |
<# | |
.SYNOPSIS | |
get status of build from Teamcity server | |
.EXAMPLE | |
Get-TeamcityBuildStatus -server servername -port 81 -buildID 12 -user timmy -password time | |
#> | |
param( | |
[Parameter(Mandatory=$true)] | |
[string]$server, | |
[Parameter(Mandatory=$true)] | |
[string]$port, | |
[Parameter(Mandatory=$true)] | |
[string]$buildID, | |
[Parameter(Mandatory=$true)] | |
[string]$user, | |
[Parameter(Mandatory=$true)] | |
[string]$password) | |
$headers=@{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($user):$password"))} | |
$url="http://$($Server):$($port)/httpAuth/app/rest/builds/id:$buildid" | |
try | |
{ | |
invoke-restmethod -uri $url -Headers $headers | |
} | |
Catch | |
{ | |
throw "Failed to get build status for $buildID" | |
exit 1 | |
} | |
} | |
function wait-TeamcityBuild | |
{ | |
<# | |
.SYNOPSIS | |
waits for build from Teamcity server to finish and resports state | |
.EXAMPLE | |
wait-Teamcitybuild -server servername -port 81 -buildID 6177 -user timmy -password time | |
#> | |
param( | |
[Parameter(Mandatory=$true)] | |
[string]$server, | |
[Parameter(Mandatory=$true)] | |
[string]$port, | |
[Parameter(Mandatory=$true)] | |
[string]$buildID, | |
[Parameter(Mandatory=$true)] | |
[string]$user, | |
[Parameter(Mandatory=$true)] | |
[string]$password, | |
[int]$delay=1) | |
do | |
{ | |
write-host "$(Get-date) - waiting for buildid: $buildID " | |
sleep $delay | |
$result=Get-TeamcityBuildStatus -server $server -port $port -buildID $buildID -user $user -password $password | |
} | |
while ($result.build.state -ne "finished") | |
Write-Host "$(Get-date) - buildTypeId: $($result.build.buildTypeId) is finished" | |
switch ($result.build.status) | |
{ | |
"SUCCESS" | |
{ | |
write-host "$(Get-date) - buildTypeId: $($result.build.buildTypeId) is Successful" | |
} | |
default | |
{ | |
write-error "buildTypeId: $($result.build.buildTypeId) is failed" | |
exit 1 | |
} | |
} | |
} | |
function invoke-teamcitybuild | |
{ | |
param( | |
[Parameter(Mandatory=$true)] | |
[string]$server, | |
[Parameter(Mandatory=$true)] | |
[string]$port, | |
[Parameter(Mandatory=$true)] | |
[string]$user, | |
[Parameter(Mandatory=$true)] | |
[string]$password, | |
[Parameter(Mandatory=$true)] | |
[string]$buildTypeID) | |
$build=[xml] @" | |
<build> | |
<buildType id="$buildTypeID"/> | |
</build> | |
"@ | |
$url="http://$($server):$($port)/app/rest/buildQueue" | |
$headers=@{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($user):$password"))} | |
Write-Host "$(Get-date) - Invoke-teamcitybuild on $server with ID $buildTypeID" -ForegroundColor Yellow | |
$result=invoke-restmethod $url -method POST -body $build -Headers $headers -ContentType application/xml | |
$result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment