是否可以在Azure DevOps中创建遵循幂等原理的PowerShell任务

问题描述

我最近开始使用Azure DevOps和PowerShell任务,并创建具有多个管道的生成。我创建了一个版本,该版本将按照任务序列在Azure中创建资源,并使用顺序管道,例如,任务1创建资源组,任务2创建Vnet和子网,任务3创建存储,任务4创建存储。 VM等。我用terraform做过类似的事情。但是在AzureDevOps中,当我尝试多次执行相同的构建时会遇到一个问题,它抛出以下错误:资源已经存在并且管道显示运行失败,我不想让管道运行并且不执行任何操作幂等运算。我们如何在Azure DevOps中实现这一目标。

幂等运算可以重复任意次,其结果将与仅进行一次相同。在算术中,将数字加零是幂等的。

任何人都可以在这里进行指导。

解决方法

对于您的问题,我知道您想为要执行的每个操作创建任务。任务将是连续的

您可以参考Azure Yaml架构以获取更多详细信息 https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-powershell?view=azure-devops#yaml-snippet

steps:
  - task: AzurePowerShell@5
    name: Create Resource Group Name
    inputs:
      azureSubscription: $(serviceConnection)
      Inline: |
        $rgO = Get-AzResourceGroup -name ${{ parameters.ResourceGroupname }} -location ${{ parameters.location }}
        if(!$$rgO)
        {
          New-AzResourceGroup -name ${{ parameters.ResourceGroupname }} -location ${{ parameters.location }}
        }
,

尝试使用ARM模板创建所有资源,然后使用ARM template deployment = Deployment Mode的{​​{1}}任务,即可实现所需的目标。

ARM template example for VNET and 2 Subnets:

Incremental

发布管道任务( ARM部署任务https://github.com/microsoft/azure-pipelines-tasks/blob/master/Tasks/AzureResourceManagerTemplateDeploymentV3/README.md

enter image description here