How to get Azure Artifact Feed ID from user readable name

This is a simple post but with some affective content. Sometimes when using Azure DevOps YAML Pipelines you might need to interact with the Azure DevOps Artifacts and their Feeds. However, to do this you need to obtain the Feed ID, which is not obvious where it is and how to get it, especially during the pipeline dynamically.

Unfortunately there is no CLI or dynamic method using the Feed Name to get the Artifact Feed ID, so instead we need to use the REST API. Azure DevOps exposes a GET endpoint for getting a list of feeds in the whole organization as documented here https://learn.microsoft.com/en-us/rest/api/azure/devops/artifacts/feed-management/get-feeds?view=azure-devops-rest-6.0

You could use native PowerShell to invoke a web request to the endpoint documented in the above, but you can also instead use the az devops CLI. Using this cut out a few lines of code and can make the authentication much easier. Although this CLI does not have a direct feed argument like az devops feeds you can use the invoke command by passing in key verbs to execute predefined REST API endpoints.

First you will need to login, which you can decide how best suites your platform by following the documentation, but in simple terms you need to run 

az devops login

The basic command for the az devops invoke — http-method GET this will invoke a REST API request using the GET method. Then there are two key attributes area and resource, these help decide what endpoint we are going to request. You can determine these from the URL, in the document above, reading them from the resource path _apis/{area}/{resource}, for example _apis/wit/workitems. Therefore as the URL is _apis/packaging/feeds it means the area is packaging and the resource is feeds.

We can put the command all together as below with the addition of api-version and organisation. We can also use the JMESPath to filter the results by the Feed Name

$organisation="hmcts"  
$feedName="vh-packages"
$organisationUrl="https://dev.azure.com/$organisation"

$feed = az devops invoke --http-method GET --api-version "6.0-preview" --org $organisationUrl --area "Packaging" --resource "feeds" --query "value[? name=='$feedName']" | ConvertFrom-Json #--debug

if ($null -ne $feed){
$id="$($feed.id)"
Write-Host "Found Feed with ID $id"

}

Bonus

If you are authenticated with your organisation on your browser, you can also just put the URL into your address bar to see the JSON response. E.g.

https://feeds.dev.azure.com/<My Organisation Name>/_apis/packaging/feeds?api-version=6.0-preview

Published by Chris Pateman - PR Coder

A Digital Technical Lead, constantly learning and sharing the knowledge journey.

Leave a message please

This site uses Akismet to reduce spam. Learn how your comment data is processed.