[英]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.