Reputation: 155
Requirement: Need to capture the current or running build pipeline result using REST API at the end of the same build pipeline.
Challenge: I'm at half stage, I'm having challenge in getting the final stage' result for that particular running build at the end of the same build pipeline.
Example code snippet:
$personalAccessToken=(Get-AzureKeyVaultSecret -VaultName $keyVaultName -Name $secretname).SecretValueText
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
$header = @{Authorization=("Basic {0}" -f $token)}
$projectsUrl = "https://dev.azure.com/$AzureDevopsAccount/$Project/_apis/build/builds?api-version=5.0&resultFilter=all&definitions=$definition"
$projects = Invoke-RestMethod -Uri $projectsUrl -Method Get -Headers $header
Write-Host "Pipeline = $($projects.value.result| ConvertTo-Json -Depth 1)"
Using this code, I'm able to capture the result for all pipelines. I just need to know how to get the status of the running build at the end of pipeline completion.
Note: As I have 3 different build pipelines, I need to be able to separately capture this result for all 3 builds at the end of each build pipeline.
Any suggestions will be appreciated. Thanks.
Upvotes: 1
Views: 1845
Reputation: 19026
Get the Current/Running Build final stage results at the end of the pipeline as post job/task using REST API
To get the stage results, please use below api which does not documented and you can get it from F12
:
Get https://dev.azure.com/{org}/{project}/_build/results?buildId=$(Build.BuildId)&__rt=fps&__ver=2
Powershell script:
$token = "{token}"
$url =" https://dev.azure.com/{org}/{project}/_build/results?buildId=$(Build.buildid)&__rt=fps&__ver=2"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get
Write-Host "results = $($response | ConvertTo-Json -Depth 100)"
Then you can get the stage results from its response.
In our system, we use number to represent the result: 0: succeeded, 1: succeded with issues, 2: failed, 3: canceled, 4: skipped, 5: abandoned
Note: As what you want, you just want to get the final stage result instead of all stages of current pipeline. I need to say, until now, there no directly way can achieve that. You must specify the stage name to filter out the result code of final stage. Here has a sample on how to filter.
as post job/task
As you know, post-job is a system build-in task which used to clean the environments. If you want to add a similar task and set as a post job, you need add a customized extension: Use a decorator to inject steps into a pipeline.
I have developed this extension for myself, and upload it into my github so that you can refer to my repos(its just a simple sample).
In its definition, you just need to paste the above powershell script into my-decorator.yml file.
At this time, the powershell script which used to prompt the final stage result can set as a post job in your pipeline.
Hope my extension can help you.
Upvotes: 5