Search code examples
azureazure-devopsazure-pipelinesazure-pipelines-yaml

Azure DevOps - Get consumed artifacts for a YAML pipeline run via REST API


I have a YAML release pipeline which triggers from a build pipeline. I want to get the details about the consumed artifacts via REST API. E.g., Consumed artifacts of all types such as repositories, builds, etc as shown below:

enter image description here

enter image description here

I tried the Runs - Get REST API: https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/get?view=azure-devops-server-rest-7.0 But it seem to return the consumed artifacts of only repository type.


Solution

  • I attempt with the same REST API "Runs - Get", and it can correctly return the resource pipeline as expected.

    1. The YAML of the pipeline. The YAML pipeline will be automatically triggered once a run of the resource pipeline (MathCalc in below sample) is completed successfully.

      # azure-pipelines.yml
      
      resources:
        pipelines:
        - pipeline: MathCalc
          source: MathCalc
          trigger: true
      
      . . .
      

      enter image description here

    2. Call the REST API "Runs - Get" in PowerShell script.

      # get-pipleine-run.ps1
      
      $organization = "Organization Name"
      $project = "Project Name"
      $pipelineId = 174
      $runId = 3818
      $uri = "https://dev.azure.com/${organization}/${project}/_apis/pipelines/${pipelineId}/runs/${runId}?api-version=7.0"
      
      $pat = "Personal Access Token"
      $base64Token = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
      $headers = @{Authorization = "Basic $base64Token"}
      
      $response = Invoke-RestMethod -Method GET -Uri $uri -Headers $headers
      $response | ConvertTo-Json -Depth 10 | Out-File -FilePath run-$runId.json
      

      enter image description here


    EDIT:

    I also tried the REST API "Runs - Get" on my Azure DevOps Server 2022, and it also can return the resource pipeline as expected.

    1. The YAML pipeline with schedules trigger.

      # azure-pipelines.yml
      
      trigger: none
      
      schedules:
      - cron: '54 6 * * *'
        displayName: 'Daily Trigger'
        branches:
          include:
          - main
      
      resources:
        pipelines:
        - pipeline: MathCalc
          source: MathCalc
          trigger: none
      
      . . .
      

      enter image description here

    2. The PowerShell script to Call the REST API.

      # get-pipleine-run.ps1
      
      $instance = "Server Name"
      $collection = "DefaultCollection"
      $project = "BrightRanProj"
      $pipelineId = 8
      $runId = 31
      $uri = "https://${instance}/${collection}/${project}/_apis/pipelines/${pipelineId}/runs/${runId}?api-version=7.0"
      
      $pat = "Personal Access Token"
      $base64Token = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
      $headers = @{Authorization = "Basic $base64Token"}
      
      $response = Invoke-RestMethod -Method GET -Uri $uri -Headers $headers
      $response | ConvertTo-Json -Depth 10 | Out-File -FilePath run-$runId.json
      

      enter image description here