azure - 如果 azure 管道中的任何任务失败,请删除 Azure 资源

标签 azure azure-devops azure-pipelines virtual-machine

我有 Azure Pipeline,它创建 VM、存储、NIC 等。我希望如果管道中的任何任务失败,应删除这些资源。如何实现这一点,我是否需要在 YAML 中使用脚本或者是否有任何可用的扩展?

最佳答案

给您的样本。您只需要输出变量,然后使用条件来检查:

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'

以上是存储服务,其他服务类似。

顺便说一句,您可以将所有服务部署到新的资源组中,如果失败,只需删除整个资源组即可。

关于azure - 如果 azure 管道中的任何任务失败,请删除 Azure 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73404551/

相关文章:

azure - 当 ADF 中重新触发主管道时,如何仅处理每个事件中的失败文件

azure-devops - 基于不同存储库分支的 Azure DevOps 管道构建名称

azure - 为什么我的 Azure Devops Pipeline Artifact 在下载后被另一个进程锁定?

python - 如何修复 Azure HTTPTrigger 函数中的 ImportError 'EventGridPublisherClient'?

git - 我如何才能只允许某些人在 Visual Studio Online 中提交?

node.js - VSTS/TFS Build 仅在提交和推送 Git 标记时运行 NPM 任务

azure-devops - 是否可以在不发布工件的情况下在 Azure Devops 中获得代码覆盖率?

azure - 如何使用go从azure队列订阅接收消息

Azure 预编译函数不可用于逻辑应用

Azure 函数自定义身份验证