Get Consumed Artifacts in Azure Pipelines

Max Yermakhanov
ObjectSharp (a Centrilogic Company)
2 min readMay 31, 2021

If you ever needed to get a list of consumed artifacts in Azure Pipelines, but couldn’t find a well documented way to do that. Well, that’s because unfortunately, there is a well documented way to do that. There is, however, an undocumented way to that. You can get consumed artifacts using POST REST API Call that uses ContributionIDs.

So, let’s get started. We will use PowerShell script to do what we need to do, but you can use any other language to make REST API calls. First, we will initialize some variables to make our REST API calls a bit more flexible:

$token = "ENTER_TOKEN_HERE"
$ORG_NAME = "ENTER_ORGNAME_HERE"
$PROJECT_NAME = "ENTER_PROJECT_HERE"
$BUILD_ID = "ENTER_BUILDID_HERE"

In this, case PAT token that you’re using will need “Read, write, & execute” permissions to the pipelines. Also, you will need to know the build ID of the pipeline for which you want to retrieve consumed artifacts. You can get a list of runs for a particular pipeline using well documented Runs — List — REST API (Azure DevOps Pipelines) | Microsoft Docs article.

Now, we will need to base64 encode the personal access token we’re using to authenticate to Azure DevOps.

$tokenEncoded = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$token"))

And, let’s set REST API call URL that we will use:

$url = "https://dev.azure.com/$ORG_NAME/_apis/Contribution/HierarchyQuery/project/malauzai?api-version=6.1-preview.0"

Next, we will create a payload body that we will use to get a list of consumed artifacts:

$buildResults = @"{\"contributionIds\": [\"ms.vss-build-web.run-consumed-artifacts-data-provider\"],\"dataProviderContext\": {\"properties\": {\"buildId\": \"$BUILD_ID\",\"sourcePage\": {\"url\": \"https://dev.azure.com/$ORG_NAME/$PROJECT_NAME/_build/results?buildId=$BUILD_ID&view=results\",\"routeId\": \"ms.vss-build-web.ci-results-hub-route\",\"routeValues\": {\"project\": \"$PROJECT_NAME\",\"viewname\": \"build-results\",\"controller\": \"ContributedPage\",\"action\": \"Execute\"}}}}}"@

We’re now ready to make a REST API call to retrieve consumed artifacts:

Invoke-RestMethod -Uri $url -Method Post -Body $buildResults -ContentType "application/json" -Headers @{Authorization = "Basic $tokenEncoded"} | ConvertTo-Json -Depth 10

This will give a JSON response back that will list artifacts consumed by the pipeline. That’s it.

Hope this helps,

#DevOpsGuy

P.S. : You can also get a list of generated artifacts the similar way. All you need to do is to change contributionIds value to “ms.vss-build-web.run-consumed-artifacts-data-provider” in payload body.

--

--