0
votes

I understand how to do this for releases with a PRE-DEPLOYMENT approval pending. It's in Pre-deployment conditions > Deployment queue settings > Subsequent releases > Deploy latest and cancel others. And there's lots of existing StackOverflow posts which ask how to do this for PRE-DEPLOYMENT approvals. But there doesnt seem to be any for POST-DEPLOYMENT.

Our current workflow involves deploying a release to our QA environment, then waiting for a post-deployment approval from the QA team before the release moves on to the PROD environment.

But as the QA team finds bugs and they get addressed, the release in QA needs to be replaced. The only way to do that at the moment is to manually cancel the release that is currently pending a post-deployment approval so that the latest release can be deployed.

This gets pretty tedious as I have to keep track of this myself and do it all manually. Is there no way to automate this?

1

1 Answers

1
votes

You could refer to this ticket: Unable to approve latest release Azure devOps

The option : Deploy latest and cancel others only supports the PRE-DEPLOYMENT approval pending.

Is there no way to automate this?

You could add the PowerShell task in the release pipeline stage to run the following Rest APIs to get the Release (Pending on POST-DEPLOYMENT Approval) and cancel the release.

Here is the PowerShell sample:

$token = "PAT"

$url=" https://vsrm.dev.azure.com/{ORG}/{PROJECT}/_apis/release/deployments?api-version=6.0"

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

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json



Foreach($status in $response.value.postDeployApprovals)
{

  echo $status.status
  $compare = "pending"

  if($status.status -eq $compare )
  {
 
    echo $status.release.id
    $releaseid = $status.release.id

    echo $status.releaseEnvironment.id
    $environmentid = $status.releaseEnvironment.id

    $url1="https://vsrm.dev.azure.com/{org}/{project}/_apis/Release/releases/$($releaseid)/environments/$($environmentid)?api-version=6.0-preview.6"
    echo $url1

    $JSON = @'
    {
      
        "status": "canceled",
        "scheduledDeploymentTime": null,
        "comment": null,
        "variables": {}
      
    }  
'@
$response = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method PATCH -Body $JSON -ContentType application/json
}

}

When you create a new Release, the new stage will run the Powershell task to cancel all stages(pending on POST-DEPLOYMENT Approval).

Note: You need to set the Maximum number of parallel deployments to at least 2.

enter image description here