簡體   English   中英

Azure DevOps:找出哪些管道正在使用特定的 yaml 模板

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

我正在使用 Azure DevOps 管道並且我正在尋找一種快速的方法(例如 REST API)來確定是否某些 yaml 模板是否被管道的名稱或模板使用,

我知道的唯一選擇是 go 通過每個管道 yaml 配置並搜索有問題的模板,但是當您必須處理大量管道和模板時,這並不方便。

我在文檔和在線都找不到任何解決方案,所以我想知道是否有人對此有任何提示?

提前致謝!

我已經設法使用Azure REST API和 Z23EEEB4347BDD7556BFC6B7EE9A3 實現了這一目標。 您需要在 Azure DevOps 上創建 PAT以驗證 API 並替換代碼中的 {PAT}。 您還需要將 {organization} 和 {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')

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

# Connect to azure to obtain a full list of pipelines
response = requests.get(
    url="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= "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(filename, index=False)

然后,您可以使用 csv 文件來分析是否正在使用模板以及在哪個管道中使用。 或者您可以使用 python 對 dataframe 進行類似的分析

您可以通過在終端中運行此 PowerShell 片段來快速獲取管道名稱及其 yaml 文件的列表:

注意:需要 PowerShell、Azure CLI 和 Azure DevOps 擴展,用於 Z3A580F3422038677F1

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

謝謝提醒伙計,

我還發現,如果您只是在項目搜索欄中簡單搜索模板名稱,它會列出所有引用模板的管道 yaml 文件。

項目搜索欄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM