Reputation: 127
I am using the Azure DevOps and PowerShell tasks and creating a build with multiple pipelines. 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. I have done similar things with 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. How we can achieve that in 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.
Upvotes: 1
Views: 1002
Reputation: 4870
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 template example for VNET and 2 Subnets:
{
"$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
Upvotes: 0
Reputation: 78
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://learn.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 }}
}
Upvotes: 1