Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
notify slack on failed teamcity build
function Test-TeamCityBuildStatus
{
param
(
[string] $ServerUrl ="[TC server url]",
[string] $UserName = "[tc user]",
[string] $Password = "[TC password]",
[string] $BuildTypeId = "[build id]"
)
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
}
}
function Notify-Slack($payload)
{
Invoke-WebRequest `
-Uri "[SLACK HOOK URL]" `
-Method "POST" `
-Body (ConvertTo-Json -Compress -InputObject $payload)
}
$result = Test-TeamCityBuildStatus;
if (!$result)
{
$payload = @{
"channel" = "[#Channel]";
"icon_emoji" = ":bomb:";
"text" = "<!channel> Deployment to CI has failed!!!";
"username" = "Teamcity";}
Notify-Slack -payload $payload
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.