在Azure DevOps中如何使用REST API触发版本?
我在Azure DevOps中具有构建和发布管道.该管道包含三个不同的阶段,即STAGING,QA和PROD.因此,在QA插槽中完成部署后,我想使用REST API触发PROD环境.那么,有可能做同样的事情吗?
I have build and release pipeline in Azure DevOps. That pipeline contains a three different stages namely STAGING, QA and PROD. So, after completion of deployment in QA slot I want to trigger PROD environment using REST API. So, Is that possible to do the same?
It is impossible to do this in a single release pipeline. The release create api can only trigger the release pipeline to run, it cannot trigger a certain stage within the pipeline. As the stages in the release pipeline only support after release,after stage and manually.
要满足您的要求,您必须将产品阶段与此发行版(发行版A)分开,这意味着您将使用单阶段产品环境创建新的发行管道(发行版B).
To achieve your requirements, You will have to separate your prod stage from this release(Release A), which means you will create a new release pipeline(Release B) with the a single stage prod environment.
然后,您可以在QA阶段结束时在发布管道A中添加powershell任务,以调用API触发版本B部署到Prod环境.下面的脚本例如:
Then you can add a powershell task at the end of QA stage in release pipeline A to call the API to trigger Release B to deploy to Prod environment. below script is for example:
$releaseUrl ="https://vsrm.dev.azure.com/<organization>/<project>/_apis/release/releases?api-version=5.1"
$body = '{
"definitionId": 4, # release definition id
"description": "Creating prod release",
"artifacts": [
{
"alias": "_NunitProject", #artifacts alias
"instanceReference": {
"id": "1367", #build id related to the artifacts
"name": null
}
}
],
"isDraft": false,
"reason": "none",
"manualEnvironments": null,
}'
$result4 = Invoke-RestMethod -Uri $releaseUrl -Headers @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } -Method post -Body $body -ContentType "application/json"