如何通过管道构建 ID 在 ymal json 中获取工件构建版本

时间:2021-01-10 07:20:30

标签: powershell yaml azure-devops-rest-api

我正在开发一个 Ymal 管道,我正在使用另一个管道中的构建工件。我可以使用以下代码下载工件:

- task: DownloadBuildArtifacts@0  

   inputs:       

   buildType:specific 

   project: Test Project 

   pipeline: TestInfrastructure-Rel

   branchName: 'refs/heads/release/preview'  

   downloadType: 'single'

   artifactName: Testartifact Packages

   buildVersionToDownload: 'latest'

   downloadPath: '$(System.DefaultWorkingDirectory)'

在上面的代码中,我知道我可以设置属性 buildId 以通过此链接获取构建 ID https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/download-build-artifacts?view=azure-devops

虽然我想知道包版本是什么。我正在使用这个 API 从特定构建中获取信息,但工件版本号没有出现。 https://dev.azure.com/{Org 名称}/{项目名称}/_apis/build/builds/{buildid}/timeline/?api-version=5.1

基本上,我必须每天根据需要所有信息的特定管道运行创建报告。

我还想通知您,我通过资源在管道中有包版本,尽管我无法将其设置为变量。如果我在任何变量中设置包版本,它不会显示在构建管道 ymal json 中。当我使用 https://dev.azure.com/{Org name}/{Project name}/_apis/build/builds/{buildid}/timeline/?api-version=5.1 运行 API 时,我没有在任务下方看到任何包版本图片。如果我们可以从任何属性或任何东西的 ymal 管道中设置版本,以便我可以在下图中获得该版本。

enter image description here

如果有人可以帮助我或指导我如何使用变量或 PowerShell 或 azure PowerShell 任务中的任何其他属性设置或传递版本号,那将非常有帮助。

谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用 pipeline sources 从另一个管道使用构建工件。然后您可以通过引用预定义变量 resources.pipeline.<Alias>.runName 来获取工件构建版本。见下例:

resources:
  pipelines:
  - pipeline: sourcepipeline  # can be any string, this is the Alias in the predefined variables. identifier for the resource used in pipeline resource variables
    project: Test Project # project for the source; optional for current project
    source: TestInfrastructure-Rel  # name of the pipeline that produces an artifact
    branch: refs/heads/release/preview

pool: 
  vmImage: vs2017-win2016

steps:
- powershell: |
     echo "$(resources.pipeline.sourcepipeline.runName)"  #get the artifact build number
     echo "$(resources.pipeline.sourcepipeline.runId)"    #get the artifact build id

使用管道源中定义的工件。您只需要在 yaml 管道中添加下载任务。见here

- download: sourcepipeline

来自关联管道资源的工件下载到 $(Pipeline.Workspace)/<pipeline resource identifier>/

如果您想使用 DownloadBuildArtifacts 任务和 rest api 来获取工件内部版本号。您可以拨打Build get rest api。获得 buildNumber 后,您可以使用 logging commands ##vso[task.setvariable variable=buildVersion]<buildVersion> 将其设置为变量。见下例:

- powershell: |
        $url = $(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds/<build.id>?api-version=5.1"

        $rawResponse = Invoke-RestMethod -UseDefaultCredentials -Uri $url -Method Get -ContentType "application/json" -Headers @{Authorization = "Bearer $(system.accesstoken)"}
        
        #pass build number to a variable 
        echo "##vso[task.setvariable variable=buildVersion]$($rawResponse.buildNumber)"
   

更新:

您可以使用 logging command 来记录警告。并将包版本设置为警告消息。然后包版本将显示在时间线 rest api 中。见下文:

添加一个 powershell 任务来运行日志命令:

steps:
- powershell: |
      echo "##vso[task.logissue type=warning]$(resources.pipeline.sourcepipeline.runName)"

然后你会在 yaml json 中看到包版本如下:

enter image description here