0

My goal is to get the build name from Azure YAML pipeline code. For example, here it is #20220803.9 JBS-2413 Do not approve yet, this is an experiment of automating the tra...

enter image description here

I looked here but no luck

https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml

I tried Build.DefinitionName it returns PR

I tried Build.BuildNumber it returns 20220803.9 only

I tried Build.SourceVersionMessage it returns Merge

TSR
  • 17,242
  • 27
  • 93
  • 197

1 Answers1

1

No built-in predefined variables or other things to get the "build name" you want. If you intercept the network traffic analysis, you will also find that the "build name" you want is not a whole, it is a combination of two data.

You need to design your own code to get it. For example, if you are based on Azure Git Repo, below pipeline YAML can help you get the "build name" you want when creating a pull request to trigger the pipeline:

trigger:
- none


# 1
stages:
- stage: s1
  displayName: Get the lastest comment
  jobs:
    - job: testJob
      steps:
        - task: PowerShell@2
          name: setvar
          inputs:
            targetType: 'inline'
            script: |
              
              $PAT = "<Your Personal Access Token>"
              
              $org_name = "<Organization Name>"
              $project_name = "<Project Name>"
              $branch_name = "<Current Branch Name>"
              $def_id = "<Pipeline definition ID>"
              $run_id = "$(Build.BuildId)"
              Write-Host $run_id

              if("$(Build.Reason)" -eq "PullRequest") {
                  $headers_run = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
                  $headers_run.Add("Authorization", "Basic "+$PAT)
                  $run_url = "https://dev.azure.com/"+$org_name+"/"+$project_name+"/_apis/pipelines/"+$def_id+"/runs/"+$run_id+"?api-version=6.0-preview.1"
                  $response_run = Invoke-RestMethod $run_url -Method 'GET' -Headers $headers_run
                  $response_run | ConvertTo-Json
                  $pull_request_id = $response_run.variables."system.pullRequest.pullRequestId".value

                  $headers_pr = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
                  $headers_pr.Add("Authorization", "Basic "+$PAT)
                  $pr_url = "https://dev.azure.com/"+$org_name+"/"+$project_name+"/_apis/git/repositories/ShowBuildName/pullrequests/"+$pull_request_id+"?api-version=6.0"
                  $response_pr = Invoke-RestMethod $pr_url -Method 'GET' -Headers $headers_pr
                  $response_pr | ConvertTo-Json
                  Write-Host $response_pr.title

                  $str = $response_pr.title
                  Write-Host "##vso[task.setvariable variable=outputvars;isOutput=true]$str"
              }
              else {
                  $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
                  $headers.Add("Authorization", "Basic "+$PAT)

                  $url = "https://dev.azure.com/"+$org_name+"/"+$project_name+"/_apis/git/repositories/ShowBuildName/commits?searchCriteria.itemVersion.version="+$branch_name+"&api-version=6.0"

                  $response = Invoke-RestMethod $url -Method 'GET' -Headers $headers
                  $response | ConvertTo-Json

                  $str = $response.value[0].comment
                  Write-Host "##vso[task.setvariable variable=outputvars;isOutput=true]$str"
              }


# 2
- stage: s2
  displayName: Get the "build name"
  dependsOn: s1  
  variables:
   vars: $[ stageDependencies.s1.testJob.outputs['setvar.outputvars'] ]
  jobs:
    - job:
      steps:  
      - task: PowerShell@2
        inputs:
          targetType: 'inline'
          script: |
            Write-Host "$(Build.BuildNumber) $(vars)"

Pipeline run:

enter image description here

Successfully get the "build name":

enter image description here

Bowman Zhu-MSFT
  • 4,776
  • 1
  • 9
  • 10