azure - 如何在 Azure 上获取构建名称?

标签 azure continuous-integration azure-pipelines pipeline azure-yaml-pipelines

我的目标是从 Azure YAML 管道代码获取构建名称。 例如,这里是#20220803.9 JBS-2413 尚未批准,这是一个自动化交易的实验...

enter image description here

我看了这里,但没有运气

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

我尝试了 Build.DefinitionName 它返回 PR

我尝试了 Build.BuildNumber 它仅返回 20220803.9

我尝试了Build.SourceVersionMessage它返回Merge

最佳答案

没有内置的预定义变量或其他东西来获取您想要的“构建名称”。如果你拦截网络流量分析,你还会发现你想要的“构建名称”并不是一个整体,它是两个数据的组合。

您需要设计自己的代码才能获得它。例如,如果您基于 Azure Git Repo,下面的管道 YAML 可以帮助您在创建拉取请求以触发管道时获取所需的“构建名称”:

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)"

管道运行:

enter image description here

成功获取“构建名称”:

enter image description here

关于azure - 如何在 Azure 上获取构建名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73224059/

相关文章:

Docker "Login succeeded"但仍无法推送到 GCP 私有(private)注册表

tfs - 如何清理多个构建代理上的源代码

powershell - 为什么 Login-AzureRmAccount 没有保留在 PowerShell session 中?

azure - 在 Azure webjob 的 Appinsights 中添加新的自定义指标,例如 "Memory Usage"

c# - 可能的 HeroCard 或 Azure 测试 Web 聊天图像缓存问题

docker - CI上kubectl在交互模式下运行失败

java - Hudson 的替代构建管理器

azure - 如何在 Azure Devops VSO 上进行调试

docker - VSTS构建管道如何共享docker任务的输出?

ios - 使用无法在 azure 通知中心工作的标签注册推送通知