Home > Net >  Delete the Azure Resources if any of the task in azure pipeline fails
Delete the Azure Resources if any of the task in azure pipeline fails

Time:08-19

I have Azure Pipeline which creates VMs, Storage , NICs etc. And i want that these resources should be deleted if any of the task in the pipeline got failed. How this can be achieved, do I need to use script in my YAML or is there any extension available?

CodePudding user response:

A sample for you. You just need to output the vars and then use condition to check:

trigger:
- none

pool:
  vmImage: windows-latest

steps:
- task: AzurePowerShell@5
  displayName: Create Storage1
  name: createstorage1
  inputs:
    azureSubscription: 'xxx'
    ScriptType: 'InlineScript'
    Inline: |
      $resourceGroup = "xxx"
      $location = "westus"
      $accountName = "bowman08191"
      Write-Host "##vso[task.setvariable variable=resourceGroup;isoutput=true]$resourceGroup"
      Write-Host "##vso[task.setvariable variable=location;isoutput=true]$location"
      Write-Host "##vso[task.setvariable variable=accountName;isoutput=true]$accountName"
      New-AzStorageAccount -ResourceGroupName $resourceGroup `
        -Name $accountName `
        -Location $location `
        -SkuName Standard_RAGRS `
        -Kind StorageV2
    azurePowerShellVersion: 'LatestVersion'
    
- task: AzurePowerShell@5
  displayName: Create Storage2
  name: createstorage2
  inputs:
    azureSubscription: 'xxx'
    ScriptType: 'InlineScript'
    Inline: |
      $resourceGroup = "xxx"
      $location = "westus"
      $accountName = "bowman08192"
      Write-Host "##vso[task.setvariable variable=resourceGroup;isoutput=true]$resourceGroup"
      Write-Host "##vso[task.setvariable variable=location;isoutput=true]$location"
      Write-Host "##vso[task.setvariable variable=accountName;isoutput=true]$accountName"
      New-AzStorageAccount -ResourceGroupName $resourceGroup `
        -Name $accountName `
        -Location $location `
        -SkuName Standard_RAGRS `
        -Kind StorageV2
    azurePowerShellVersion: 'LatestVersion'

- task: AzurePowerShell@5
  displayName: This will be failed
  inputs:
    azureSubscription: 'xxx'
    ScriptType: 'InlineScript'
    Inline: |
      xxx
    azurePowerShellVersion: 'LatestVersion'
- task: AzurePowerShell@5
  displayName: Create Storage1
  name: createstorage3
  inputs:
    azureSubscription: 'xxx'
    ScriptType: 'InlineScript'
    Inline: |
      xxx
      $resourceGroup = "xxx"
      $location = "westus"
      $accountName = "bowman08193"
      Write-Host "##vso[task.setvariable variable=resourceGroup;isoutput=true]$resourceGroup"
      Write-Host "##vso[task.setvariable variable=location;isoutput=true]$location"
      Write-Host "##vso[task.setvariable variable=accountName;isoutput=true]$accountName"
      New-AzStorageAccount -ResourceGroupName $resourceGroup `
        -Name $accountName `
        -Location $location `
        -SkuName Standard_RAGRS `
        -Kind StorageV2
    azurePowerShellVersion: 'LatestVersion'
- task: AzurePowerShell@5
  condition: failed()
  continueOnError: true
  inputs:
    azureSubscription: 'xxx'
    ScriptType: 'InlineScript'
    Inline: |
      Remove-AzStorageAccount -Name $(createstorage1.accountName) -ResourceGroupName $(createstorage1.resourceGroup) -Force
      Remove-AzStorageAccount -Name $(createstorage2.accountName) -ResourceGroupName $(createstorage2.resourceGroup) -Force
      Remove-AzStorageAccount -Name $(createstorage3.accountName) -ResourceGroupName $(createstorage3.resourceGroup) -Force
    azurePowerShellVersion: 'LatestVersion'

The above is Storage service, other service are similar.

By the way, you can deploy all of the services to a new resource group, if failed, just delete the whole group.

CodePudding user response:

You haven't mentioned which scripting language you are using to deploy these resources. It depends on both scripting language and logic. In this case YAML pipeline can be more useful like having stages or jobs. Also use 'continue on error' as true

  1. Set a variable to find your job executed successfully or not echo "##vso[task.setvariable variable=ISVALIDBUILD;isOutput=true]True"
  2. Create a new job if previous job fails. here InfraBuild is previous job.
- job: RunOnceifFailed
    dependsOn: InfraBuild
    variables: 
      PrintResults: $[ dependencies.InfraBuild.outputs['DetermineResult.PrintResults'] ]
    condition: eq(dependencies.InfraBuild.outputs['DetermineResult.ISVALIDBUILD'], 'False')
  1. Write the tasks to delete the resources in the new job.
  2. Likewise you can also have a job if 'Infrabuild' Job executed successfully.

Please refer MS documents to get to know more about this, hope it helps you get started.

  • Related