如何获取部署管道中的所有工件名称?

问题描述 投票:0回答:1

TL;DR:如何获取部署管道中的所有工件名称?

嗨,

我有多个分支和一个通用构建管道来构建他们的工件

神器名称示例:

TestProject_Feature1Branch_BuildNumber70_Date20240725

我想创建一个通用部署管道。 如何轻松选择要部署的工件

在部署管道中,我可以参数化工件名称,但必须深入研究每个构建工件来复制和粘贴,或者手动输入名称似乎很烦人且容易出错。 理想情况下,我想要一个包含构建管道中所有工件名称的下拉列表

谢谢!

azure azure-devops continuous-integration azure-pipelines
1个回答
0
投票

您可以使用 REST API 在脚本中获取构建管道的所有工件名称。

首先,使用Builds - List获取管道的成功运行id。然后,使用 Artifacts - List获取构建运行的工件名称。

这是示例 PowerShell 脚本:

# Define variables
$organization = ""
$project = ""
$pipelineDefinitionId = "106"
$personalAccessToken = ""

# Base64 encode the PAT
$Authentication = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$personalAccessToken"))
$Headers = @{Authorization = ("Basic {0}" -f $Authentication)}


# Step 1: List the runs of a pipeline definition
$buildsUrl = "https://dev.azure.com/$organization/$project/_apis/build/builds?definitions=$($pipelineDefinitionId)&resultFilter=succeeded&api-version=7.2-preview.5"
$buildsResponse = Invoke-RestMethod -Uri $buildsUrl -Method Get  -Headers $Headers

# Step 2: Get the ID of each run
$buildIds = $buildsResponse.value | ForEach-Object { $_.id }

# Step 3: Get the artifact name of each run
$artifactNames = @()
foreach ($buildId in $buildIds) {
    $artifactsUrl = "https://dev.azure.com/$organization/$project/_apis/build/builds/$buildId/artifacts?api-version=7.2-preview.5"
    $artifactsResponse = Invoke-RestMethod -Uri $artifactsUrl -Method Get  -Headers $Headers
    $artifactNames += $artifactsResponse.value | ForEach-Object { $_.name }
}

# Output artifact names
$artifactNames

# Output each artifact name with the format of parameters values
foreach ($artifact in $artifactNames) {
    Write-Output "- $artifact"
}

运行脚本后,您可以复制脚本的输出并将其用作部署管道 YAML 中的参数值。

© www.soinside.com 2019 - 2024. All rights reserved.