您可以参考这张票:无法批准最新版本 Azure devOps
选项 :Deploy latest and cancel others
仅支持 PRE-DEPLOYMENT 审批待定。
有没有办法自动化这个?
您可以在发布管道阶段添加 PowerShell 任务以运行以下 Rest API 以获取发布(待部署后批准)并取消发布。
这是 PowerShell 示例:
$token = "PAT"
$url=" https://vsrm.dev.azure.com/{ORG}/{PROJECT}/_apis/release/deployments?api-version=6.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json
Foreach($status in $response.value.postDeployApprovals)
{
echo $status.status
$compare = "pending"
if($status.status -eq $compare )
{
echo $status.release.id
$releaseid = $status.release.id
echo $status.releaseEnvironment.id
$environmentid = $status.releaseEnvironment.id
$url1="https://vsrm.dev.azure.com/{org}/{project}/_apis/Release/releases/$($releaseid)/environments/$($environmentid)?api-version=6.0-preview.6"
echo $url1
$JSON = @'
{
"status": "canceled",
"scheduledDeploymentTime": null,
"comment": null,
"variables": {}
}
'@
$response = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method PATCH -Body $JSON -ContentType application/json
}
}
创建新版本时,新阶段将运行 Powershell 任务以取消所有阶段(等待 POST-DEPLOYMENT 批准)。
注意:您需要将最大并行部署数设置为至少 2。