Get a list of Azure Pipelines and YAML files
I wanted to document the pipelines in a particular Azure DevOps project. Rather than manually write up the name of each pipeline and the corresponding YAML file, I figured there must be a way to query that data.
I've done something similar in the past using the Azure DevOps REST API, but this time I'm using the Azure CLI.
Make sure you have the devops
extension installed (az extension add --name azure-devops
if you don't have it already). The commands provided by this extension use the same REST API under the hood that we used directly last time.
I can get a list of pipelines for the current project with az pipelines list
.
This command returns a list of objects corresponding to the BuildDefinitionReference data structure. While it has the pipeline name, I noticed that doesn't include any information about the YAML file. To get that you need query an individual pipeline using:
az pipelines show --name PipelineName
This produces a BuildDefinition object, which happens to include a process
property. While it isn't documented in the BuildProcess data structure, if you look at the actual data you'll see not only the type
property but a yamlFilename
property, which is just what we want.
"process": {
"type": 2,
"yamlFilename": "release.yaml"
}
Putting it all together, and taking advantage of the JMESPath query to limit which fields we get back, I can produce a comma-separated list of pipeline names and their corresponding YAML files with the following:
(az pipelines list --query "[].name" --query-order NameAsc -o tsv) | % { (az pipelines show --name $_ --query "[name, process.yamlFilename]" | ConvertFrom-Json) -join "," }
So for this project:
You get this:
Alternate,alternate.yml
ReleaseTest,release.yaml
This will be more useful in a project with many more pipelines. If the project has multiple repositories you could also include the repository name as well.
e.g.
(az pipelines list --query "[].name" --query-order NameAsc -o tsv) | % { (az pipelines show --name $_ --query "[name, process.yamlFilename, repository.name]" | ConvertFrom-Json) -join "," }
Such a project would produce something similar to this:
Custom Git,azure-pipelines.yml,Repro
Repro,azure-pipelines.yml,Repro
task-test,azure-pipelines.yml,task-test
You can include extra columns of data as needed.
Categories: Azure DevOps, Azure Pipelines, PowerShell