0
votes

I am working on a Ymal pipeline where I'm consuming build artifact from another pipeline.I'm able to download artifact using below code:

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

In above code I know I can set property buildId to get build id via this link https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/download-build-artifacts?view=azure-devops

Though I want to know what is the package version.I'm using this API to get information from the particular build but artifact version number is not coming up. https://dev.azure.com/{Org name}/{Project name}/_apis/build/builds/{buildid}/timeline/?api-version=5.1

Basically I have to create a report on daily basis based on the specific pipeline run where I will need all the information.

I also would like to inform you I have package version in the pipeline via resource though I am not able to set it in to a variable. If i set package version in any variable its not showing in the build pipeline ymal json. when I run the API using the https://dev.azure.com/{Org name}/{Project name}/_apis/build/builds/{buildid}/timeline/?api-version=5.1 I am not seeing any package version in side below task in the image. If we can set version from ymal pipeline from any property or anything so that I can get that version here in below image.

enter image description here

If anyone can help me or guide how I can set or pass version number using variable or any other property in PowerShell or azure PowerShell task that would be really helpful.

Thanks

1

1 Answers

1
votes

You can use pipeline sources to consuming build artifact from another pipeline. Then you can get the artifact build version by referencing to the predefined variable resources.pipeline.<Alias>.runName. See below example:

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

To consume the artifacts defined in the pipeline source. You just need to add the download task in your yaml pipeline. See here.

- download: sourcepipeline

Artifacts from the associated pipeline resource are downloaded to $(Pipeline.Workspace)/<pipeline resource identifier>/.

If you want use the DownloadBuildArtifacts task and rest api to get the artifact build number. You can call Build get rest api. After you get the buildNumber, you can set it to a variable using logging commands ##vso[task.setvariable variable=buildVersion]<buildVersion>. See below example:

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

Update:

You can use the logging command to log a warning. And set the package version to be the warning message. Then the package version will show up in the timeline rest api. see below:

Add a powershell task to run the logging commands:

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

Then you will see the package version in the yaml json as below:

enter image description here