如何使用API调用以结构化格式获取Pipeline rawlog?

问题描述 投票:0回答:1

我能够使用 API 调用以非结构化格式获取管道中的日志数据。

我想使用单个 GET API 调用以结构化格式获取管道日志数据

$url = "https://dev.azure.com/$organization/$project/_apis/build/builds/{buildId}/logs/{$logId}?api-version=7.1-preview"

这是我获取非结构化格式的管道原始日志的参考 API。

我想通过单个 API 调用 (GET) 获取结构化格式的输出。

azure-devops azure-pipelines
1个回答
0
投票

从评论中共享的示例来看,您似乎需要从脚本中提取实际输出并忽略任务的默认输出日志。

恐怕没有一个Rest API可以直接获取实际的输出日志。目前仅支持输出整个任务日志。

为了满足您的要求,我建议您使用PowerShell正则表达式来一一提取每个元素。

这是一个例子:

管道输出:

enter image description here

PowerShell 脚本示例:

$token = "PAT"

$url="https://dev.azure.com/{OrgName}/{ProjectName}/_apis/build/builds/{BuildId}/logs/{logid}"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get  -ContentType application/json

$regex = 'Users(\s([A-Za-z0-9]+\s)+)days'
$Inactiveday= [regex]::matches($response, $regex).value
$regex = '"UserPrincipalName":"[A-Za-z]+"'
$UserPrincipalName= [regex]::matches($response, $regex).value
$regex = '"HostPoolName": "[A-Za-z]+"'
$HostPoolName= [regex]::matches($response, $regex).value
$regex = '"SessionHostName": "[A-Za-z]+"'
$SessionHostName= [regex]::matches($response, $regex).value
$regex = '"DaysInactive": [0-9]*\.[0-9]+'
$DaysInactive= [regex]::matches($response, $regex).value
$regex = '"LastLogin": "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?([Zz]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?"'
$LastLogin= [regex]::matches($response, $regex).value


echo $Inactiveday
$Output= "
{
  $UserPrincipalName,
  $HostPoolName,
  $SessionHostName
  $DaysInactive,
  $LastLogin
}"
echo $Output

结果:

enter image description here

注意: 您需要根据实际输出生成通配符。您可以使用此站点来转换正则表达式:Regex Generator

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.