Skip to content

Instantly share code, notes, and snippets.

@munkyjunky
Last active July 22, 2020 18:21
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save munkyjunky/44fba7ab7b36c4dbeeab95dec2c125a3 to your computer and use it in GitHub Desktop.
Save munkyjunky/44fba7ab7b36c4dbeeab95dec2c125a3 to your computer and use it in GitHub Desktop.
Powershell script to trigger a TeamCity build from Octopus Deploy, and wait for the build to finish
# Powershell script to trigger a Team City build from Octopus Deploy, and wait for it to be finished
# Use case for this is running an automated script (such as test scripts) from Team City as part of
# a deployment, and holding the deployment until the script has finished
# teamcity-host teamcity host url
# teamcity-username teamcity api username
# teamcity-password teamcity api password
# teamcity-build-configuration-id teamcity build configuration
# teamcity-changeset (optional) changeset to build from - NOTE: this is a VCS changeset
# teamcity-env-variables (optional) environment variables to be passed to TeamCity, newline seperated
# teamcity-branch (optional) branch to build
## Variables
$pollInterval = 30
$buildState = "unknown"
$username = "#{teamcity-username}"
$password = "#{teamcity-password}"
$branch = ""
$buildConfigurationId = "#{teamcity-build-configuration-id}"
$teamcityHost = "#{teamcity-host}"
$changeset = ""
$teamcityEnvVariables = ""
# Check if these are set, since they're optional
if($OctopusParameters -and $OctopusParameters.ContainsKey("teamcity-branch") )
{ $branch = $OctopusParameters["teamcity-branch"] }
if($OctopusParameters -and $OctopusParameters.ContainsKey("teamcity-changeset") )
{ $changeset = $OctopusParameters["teamcity-changeset"] }
if($OctopusParameters -and $OctopusParameters.ContainsKey("teamcity-env-variables") )
{ $teamcityEnvVariables = $OctopusParameters["teamcity-env-variables"] }
function getAuthHeader($user, $pass) {
$pair = "$($user):$($pass)"
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
$basicAuthValue = "Basic $base64"
return $basicAuthValue
}
function getParams($vars) {
$arr = $vars -split '\n'
$buffer = "";
foreach ($item in $arr) {
$key,$val = $item.split('=');
if (![string]::IsNullOrWhiteSpace($key) -and ![string]::IsNullOrWhiteSpace($val)) {
$buffer += $('<property name="'+ $key +'" value="' + $val +'"/>')
}
}
return $buffer;
}
function getBranchName($branch) {
if (![string]::IsNullOrWhiteSpace($branch)) {
return $('branchName="' + $branch + '"')
} else {
return "";
}
}
function getChangeset($changeset) {
if (![string]::IsNullOrWhiteSpace($changeset)){
return $('<lastChanges><change id="' + $changeset + '"/></lastChanges>')
} else { return ""; }
}
# Request object
[xml]$request = $('
<build ' + $(getBranchName $branch) + '>
<buildType id="' + $buildConfigurationId + '"/>
<triggeringOptions queueAtTop="true"/>
<comment>
<text>Triggered from deployment #{Octopus.Release.Number} of #{Octopus.ProjectGroup.Name}/#{Octopus.Project.Name} by #{Octopus.Deployment.CreatedBy.DisplayName}</text>
</comment>
<properties>' + $(getParams $teamcityEnvVariables) + '</properties>
' + $(getChangeset $changeset) + '
</build>
')
# Send POST to TeamCity to trigger the build, store the returned status URL
[xml]$response = Invoke-WebRequest -UseBasicParsing -Method Post -Uri $($teamcityHost + "/httpAuth/app/rest/buildQueue") -Body $request -Headers @{ Authorization = $(getAuthHeader $username $password) } -ContentType "application/xml"
# Failed to queue build
if ($response.build.state -ne "queued") {
Write-Host "Failed to queue build"
exit 1;
} else {
Write-Host $("Build started with ID " + $response.build.id)
}
# Poll the status URL until a success or failure
while ($buildState -ne "finished") {
start-sleep -seconds $pollInterval
# GET the status
[xml]$build = Invoke-WebRequest -UseBasicParsing -Method Get -Uri $($teamcityHost + $response.build.href) -Headers @{ Authorization = $(getAuthHeader $username $password) } -ContentType "application/xml"
$buildState = $build.build.state
Write-Host $("Build ID "+ $response.build.id + " state: " + $buildState)
}
write-host $("Build finished with status: " + $build.build.status)
# Exit 1 if the build failed
if ($build.build.status -eq "SUCCESS") { exit 0 }
# Assume a failure, exit 1
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment