Reputation: 1698
How to configure azure-pipeline.yaml file in post build to get the build status? I have to get the current build status of the pipeline by using azure-devops-rest-api
or azure-devops-node-api
within the azure-pipeline.yaml file.
Please help!
Upvotes: 2
Views: 7437
Reputation: 76760
How to configure azure-pipeline.yaml file in post build to get the build status?
We could use the REST API Builds - List to get the detailed build info:
https://dev.azure.com/{organization}/{project}/_apis/build/builds?definitions={definitions}&api-version=5.1
In the YAML, we could add a powershell task to get the build result, like:
- task: PowerShell@2
inputs:
targetType : inline
script: |
$url = "https://dev.azure.com/{organization}/{project}/_apis/build/builds?definitions={definitionID}&api-version=5.1"
$connectionToken="Your PAT Here"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$buildPipeline= Invoke-RestMethod -Uri $url -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get
$BuildResult= $buildPipeline.value.result | Select-Object -first 1
Write-Host This is Build Result: $BuildResult
We list all the build result for the specify definitions, then use Select-Object -first 1
to get the latest build result.
As test, use the REST API, we could get the result of latest build for the current pipeline, but we could not get the results of the build we are performing this time.
Besides, there is a Predefined variables, which we could check the get the current build status of the pipeline by the Predefined variables Agent.JobStatus
, so I add a command line task to output this value in YAML:
- script: |
echo $(Agent.JobStatus)
This variable could get the current build status.
Hope this helps.
Upvotes: 4