首页 > 解决方案 > 我需要对 TeamCity API 进行什么 REST 调用才能获得以下构建状态?我用的是不正确的

问题描述

在此处输入图像描述

在过去的几天里,我一直在寻找这个问题的答案。TC API 中有许多标记为“状态”的字段,而对于我来说,我无法弄清楚哪一个会返回上面列出的状态。

我现在正在打电话...

$status = (Invoke-RestMethod -Uri %teamcity.serverUrl%/httpAuth/app/rest/builds/id:%teamcity.build.id% -Method Get -Headers $header).build.status

...在 PowerShell 中。除非构建有执行超时,否则这将返回 SUCCESS。但是,正如您从上面的屏幕截图中看到的那样,构建失败了。显然,上面的调用没有返回正确的状态。失败是由于特定的构建步骤失败。如果任何构建步骤失败,则整个构建失败。不知道这是否有帮助!

标签: powershellapiteamcity

解决方案


好的,这就是发生的事情。我在 TeamCity 中有一个运行 Katalon(我们的测试自动化软件)的构建步骤。即使测试失败了,TeamCity 直到最后一步才更新构建状态,这恰好是我调用 API 的 PS 脚本,然后将信息发送到 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

推荐阅读