trigger:
none
######################
#DECLARE PARAMETERS:-
######################
parameters:
- name: SUBSCRIPTIONID
displayName: Subscription ID Details Follow Below:-
type: string
default: 210e66cb-55cf-424e-8daa-6cad804ab604
values:
- 210e66cb-55cf-424e-8daa-6cad804ab604
- name: RGNAME
displayName: Please Provide the Resource Group Name:-
type: object
default: [AMRG001,AMRG002,AMRG003]
######################
#DECLARE VARIABLES:-
######################
variables:
ServiceConnection: amcloud-cicd-service-connection
BuildAgent: windows-latest
loc: westeurope
#########################
# Declare Build Agents:-
#########################
pool:
vmImage: $(BuildAgent)
###################
# Declare Stages:-
###################
stages:
- ${{ each rg in parameters.RGNAME }}:
- stage: VALIDATE_AND_CREATE_${{ rg }}
jobs:
- job: VALIDATE_AND_CREATE_${{ rg }}
displayName: VALIDATE AND CREATE ${{ rg }}
steps:
- task: AzureCLI@2
displayName: SET AZURE ACCOUNT
inputs:
azureSubscription: $(ServiceConnection)
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
az --version
az account set --subscription ${{ parameters.SUBSCRIPTIONID }}
az account show
- task: AzureCLI@2
displayName: VALIDATE AND CREATE ${{ rg }}
inputs:
azureSubscription: $(ServiceConnection)
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
echo "Looped Value is: "${{ rg }}""
$i = az group exists -n ${{ rg }}
if ($i -eq "true") {
echo "#####################################################"
echo "Resource Group ${{ rg }} exists!!!"
echo "#####################################################"
}
else {
echo "#############################################################"
echo "Resource Group ${{ rg }} DOES NOT EXISTS!!!"
echo "#############################################################"
echo "Creating Resource Group ${{ rg }}..."
echo "#############################################################"
az group create --name ${{ rg }} --location $(loc)
echo "#############################################################"
echo "Resource Group ${{ rg }} Successfully"
echo "#############################################################"
}
EXPLANATION:-
I.) It is a Single Stage Pipeline which first validates if Resource Group by the same name is available. If "No", then it will go ahead and create. If "Yes", it will then display in the pipeline console that "Resource Group Exists".
II.) Declare "Parameter" of type "Object". In this example, I have declared 3 values (Resource Group Names) as an array.
######################
#DECLARE PARAMETERS:-
######################
parameters:
- name: SUBSCRIPTIONID
displayName: Subscription ID Details Follow Below:-
type: string
default: 210e66cb-55cf-424e-8daa-6cad804ab604
values:
- 210e66cb-55cf-424e-8daa-6cad804ab604
- name: RGNAME
displayName: Please Provide the Resource Group Name:-
type: object
default: [AMRG001,AMRG002,AMRG003]
III.) Declare "Each" at beginning of the Pipeline stage. The "rg" variable stores all the values that the "each" keyword iterates over in the parameter of type object.
stages:
- ${{ each rg in parameters.RGNAME }}:
IV.) As there are 3 values declared in "Parameter" of type "Object", there will be 3 STAGES CREATED.
V.) Name of each stage and job will be one of iterated value from the array declared in "Parameter" of type "Object".
VI.) Validation of the iterated value from the array declared in "Parameter" of type "Object" followed by the decision, whether to create Resource Group or not, happens as part of Inline Script.
- task: AzureCLI@2
displayName: VALIDATE AND CREATE ${{ rg }}
inputs:
azureSubscription: $(ServiceConnection)
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
echo "Looped Value is: "${{ rg }}""
$i = az group exists -n ${{ rg }}
if ($i -eq "true") {
echo "#####################################################"
echo "Resource Group ${{ rg }} exists!!!"
echo "#####################################################"
}
else {
echo "#############################################################"
echo "Resource Group ${{ rg }} DOES NOT EXISTS!!!"
echo "#############################################################"
echo "Creating Resource Group ${{ rg }}..."
echo "#############################################################"
az group create --name ${{ rg }} --location $(loc)
echo "#############################################################"
echo "Resource Group ${{ rg }} Successfully"
echo "#############################################################"
}
OUTPUT OF EXAMPLE:-
1. As in the example, there were 3 values declared in "Parameter" of type "Object", hence 3 STAGES got created.
2. Name of each stage and Job is the iterated value from the array declared in "Parameter" of type "Object".
3. Below is how it looks when the Pipeline iterates and creates resource group(s).
4. Below is how it looks when the pipeline iterates, detects that a resource group with the same name already exists, and therefore skips creating the resource group(s).