You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过REST API快速查询Azure DevOps管道使用特定YAML模板情况

查找Azure DevOps中使用特定YAML模板的管道

方法:通过Azure DevOps REST API批量检查

1. 获取项目下所有YAML管道

调用REST API获取项目内所有管道,过滤出YAML类型的管道:

GET https://dev.azure.com/{organization}/{project}/_apis/pipelines?api-version=7.1-preview.1

返回结果中,筛选configuration.type为yaml的项,记录每个管道的id和name。

2. 逐个获取管道的YAML定义并搜索模板

对每个YAML管道,调用API获取其YAML内容:

GET https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/yaml?api-version=7.1-preview.1

在返回的yaml字段内容中,搜索目标模板的引用路径(比如template: ../templates/target-template.yml或带仓库的template: /templates/target-template.yml@MySharedRepo)。

3. 收集并输出结果

将包含目标模板引用的管道名称和ID整理输出即可。

脚本示例(PowerShell)

$orgName = "你的组织名"
$projectName = "你的项目名"
$targetTemplatePath = "../templates/my-target-template.yml" # 替换为你的目标模板路径
$patToken = "你的PAT令牌" # 需要有管道读取权限的PAT

# 构建请求头
$headers = @{
    Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$patToken"))
}

# 获取所有YAML管道
$pipelinesUrl = "https://dev.azure.com/$orgName/$projectName/_apis/pipelines?api-version=7.1-preview.1"
$pipelines = Invoke-RestMethod -Uri $pipelinesUrl -Headers $headers -Method Get
$yamlPipelines = $pipelines.value | Where-Object {$_.configuration.type -eq "yaml"}

# 遍历每个管道检查模板引用
$usedByPipelines = @()
foreach ($pipeline in $yamlPipelines) {
    $yamlUrl = "https://dev.azure.com/$orgName/$projectName/_apis/pipelines/$($pipeline.id)/yaml?api-version=7.1-preview.1"
    $pipelineYaml = Invoke-RestMethod -Uri $yamlUrl -Headers $headers -Method Get
    if ($pipelineYaml.yaml -match [regex]::Escape($targetTemplatePath)) {
        $usedByPipelines += [PSCustomObject]@{
            PipelineId = $pipeline.id
            PipelineName = $pipeline.name
            PipelineFolder = $pipeline.folder
        }
    }
}

# 输出结果
Write-Host "使用目标模板的管道:"
$usedByPipelines | Format-Table -AutoSize

注意事项

  • 权限要求:用于调用API的PAT需要拥有Build (Read)权限。
  • 嵌套模板引用:上述方法仅检测直接引用目标模板的管道,如果需要检测间接引用(即模板A引用目标模板,管道引用模板A),需要递归获取并检查所有被引用的模板内容。
  • 跨仓库模板:如果模板存储在其他仓库,搜索时要包含仓库标识(比如@MyRepo),确保匹配准确。
  • 经典管道:API返回的结果中会包含经典管道,需通过configuration.type过滤,避免无效请求。

内容的提问来源于stack exchange,提问作者D-Angelov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 10:21:39