简体   繁体   English

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

[英]Is it possible to create PowerShell task in Azure DevOps which follow the principle of idempotent

I am using the Azure DevOps and PowerShell tasks and creating a build with multiple pipelines.我正在使用 Azure DevOps 和 PowerShell 任务并创建具有多个管道的构建。 I have created a release that will create the resources in Azure following the task sequence, with a sequential pipeline, for example, task 1 create the resource group, task 2 create the Vnet and subnet, task 3 create the storage, task 4 create the VM and so on.我创建了一个发布,它将按照任务序列在 Azure 中创建资源,使用顺序管道,例如,任务 1 创建资源组,任务 2 创建 Vnet 和子网,任务 3 创建存储,任务 4 创建虚拟机等等。 I have done similar things with Terraform.我用 Terraform 做过类似的事情。 But in AzureDevOps I am facing a problem when I am trying to execute the same build multiple time it's throwing the error that resource already exists and pipeline showing a failed run, I don't want that I want the pipeline to run and do nothing like an idempotent operation.但是在 AzureDevOps 中,当我尝试多次执行相同的构建时,我遇到了一个问题,它抛出了资源已经存在的错误,并且管道显示运行失败,我不希望管道运行而什么也不做幂等操作。 How we can achieve that in Azure DevOps?我们如何在 Azure DevOps 中实现这一目标?

An idempotent operation can be repeated an arbitrary number of times and the result will be the same as if it had been done only once.幂等操作可以重复任意次,结果与只执行一次的结果相同。 In arithmetic, adding zero to a number is idempotent.在算术中,给一个数加零是幂等的。

For Your question I understand that you want to create Task for Each action you want to perform.对于您的问题,我了解您想为要执行的每个操作创建任务。 The task will be sequential任务将是连续的

You can refer Azure Yaml Schema for more detail https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-powershell?view=azure-devops#yaml-snippet您可以参考 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 }}
        }

Try using ARM template to create all your resources and then use ARM template deployment task with Deployment Mode = Incremental , then you will achieve what you want.尝试使用 ARM 模板创建您的所有资源,然后使用ARM template deployment任务与Deployment Mode = Incremental ,然后您将实现您想要的。

ARM template example for VNET and 2 Subnets: VNET 和 2 个子网的 ARM 模板示例:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vnetName": {
      "type": "string",
      "defaultValue": "VNet1",
      "metadata": {
        "description": "VNet name"
      }
    },
    "vnetAddressPrefix": {
      "type": "string",
      "defaultValue": "10.0.0.0/16",
      "metadata": {
        "description": "Address prefix"
      }
    },
    "subnet1Prefix": {
      "type": "string",
      "defaultValue": "10.0.0.0/24",
      "metadata": {
        "description": "Subnet 1 Prefix"
      }
    },
    "subnet1Name": {
      "type": "string",
      "defaultValue": "Subnet1",
      "metadata": {
        "description": "Subnet 1 Name"
      }
    },
    "subnet2Prefix": {
      "type": "string",
      "defaultValue": "10.0.1.0/24",
      "metadata": {
        "description": "Subnet 2 Prefix"
      }
    },
    "subnet2Name": {
      "type": "string",
      "defaultValue": "Subnet2",
      "metadata": {
        "description": "Subnet 2 Name"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "variables": {},
  "resources": [
    {
      "type": "Microsoft.Network/virtualNetworks",
      "apiVersion": "2020-05-01",
      "name": "[parameters('vnetName')]",
      "location": "[parameters('location')]",
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "[parameters('vnetAddressPrefix')]"
          ]
        }
      },
      "resources": [
        {
          "type": "subnets",
          "apiVersion": "2020-05-01",
          "location": "[parameters('location')]",
          "name": "[parameters('subnet1Name')]",
          "dependsOn": [
            "[parameters('vnetName')]"
          ],
          "properties": {
            "addressPrefix": "[parameters('subnet1Prefix')]"
          }
        },
        {
          "type": "subnets",
          "apiVersion": "2020-05-01",
          "location": "[parameters('location')]",
          "name": "[parameters('subnet2Name')]",
          "dependsOn": [
            "[parameters('vnetName')]",
            "[parameters('subnet1Name')]"
          ],
          "properties": {
            "addressPrefix": "[parameters('subnet2Prefix')]"
          }
        }
      ]
    }
  ]
}

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在 Azure DevOps 的 Azure Powershell 任务中设置变量并将它们传递给另一个任务? - Is it possible to set variables in the Azure Powershell task in Azure DevOps and pass them to another task? Azure DevOps-自定义任务-具有Azure身份验证的PowerShell - Azure DevOps - Custom Task - PowerShell with Azure Authentification Azure powershell 任务中的 AzureRM 命令:Azure DevOps - AzureRM commands in Azure powershell Task: Azure DevOps 创建带有描述的 Azure DevOps 任务 - Create Azure DevOps task with description PowerShell任务脚本路径Azure DevOps发布 - Script path of PowerShell task Azure DevOps release 如何在 Azure DevOps Azure Powershell 任务中启用 PowerShell 交互模式? - How to enable PowerShell Interactive mode in Azure DevOps Azure Powershell Task? 在 Azure powershell 任务中添加/获取 Azure Keyvault 的秘密 - Add/get secret to Azure Keyvault with Azure powershell task in Azure devops Azure DevOps - 服务原则不起作用 - Azure DevOps - Service Principle Not Working 如何使用 PowerShell 创建 Azure Devops Wiki 子页面 - How to create Azure Devops Wiki SubPage with PowerShell Azure Devops Powershell 任务:生成变量的编程访问 - Azure Devops Powershell task: Programmatic Access of Build Variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM