2

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.

Suresh
  • 59
  • 7
  • Now, we have 2 release definition, one is **DefiniA** and another is **DefiniB**. Now, you'd like to trigger B(Previous success release) from **DefiniB** once A is failed, right? – Mengdi Liang Sep 28 '20 at 09:40
  • Yes, correct @Merlin Liang - MSFT – Suresh Sep 28 '20 at 10:25
  • This would be very complex(BUT can be achieved). One furthur question, why you are looking for such complecated approach to trigger B? Do you have any concern? – Mengdi Liang Sep 28 '20 at 15:18
  • 1
    This approach for the first deploy web app pipeline, second Web API and the last one is DB script. so last DB Script has failed means rollback the last successful release in Web App and Web API deployment. – Suresh Sep 28 '20 at 15:36
  • Copy that. So web app, web api and DB script deployments are separate pipeline definitions? Or they are deployed with one release definition(different stage)? I would expect to find an easier approach for your scenario. – Mengdi Liang Sep 28 '20 at 16:10
  • web app, web api and DB script deployments are used separate pipeline definitions. not used the different stage. – Suresh Sep 29 '20 at 04:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222213/discussion-between-suresh-and-merlin-liang-msft). – Suresh Sep 29 '20 at 04:55

2 Answers2

5

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
Mengdi Liang
  • 17,577
  • 2
  • 28
  • 35
  • @Suresh, Re-run the release pipeline, isn't it rerun the stage job in the pipeline? – Mengdi Liang Sep 25 '20 at 07:50
  • @Suresh. Welcome. Really hope this could resolve your puzzle. Feel free to leave comment if you still has any puzzle on this approach. :-) – Mengdi Liang Sep 25 '20 at 07:52
  • How to trigger one release to another release definition for the last success deploy? – Suresh Sep 25 '20 at 07:54
  • @Suresh Is this triggered release the specific one? Have fixed definition id? – Mengdi Liang Sep 25 '20 at 09:48
  • Yes trigger specific one with a definition id, but not trigger last success release – Suresh Sep 28 '20 at 02:33
  • @Suresh, We do not have built-in feature can do this. You have to achieve that via powershell script. See my updated. – Mengdi Liang Sep 28 '20 at 03:46
  • Created sample release and execute the above script, but still not trigger the last successful release. – Suresh Sep 28 '20 at 04:22
  • @Suresh, In your previous comment, you mentioned that not trigger last success release. So I need confirm that whether you need trigger the last successful release of specific pipeline, or just re-run the specific pipeline? – Mengdi Liang Sep 28 '20 at 04:51
  • Not a specific release, need to trigger check all release with based on a specific definition id – Suresh Sep 28 '20 at 05:12
1

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
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • I have used the above powershell script but $build response is showing empty like @{count=0; value=System.Object[]} – Suresh Sep 24 '20 at 12:40
  • Did you change my definition id `definitionId=7`? – Krzysztof Madej Sep 24 '20 at 13:03
  • Yes changed definition id, but not trigger last success release – Suresh Sep 28 '20 at 02:34
  • To run an already created release you can make a PATCH request to "https://vsrm.dev.azure.com/nsudeep/ADO-Cloud-tesing/_apis/release/releases/{releaseID}/environments/{environemntID}?api-version=6.0" with a body stating "status": "inProgress" – Gautham Sreenivasan Jul 22 '22 at 11:33