Skip to content
mnaoumov.dev
Go back

Get TeamCity build status from PowerShell

TeamCity has REST api, so it is pretty easy to get the build status from PowerShell.

The function implemented with earliest failure detection: build will be considered as broken if either it has been completed as broken or currently running is broken.

function Test-TeamCityBuildStatus
{
    param
    (
        [string] $ServerUrl,
        [string] $UserName,
        [string] $Password,
        [string] $BuildTypeId
    )

    try
    {
        $client = New-Object System.Net.WebClient
        $client.Credentials = New-Object System.Net.NetworkCredential $UserName, $Password

        $url = "$ServerUrl/httpAuth/app/rest/buildTypes/id:$BuildTypeId/builds/canceled:false/status"

        $status = $client.DownloadString($url)

        if ($status -ne "SUCCESS")
        {
            return $false
        }
        else
        {
            $url = "$ServerUrl/httpAuth/app/rest/buildTypes/id:$buildTypeId/builds/canceled:false,running:any/status"
            $status = $client.DownloadString($url)
            $status -eq "SUCCESS"
        }
    }
    catch
    {
        return $null
    }
}

Share this post on:

Previous Post
git server-side hooks - maintenance
Next Post
Detect Console mode vs UI