如何在 Azure Devops 发布管道中执行下一阶段?

sen*_*sen 7 azure azure-devops

在 Azure devops 发布管道中,我试图在两个不同的阶段之后运行一个阶段,如下所示。我在运行 API 测试阶段时遇到问题。

  1. 开发阶段是自动触发的。
  2. QA 阶段是手动触发的。
  3. API-test 需要运行 Dev/QA 成功。

在此处输入图片说明

预期的:

如果 Dev 或 QA 阶段成功,则需要运行 API 测试。

实际的:

开发阶段成功时不会触发 API 测试阶段。

请让我知道所需的配置。

Lev*_*SFT 1

除了重复 API 测试阶段之外,另一个解决方法是使用Update Release Environment rest api。请参阅以下步骤:

1、设置API-Test阶段仅在Dev阶段后自动触发。

在此输入图像描述

2、进入版本编辑页面的 安全页面。在此输入图像描述

设置管理部署允许帐户yourProjectname Build Service(Your Organization)。此权限将允许您更新发布管道中的发布环境。

在此输入图像描述

3、进入QA阶段-->在代理作业部分-->检查Allow scripts to access the OAuth token。此设置将允许您在发布管道中使用访问令牌。

在此输入图像描述

4、完成上述准备后,您现在可以在QA 阶段结束时添加脚本任务来调用发布 REST API。请参阅 powershell 任务中的以下示例:

#Get releaseresponse
$Releaseurl= "https://vsrm.dev.azure.com/{yourOrg}/$(System.TeamProject)/_apis/Release/releases/$(Release.ReleaseId)?api-version=6.0-preview.8" 

$releaseresponse = Invoke-RestMethod -Method Get -Headers @{Authorization = "Bearer $(system.accesstoken)"} -ContentType application/json -Uri $Releaseurl

#Get the environment ID of API-Test stage from the release response:
$id = $releaseresponse.environments |  Where-Object{$_.name -match "API-Test"} | select id

#Create the JSON body for the deployment:
$deploymentbody = @" 
{"status": "inprogress"} 
"@
#Invoke the REST method to trigger the deployment to API-Test stage:
$DeployUrl = "https://vsrm.dev.azure.com/{yourOrg}/$(System.TeamProject)/_apis/release/releases/$(Release.ReleaseId)/environments/$($id.id)?api-version=6.0-preview.7" 

$DeployRelease = Invoke-RestMethod -Method Patch -ContentType application/json -Uri $DeployUrl -Headers @{Authorization = "Bearer $(system.accesstoken)"} -Body $deploymentbody
Run Code Online (Sandbox Code Playgroud)

上述脚本首先调用get Release Rest api来获取 API-Test 阶段的环境 ID。然后调用更新发布环境rest api触发部署到API-Test。

这样上面的脚本就可以实现手动部署到QA阶段成功后触发API-Test阶段。