1

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.

Spevacus
  • 584
  • 2
  • 13
  • 23
praveen krish
  • 135
  • 2
  • 11

1 Answers1

4

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.

Mengdi Liang
  • 17,577
  • 2
  • 28
  • 35
  • Hi Merlin, thanks for your comment. I'm getting the error as "Build.buildid : The term 'Build.buildid' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again." will that not automatically parse the Build ID or do i need to specifically assign value for it? And how does the script take the current build's Build ID ? – praveen krish Jan 09 '20 at 06:54
  • @praveenkrish, Nope, Build.BuildId is a [predefined variable](https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables) which can be get automatically during the build running. Did you run above api in VSTS pipeline? If you run it in local powershell command line, you need assign its value. Because, as I said, it is a predefined variable in VSTS, and can be get automatically during the build running. – Mengdi Liang Jan 09 '20 at 06:59
  • @praveenkrish, You would see the stages info under ftp -> dataproviders -> data -> ms.vss-build-web.run-details-data-provider -> stages. – Mengdi Liang Jan 09 '20 at 09:01
  • Ok Thanks @MerlinLiang, accidentally my previous comment has been removed. I do see it now but under different place, but it seems like i need to dig deep in to lot of content of Json. If i need to capture this value in to variable, seems it will be heavy $response.ftp.dataprovides....stages. Because i need to send this value to Log Analytics for projecting in Dashbaord. Do you have any suggestion on that part pls ? – praveen krish Jan 09 '20 at 09:10
  • @praveenkrish. It's okay, I've got your trouble. I know its very inconvenient and trouble to get the specified value by using $response.ftp.xxxxx, but until now, there's no another light way to get that. Because we haven't develop and public the corresponding api(or quick pre-defined variable) to let users can get stage result directly. You know, the multi-stage feature are just in product less one year ago. We are trying our best to improve the experience of this usage. What about create a seprate variable to get its value and then pass it into your log analytics? – Mengdi Liang Jan 09 '20 at 09:21
  • Yes, even i thought of the same option but how do i get that value in to a variable that is where i'm stuck Merlin. Because if i use Get-Builds API again it gets all the build results not the current or running one right? Even I would be happy if i can get the entire current build result at the end of the final stage so that i can pass on tat value in to Variable. – praveen krish Jan 09 '20 at 09:24
  • @praveenkrish. I'm afraid, you could not avoid that heavy expression since the result of our API is a JSON body, and as I know you can only get one specified content via $xxx.xxx.xxx... (Like this https://stackoverflow.com/questions/58597755/import-azure-devops-pipeline-variables-from-json-file) In another, the Get-Builds api can only get the complete build, it does not display the details of builds. We are improving the supplement api, if you don't mind, you could raise suggestion ticket [here](https://developercommunity.visualstudio.com/content/idea/post.html?space=21). – Mengdi Liang Jan 09 '20 at 09:38
  • Liang, I'm able to get the result value from Json content and storing it in variable then passing that in to Log Analytics successfully. Thanks heaps for your help and suggestions. It was very helpful indeed. – praveen krish Jan 09 '20 at 13:19
  • @praveenkrish Awesome to hear you have the guide on your puzzle solved! If you think this answer has help to you, you could [accept it as answer](https://meta.stackexchange.com/q/5234/541930) so that others who has the same puzzle with you could directly know whether this answer is work. – Mengdi Liang Jan 12 '20 at 09:55
  • @MengdiLiang - would you happen to know what Scopes are required to access this API? I have a full access PAT that works, but would like to reduce it. Ideally I would like to pass in the `System.AccessToken` from the Azure DevOps pipeline, but it doesn't have permission. – Brandon Boone Jan 06 '22 at 14:00
  • 1
    @BrandonBoone If you'd like to use **System.AccessToken** to perform above script in pipelines, then you need to follow this [doc](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/access-tokens?view=azure-devops&tabs=classic) to configure the permission of this system token in your organization settings/project settings page. – Mengdi Liang Jan 08 '22 at 07:37