ict.ken.be

 

Stop Teamcity build when other build starts

Related Posts

Categories: Windows

Most likely you want to stop running your long end-2-end tests when a new deployment is ready.
You will lose the correlation between when the error happened, but at least you can move on.
The easiest way to do this in Teamcity (as there is no support at this writing),
is to add an additional step before the deployment build configuration using the following powershell script.

So you basically cancel the test build configuration when the deployment starts.

$buildConfigurationId = '%E2ETestBuildConfigurationId%'
If (![string]::IsNullOrWhitespace($buildConfigurationId)) { 
    Write-Host "Finding test builds"
    $user = '%system.teamcity.auth.userId%'
    $pass = '%system.teamcity.auth.password%'
    $pair = "${user}:${pass}"
    $bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
    $base64 = [System.Convert]::ToBase64String($bytes)
    $basicAuthValue = "Basic $base64"
    $headers = @{ Authorization = $basicAuthValue; "Content-Type" = "application/xml" }
    $response = Invoke-WebRequest -Uri "http://teamcity.conseur.org/app/rest/builds?locator=buildType:($buildConfigurationId),state:queued" -Method GET -Headers $headers
    $postParams = "<buildCancelRequest comment='Build was canceled because of new deployment.' readdIntoQueue='true' />"
    Foreach($buildId in ([xml]$response.Content).builds.build.id) 
    {
        Write-Host "Cancel queued buildId: $buildId"
        Invoke-WebRequest -Uri "http://teamcity.conseur.org/app/rest/buildQueue/id:$buildId" -Headers $headers -Method POST -Body $postParams
    }
    $response = Invoke-WebRequest -Uri "http://teamcity.conseur.org/app/rest/builds?locator=buildType:($buildConfigurationId),state:running" -Method GET -Headers $headers
    $postParams = "<buildCancelRequest comment='Build was canceled because of new deployment.' readdIntoQueue='false' />"
    Foreach($buildId in ([xml]$response.Content).builds.build.id) 
    {
        Write-Host "Cancel running buildId: $buildId"
        Invoke-WebRequest -Uri "http://teamcity.conseur.org/app/rest/builds/id:$buildId" -Headers $headers -Method POST -Body $postParams
    }
}