2

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?

akoliya01
  • 309
  • 2
  • 4
  • 7
  • 1
    Do you really need it to be triggered with the REST API? Have you looked through the out-of-the-box triggers? https://learn.microsoft.com/en-us/azure/devops/pipelines/release/triggers?view=azure-devops#env-triggers – Yan Sklyarenko Nov 21 '19 at 10:28
  • yeah, why dont you want to configure a stage trigger? – 4c74356b41 Nov 21 '19 at 10:33
  • Yeah, as per requirements releases should be trigger by REST API. – akoliya01 Nov 21 '19 at 10:34
  • 2
    @4c74356b41 Because QA task will perform on VM. So I need to trigger PROD release from that VM using REST API. – akoliya01 Nov 21 '19 at 10:36
  • https://learn.microsoft.com/en-us/rest/api/azure/devops/release/releases/create?view=azure-devops-rest-5.1 – 4c74356b41 Nov 21 '19 at 10:42

1 Answers1

3

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.

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.

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"
Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • Hi @akoliya01 Did you get a chance to try out below answer? Please let me know how did it go? We can discuss it to work out a better solution. – Levi Lu-MSFT Nov 27 '19 at 12:38
  • Hello @Levi Lu-MSFT - since this response is almost a year old, is this still the case can't I trigger a particular stage using REST API ? I have 3 pre-prod stages and two prod stages in same pipeline. I want to trigger a release either at the first pre-prod stage or the first prod stage. The Create Release API starts deployment only if the stage trigger type is "After Release" – Guru Pasupathy Oct 25 '20 at 16:26