从Spinnaker触发Azure DevOps发布管道阶段

时间:2020-09-29 05:15:37

标签: azure-devops azure-devops-rest-api spinnaker azure-devops-self-hosted-agent spinnaker-halyard

我已经在Azure DevOps中创建了发布管道。 我正在大三角帆内进行部署。一旦部署成功/失败,则部署状态报告应进入Azure DevOps的发布管道阶段。

如何实现? Rest-Api有可能吗?

1 个答案:

答案 0 :(得分:1)

可以通过rest api触发azure devops发布管道阶段。

您可以先使用Releases-Create rest api创建发行版。

 POST https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases?api-version=6.1-preview.8

如果要在此版本中触发特定阶段。然后,您可以使用Releases - Update Release Environment

PATCH https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/releases/{releaseId}/environments/{environmentId}?api-version=6.1-preview.7

请参阅以下Powershell脚本示例:您可以按照here的步骤获取个人访问令牌。

$url = "https://vsrm.dev.azure.com/{org}/{proj}/_apis/release/releases?api-version=6.1-preview.8"

$PAT="Personnal Access Token"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))

$body = '{
  "definitionId": releaseDefinitionId,
 }'

#Create a release
$releaseInfo = Invoke-RestMethod -Uri $url -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} -Method post -Body  $body -ContentType "application/json"

#Trigger a specific stage
$releaseId = $releaseInfo.id
$EnvironmentIds = $releaseInfo.environments | select id

#Trigger the second stage
$EnvUrl = "https://vsrm.dev.azure.com/{org}/{proj}/_apis/Release/releases/$($releaseId)/environments/$($EnvironmentIds[1].id)?api-version=6.1-preview.7"

$envBody='{
"status": "inProgress"
}'

Invoke-RestMethod -Uri $EnvUrl -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} -Method patch -Body $envBody -ContentType "application/json"