Last active
August 22, 2018 03:02
-
-
Save feanz/6da94c0a04a9093b77cf5e1b9bd086b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Set-StrictMode -Version Latest | |
#change these values to your own setup | |
$webhockUrl = "Slack Web hock" | |
$teamcityUsername = "username" | |
$teamcityPassword = "password" | |
$teamcityUrl = "teamcity URL" | |
$teamcityBuildNumber = %build.number% | |
$teamcityBuildId = %teamcity.build.id% | |
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" | |
Write-Host "Calling teamcity server to get build status $($url)" | |
$status = $client.DownloadString($url) | |
if ($status -eq "SUCCESS") { | |
Write-Host "Response from teamcity success" | |
return $false | |
} | |
else { | |
$url = "$ServerUrl/httpAuth/app/rest/buildTypes/id:$buildTypeId/builds/canceled:false,running:any/status" | |
Write-Host "Calling teamcity server to try and check current running build $($url)" | |
$status = $client.DownloadString($url) | |
Write-Host $status | |
if($status -eq "SUCCESS") | |
{ | |
Write-Host "Response from teamcity success" | |
return $false | |
} | |
else { | |
Write-Host "Response from teamcity failure" | |
return $true; | |
} | |
} | |
} | |
catch { | |
$ErrorMessage = $_.Exception.Message | |
Write-Host $ErrorMessage | |
return $null | |
} | |
} | |
$failed = Test-TeamCityBuildStatus -ServerUrl $teamcityUrl -UserName $teamcityUsername -Password $teamcityPassword -BuildTypeId $teamcityBuildId | |
$buildStatus = If ($failed) {"Failed"} Else {"Sucess"} | |
Write-Host "Build status: $($buildStatus)" | |
if ($failed) { | |
try { | |
$payload = @{ | |
"attachments" = @(@{ | |
"fallback" = "Teamcity build failed" | |
"title" = "Teamcity build failed!" | |
"title_link" = "$($teamcityUrl)/viewLog.html?buildId=$($teamcityBuildNumber)&tab=buildResultsDiv&buildTypeId=$($teamcityBuildId)" | |
"color" = "#ed1515" | |
"text" = "The latest build has failed. Fix it!" | |
}) | |
} | |
Invoke-WebRequest ` | |
-Body ($payload|ConvertTo-Json) ` | |
-Method 'Post' ` | |
-Verbose ` | |
-Uri $webhockUrl | Out-Null | |
} | |
catch { | |
Write-Host "Error calling slack webhock" | |
Write-Host "Response Status code $($_.Exception.Response.StatusCode.Value__)" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment