sub
sub

Reputation: 669

How to execute next stage in Azure Devops Release pipeline?

In Azure devops, Release pipeline, I am trying to run a stage after two different stage as below. I am facing issue in running the API-test stage.

  1. Dev stage is auto triggered.
  2. QA stage is manual triggered.
  3. API-test is needs to run either Dev/QA is successful.

enter image description here

Expected:

API-test needs to run if either Dev or QA stage is successful.

Actual:

API-test stage is not triggered when Dev stage is successful.

Kindly let me know the required configuration.

Upvotes: 8

Views: 1259

Answers (1)

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30313

Besides of duplicating the API-Test stage, another workaround is to use Update Release Environment rest api. See below steps:

1, Set API-Test stage only be auto triggered after Dev stage.

enter image description here

2, Go the security page of your release edit page. enter image description here

Set the Manage deployments to allow for account yourProjectname Build Service(Your Organization). This persmission will allow you to update the release environment in the release pipeline.

enter image description here

3, Go to QA stage-->In the Agent job section-->Check Allow scripts to access the OAuth token. This setting will allow you to use the accesstoken in the release pipeline.

enter image description here

4, After above preparation, you can now add a script task at the end of QA stage to call the release rest api. See below example in powershell task:

#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

Above scripts first call get Release rest api to get the environment id of API-Test stage. Then call the update release environment rest api to trigger the deployment to API-Test.

So that above script can achieve the API-Test stage be triggered after manually Deployment to QA stage is successfully.

Upvotes: 1

Related Questions