praveen krish
praveen krish

Reputation: 155

Get the Current/Running Build final stage results at the end of the pipeline as post job/task using REST API

Requirement: Need to capture the current or running build pipeline result using REST API at the end of the same build pipeline.

  1. I have 3 build pipelines for 3 different environments and each build having 3 different stages(Stage1, Stage2, Stage3).
  2. I need to get the current running build's final stage (Stage3) results (whether succeeded/failed).
  3. I need to get that result information post final stage, i would like to run PS script as next task/job or Post job to capture the result of final stage whether it has passed/failed using Rest API.
  4. I have PS script ready and I would like to know exact API which can be used for this scenario.

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

Answers (1)

Mengdi Liang
Mengdi Liang

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.

enter image description here

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

Related Questions