How to start previous successful release automatically in Devops/VSTS

1.3k views Asked by At

If the current release has failed and how to run the previous successful release automatically?

enter image description here

Ex: If A(Current release) has failed then automatically trigger B(Previous success release) release. Trigger from another release definition.

2

There are 2 answers

9
Mengdi Liang On

No need to be so complicated to use powershell to call api. We have built-in feature can achieve your requirement. Since release pipeline consists of one or more stages,re-deploy release pipeline can be considered as re-running stages.

So open the release pipeline definition first. Then navigate to Post-deployment conditions of the stage => Enable Auto-redeploy trigger => Select the Event and the action:

enter image description here

See the actual execution result:

enter image description here

This is the advantage of the Azure Devops so that you don't need to worry if some thing goes wrong during the Prod deployments as the tool will automatically reverse the last successful deployment by its own.

Updated:

$connectionToken="{PAT}" 

$url="https://vsrm.dev.azure.com/{org}/{project}/_apis/release/releases?api-version=6.0"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
    
$Body=@"
{
  "definitionId": {specific release definition id},
  "description": "Creating release from powershell",
  "artifacts": [
    {
      "alias": "{artifact name}",
      "instanceReference": {
        "id": "{buildid}",
        "name": null
      }
    }
  ],
  "isDraft": false,
  "reason": "none",
  "manualEnvironments": null
}
"@
Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $Body -ContentType application/json
4
Krzysztof Madej On

There is no easy way to achieve this and you need to use REST API.

  1. First you need to get last succeeded deployments. For that you should use this endpoint.
GET https://vsrm.dev.azure.com/{{organization}}/{{project}}/_apis/release/deployments?definitionId=7&api-version=6.1-preview.2

You need to parse response in powershell to get your release id and other details.

  1. Once you have details from above endpoint you need to create a new release with those details. (So far I haven't found an endpoint to run again already created release) To create release you need to call this endpoint:
POST https://vsrm.dev.azure.com/fabrikam/MyFirstProject/_apis/release/releases?api-version=6.0

Here is an example body

{
  "definitionId": 1,
  "description": "Creating Sample release",
  "artifacts": [
    {
      "alias": "Fabrikam.CI",
      "instanceReference": {
        "id": "2",
        "name": null
      }
    }
  ],
  "isDraft": false,
  "reason": "none",
  "manualEnvironments": null
}

Details of artifacts you will find in response of first endpoint.

Here is an example how you may call REST API from task

$uri = "https://dev.azure.com/thecodemanual/DevOps Manual/_apis/build/builds/$(Build.BuildId)/timeline?api-version=5.1"

Write-Host $uri

# Invoke the REST call
$build = Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization = "Bearer $(System.AccessToken)"} -ContentType "application/json"

$taskResult = $build.records | Where-Object {$_.name -eq "ConditionalStep" } | Select-Object result

Write-Host $taskResult.result