D-Angelov
D-Angelov

Reputation: 63

Azure DevOps: find out which pipelines are using specific yaml template

I am using Azure DevOps pipelines and I am looking for a quick way (e.g. REST API) to find out if certain yaml template is being used by any pipelines or not, and if it does, to get the name of the pipelines.

The only option I am aware of is to go through each pipeline yaml config and search for the template in question, but this is not handy when you have to deal with lots of pipelines and templates.

I couldn't find any solution neither in the documentation nor online, so I was wondering if anybody have any tips for that?

Thanks in advance!

Upvotes: 5

Views: 2305

Answers (3)

DavidCox88
DavidCox88

Reputation: 1018

I've managed to achieve this using the Azure REST API and python. You will need to create a PAT on Azure DevOps to authenticate the API and replace the {PAT} in the code. You would also need to replace {organization} and {project} with the values of your organisation and project

# import the required python packages
import requests
import base64
import pandas as pd

# Use PAT token to authenticate connection to Azure
pat = '{PAT}'
authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii')
organization = "YOUR_ORG"
project = "YOUR_PROJECT"

headers = {
    'Accept': 'application/json',
    'Authorization': 'Basic '+authorization
}

# Connect to azure to obtain a full list of pipelines
response = requests.get(
    url=f"https://dev.azure.com/{organization}/{project}/_apis/pipelines/?api-version=6.0-preview.1", headers=headers)

# Save this list as a JSON variable
json_object = response.json()

# Create a blank df to populate information
df = pd.DataFrame(columns=['pipeline_name','yml_path'])

# iterate over the json object to get full details about the pipeline
for item in json_object['value']:
    url= f"https://dev.azure.com/{organization}/{project}/_apis/pipelines/" + str(item['id']) + "?api-version=6.0-preview.1"
    response = requests.get(url=url, headers=headers, verify=False)
    jsonObject = response.json()
    # if pipeline type is yaml then add the pipeline name and yaml path to the dataframe
    if jsonObject['configuration']['type'] == 'yaml':
        df = pd.concat([pd.DataFrame([[jsonObject['name'],jsonObject['configuration']['path']]], columns=df.columns), df], ignore_index=True)

# create a filepath to save the df as a csv file
filepath = r'C:\path\filename.csv'

# Save the df to csv
df.to_csv(filepath, index=False)

You can then either use the csv file to analyse if the template is being used and in which pipeline. Or you can use python to perforn similar analysis on the dataframe

Upvotes: 4

D-Angelov
D-Angelov

Reputation: 63

Thanks for the tips guys,

I've also figured out that if you just do a simple search for the template name in the Project Search bar, it lists all the pipeline yaml files which are referencing the template.

Project Search bar

Upvotes: 0

Jani Hyytiäinen
Jani Hyytiäinen

Reputation: 5407

You can quickly get a list of pipeline names and their yaml files by running this PowerShell snippet in a terminal:

NOTE: Requires PowerShell, Azure CLI and Azure DevOps extension for Azure CLI

az pipelines list | ConvertFrom-Json | ForEach-Object {
    Write-Host $_.name -ForegroundColor Green
    az pipelines show --id $_.id --query process | ConvertFrom-Json | Format-List
}

Upvotes: 2

Related Questions