Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 257
sub Avatar asked Jul 24 '20 01:07

sub


People also ask

How do you use stages in Azure DevOps pipeline?

In Azure DevOps Server 2019, pools can only be specified at job level. This version of TFS doesn't support YAML pipelines. To add a stage to your release pipeline, select the release pipeline in Releases page, select the action to Edit it, and then select the Pipeline tab.

Which option will start a stage as soon as the release starts?

Stage triggersSelect trigger: Set the trigger that will start the deployment to this stage automatically. Select "Release" to deploy to the stage every time a new release is created. Use the "Stage" option to deploy after deployments to selected stages are successful.


1 Answers

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.

like image 156
Levi Lu-MSFT Avatar answered Oct 05 '22 20:10

Levi Lu-MSFT