powershell - 我需要对 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。但是,正如您从上面的屏幕截图中看到的那样,构建失败了。显然,上面的调用没有返回正确的状态。失败是由于特定的构建步骤失败。如果任何构建步骤失败,则整个构建失败。不知道这是否有帮助!
解决方案
好的,这就是发生的事情。我在 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
推荐阅读
- c# - 具有 DeserializeObject 的动态类提供错误:找不到用于类型的默认构造函数
- mysql - 通过 MongoDB BI 连接器连接到 Tableau 时出错
- node.js - 如何在 vs-code 中调试安装在 bot (botpress) 中的 botpress 模块?
- android - 尝试在空对象引用上调用虚拟方法“boolean android.media.AudioManager.isBluetoothA2dpOn()”
- jquery - 如何在点击时隐藏手风琴
- pact - 在提供者服务中,我们可以有两个或多个具有相同状态或提供者状态的交互吗?
- reactjs - 如何使用 react 和 typescript 解决程序化导航问题
- r - 分层数据聚合和操作
- android - 获取 java.lang.SecurityException:DownloadManager:用户 10173 和当前进程都没有 android.permission.ACCESS_ALL_DOWNLOADS
- list - 如何在 q# 中选择列表的唯一元素?