因此,我可以帮助您解决如何从构建日志中捕获错误并将其发送到Slack。
假设我们将在生成错误之后将此任务作为下一个任务运行。
system_access token
以便您可以进行身份验证。[[error]]
线条- pwsh: |
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $env:System_AccessToken)))
$uriLogs = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/builds/$env:BUILD_BUILDID/logs?api-version=5.1"
$response = Invoke-RestMethod -Uri $uriLogs -Method 'GET' -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
$logUrl =$response.value[-1].url
$webContent = Invoke-WebRequest -Uri $logUrl -Method 'GET' -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
$ErrorMessage = $webContent.Content.tostring() -split "[`r`n]" | select-string "[[Error]]"
Write-Output "Error lines found " $ErrorMessage
$postSlackMessage = @{token=$env:SLACK_TOKEN;channel="@user,#channel-name";text=$ErrorMessage}
Invoke-RestMethod -Uri https://slack.com/api/chat.postMessage -Body $postSlackMessage
env:
system_accesstoken: $(System.AccessToken)
condition: failed()
其他选择:
startLine
和endLine
参数在这里。
感谢您的评论-非常感谢。我出去一个星期了,所以今天早上才看
没问题,很乐意帮忙!