0

I have a Multi-Stage YAML Pipeline along with 3 deployment stages Dev, QA and Prod. Now Using Azure DevOps REST API, I would like to fetch the latest Build Number when a particular deployment Stage got deployed successfully. For example, fetch the Build Number of the last successful QA stage deployment.

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 20 '22 at 06:01

1 Answers1

0

Build number is the name of the completed build, different stages in same build have same build number. You can use predefined variable $(Build.BuildNumber) to get the value(link).

enter image description here

If you'd like to distinct different stages, you can get stage name instead with predefined variable $(System.StageName). Sample below, combined the buildnumber and stage name.

trigger: none

stages:
- stage: Dev
  pool: 
    vmImage: 'ubuntu-20.04'
  jobs:
  - job:
    steps:
      - bash: |
         echo $(Build.BuildNumber)-$(System.StageName)


- stage: QA
  pool: 
    vmImage: 'ubuntu-20.04'
  jobs:
  - job:
    steps:
      - bash: |
         echo $(Build.BuildNumber)-$(System.StageName)

- stage: Prod
  dependsOn: [Dev,QA]
  pool:
    vmImage: 'ubuntu-20.04'
  jobs:
  - job:
    steps:
      - bash: |
          echo $(Build.BuildNumber)-$(System.StageName)

For example, the output for Dev stage:

enter image description here

Edit:

Regarding to get the last the build number of the last successful deployment of a particular deployment stage, it's more complicated.

Step1: You need to get all build url with build id in the target build definition with rest api "Builds - List", it also list the build number for each build run.

https://dev.azure.com/{org}/{project}/_apis/build/builds?definitions={definitionid}&api-version=6.1-preview.7

enter image description here

Step2: after you get all build url, you can use below rest api to check the stage result with build url(more details here.):

we use number to represent the result: 0: succeeded, 1: succeded with issues, 2: failed, 3: canceled, 4: skipped, 5: abandoned

Get https://dev.azure.com/{org}/{pro}/_build/results?buildId={id}&__rt=fps&__ver=2

enter image description here

You need to validate the stage result for each build with a loop, from high build id to lower, until it find the stage result is successful, then return the build number.

wade zhou - MSFT
  • 1,397
  • 1
  • 3
  • 6
  • Thanks for the answer. But as mentioned, I wanted to fetch the latest Build Number that has been made for a particular stage using the DevOps REST API. – Ahsan Habib Oct 20 '22 at 04:06
  • Do you want to fetch the `last` successful build number based on the stage name? Or current build number if the stage is successful? – wade zhou - MSFT Oct 20 '22 at 05:23
  • Sorry, the description might not have been clear enough. I edited it. So would like to fetch the build number of the `last successful deployment` of a `particular deployment stage`. For instance, get the last build number when `QA deployment stage` got deployed successfully. During that build phase, `Prod` may have not been deployed. – Ahsan Habib Oct 20 '22 at 09:33
  • I have updated the comment, please check. – wade zhou - MSFT Oct 21 '22 at 05:32