我需要对 TeamCity API 进行什么 REST 调用才能获得下面的构建状态?我用的那个不对
What is the REST call I need to make to the TeamCity API to get this build status below? The one I'm using is not correct
过去几天我一直在寻找这个问题的答案。在 TC API 中有许多标记为“状态”的字段,对于我来说,我无法弄清楚上面列出的状态 return 是哪一个。
我正在打电话...
$status = (Invoke-RestMethod -Uri %teamcity.serverUrl%/httpAuth/app/rest/builds/id:%teamcity.build.id% -Method Get -Headers $header).build.status
...在 PowerShell 中。这 return 成功,除非构建有执行超时。但是,正如您从上面的屏幕截图中看到的那样,构建失败了。显然,上面的调用不 return 正确的状态。失败是由于特定的构建步骤失败。如果任何构建步骤失败,则整个构建失败。不确定是否有帮助!
问题似乎不在端点本身。您正在请求正确的字段。也可以通过以下请求完成同样的操作:
GET /app/rest/builds?locator=id:%teamcity.build.id%&fields=build(status)
GET /app/rest/builds/%teamcity.build.id%/status
第一个请求将 return 你一个 XML 或 JSON 结构取决于你的 Accept
header。第二个响应将 return 您是纯文本,因此相应地设置 headers。
This returns SUCCESS unless the build has an execution timeout. However, as you can see from the screenshot above, the build failed. Obviously, the call above doesn't return the right status. The failure is due to a specific build step failing. If any of the build steps fail, then the entire build fails.
您正在构建执行期间请求构建状态。有可能,当检查状态的构建步骤是 运行 时,构建的状态仍然是 SUCCESS。尝试将构建步骤移动到此构建的最后并将其配置为执行 'Always, even if build stop command was issued'
好的,这就是发生的事情。我在 TeamCity 中有一个构建步骤 运行s Katalon(我们的测试自动化软件)。即使测试失败,TeamCity 直到最后一步才更新构建状态,这恰好是我的 PS 脚本调用 API 然后将信息发送给 MS Teams。我必须在 TeamCity 中为 Katalon 构建步骤设置失败条件,该步骤在构建日志中查找特定文本“(Katalon) 失败”。这会立即导致构建失败,但构建步骤的其余部分会继续 运行。现在,当我的最后一步 运行 时,它会提取正确的状态并将其推送到 MS Teams。对于任何感兴趣的人,这是该构建步骤的完整 PowerShell 脚本。
function Get-AuthenticationHeader
{
param($User, $Password)
$pair = "$($User):$($Password)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
"Basic $encodedCreds"
}
$authHeader = Get-AuthenticationHeader -User "%system.teamcity.auth.userId%" -Password "%system.teamcity.auth.password%"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
$header = @{
"Authorization"="$authHeader"
}
$response = (Invoke-RestMethod -Method GET -Headers $header -ContentType "application/json" -Uri "%teamcity.serverUrl%/httpAuth/app/rest/builds/%teamcity.build.id%")
$status = $response.build.status
$name = "%system.teamcity.buildConfName%"
$teamcityAgent = "%teamcity.agent.name%"
$testResults = $response.build.webUrl
Write-Host "%teamcity.build.id%"
Write-Host $status
Write-Host $name
Write-Host $testResults
if ( $status -eq "SUCCESS" ) {
$color = "00ff00"
}
else {
$status = "FAILURE"
$color = "ff0000"
}
$body = @{
title= "API Test Automation";
text= "<pre><b><i><font color=$color>$status</font></i></b> - $name<br>Tests ran on <i>$teamcityAgent</i><br>Test results are <a href=""$testResults"">HERE</a></pre>"
} | ConvertTo-Json
Invoke-WebRequest -Method POST -ContentType "application/json" -Uri "<YOUR MS TEAMS WEBHOOK HERE>" -Body $body -UseBasicParsing
过去几天我一直在寻找这个问题的答案。在 TC API 中有许多标记为“状态”的字段,对于我来说,我无法弄清楚上面列出的状态 return 是哪一个。
我正在打电话...
$status = (Invoke-RestMethod -Uri %teamcity.serverUrl%/httpAuth/app/rest/builds/id:%teamcity.build.id% -Method Get -Headers $header).build.status
...在 PowerShell 中。这 return 成功,除非构建有执行超时。但是,正如您从上面的屏幕截图中看到的那样,构建失败了。显然,上面的调用不 return 正确的状态。失败是由于特定的构建步骤失败。如果任何构建步骤失败,则整个构建失败。不确定是否有帮助!
问题似乎不在端点本身。您正在请求正确的字段。也可以通过以下请求完成同样的操作:
GET /app/rest/builds?locator=id:%teamcity.build.id%&fields=build(status)
GET /app/rest/builds/%teamcity.build.id%/status
第一个请求将 return 你一个 XML 或 JSON 结构取决于你的 Accept
header。第二个响应将 return 您是纯文本,因此相应地设置 headers。
This returns SUCCESS unless the build has an execution timeout. However, as you can see from the screenshot above, the build failed. Obviously, the call above doesn't return the right status. The failure is due to a specific build step failing. If any of the build steps fail, then the entire build fails.
您正在构建执行期间请求构建状态。有可能,当检查状态的构建步骤是 运行 时,构建的状态仍然是 SUCCESS。尝试将构建步骤移动到此构建的最后并将其配置为执行 'Always, even if build stop command was issued'
好的,这就是发生的事情。我在 TeamCity 中有一个构建步骤 运行s Katalon(我们的测试自动化软件)。即使测试失败,TeamCity 直到最后一步才更新构建状态,这恰好是我的 PS 脚本调用 API 然后将信息发送给 MS Teams。我必须在 TeamCity 中为 Katalon 构建步骤设置失败条件,该步骤在构建日志中查找特定文本“(Katalon) 失败”。这会立即导致构建失败,但构建步骤的其余部分会继续 运行。现在,当我的最后一步 运行 时,它会提取正确的状态并将其推送到 MS Teams。对于任何感兴趣的人,这是该构建步骤的完整 PowerShell 脚本。
function Get-AuthenticationHeader
{
param($User, $Password)
$pair = "$($User):$($Password)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
"Basic $encodedCreds"
}
$authHeader = Get-AuthenticationHeader -User "%system.teamcity.auth.userId%" -Password "%system.teamcity.auth.password%"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
$header = @{
"Authorization"="$authHeader"
}
$response = (Invoke-RestMethod -Method GET -Headers $header -ContentType "application/json" -Uri "%teamcity.serverUrl%/httpAuth/app/rest/builds/%teamcity.build.id%")
$status = $response.build.status
$name = "%system.teamcity.buildConfName%"
$teamcityAgent = "%teamcity.agent.name%"
$testResults = $response.build.webUrl
Write-Host "%teamcity.build.id%"
Write-Host $status
Write-Host $name
Write-Host $testResults
if ( $status -eq "SUCCESS" ) {
$color = "00ff00"
}
else {
$status = "FAILURE"
$color = "ff0000"
}
$body = @{
title= "API Test Automation";
text= "<pre><b><i><font color=$color>$status</font></i></b> - $name<br>Tests ran on <i>$teamcityAgent</i><br>Test results are <a href=""$testResults"">HERE</a></pre>"
} | ConvertTo-Json
Invoke-WebRequest -Method POST -ContentType "application/json" -Uri "<YOUR MS TEAMS WEBHOOK HERE>" -Body $body -UseBasicParsing