Skip to content

Instantly share code, notes, and snippets.

@lazytesting
Created August 17, 2016 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lazytesting/44274a0fdef5fc033598ed8eb5d8a6ea to your computer and use it in GitHub Desktop.
Save lazytesting/44274a0fdef5fc033598ed8eb5d8a6ea to your computer and use it in GitHub Desktop.
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