Greetings my fellow Technology Advocates and Specialists.
This is the Chapter #2 of my Mastering Loops in Azure DevOps Series.
In this Session, I will walk you through The Art of Iteration: Loop in Pipeline Stage.
USECASE:- |
---|
Validate and Provision Multiple Azure Resource Groups using Iterations in an Azure DevOps YAML Pipeline. |
IMPORTANT NOTE:- |
---|
The YAML Pipeline is tested on WINDOWS BUILD AGENT Only!!!
REQUIREMENTS:- |
---|
- Azure Subscription.
- Azure DevOps Organisation and Project.
- Service Principal with Required RBAC (Contributor) applied on Subscription or Resource Group(s).
- Azure Resource Manager Service Connection in Azure DevOps.
CODE REPOSITORY:- |
---|
arindam0310018 / 03-Jan-2025-DevOps__Mastering-Loops-in-Azure-DevOps
MASTERING LOOPS IN AZURE DEVOPS
Mastering Loops in Azure DevOps:-
Greetings to my fellow Technology Advocates and Specialists.
This is "Mastering Loops in Azure DevOps" Series !
DATE | TOPICS | CONTENT |
---|---|---|
03.01.2025 | The Art of Iteration: Starting the Cycle | https://dev.to/arindam0310018/the-art-of-iteration-starting-the-cycle-1op |
04.01.2025 | The Art of Iteration: Loop in Pipeline Stage | https://dev.to/arindam0310018/the-art-of-iteration-loop-in-pipeline-stage-40he |
HOW DOES MY CODE PLACEHOLDER LOOKS LIKE:-:- |
---|
EXAMPLE:- |
---|
PIPELINE CODE SNIPPET:- |
---|
AZURE DEVOPS YAML PIPELINE (azure-pipelines-Leverage-Loops-For-Automation-v1.0.yml):- |
---|
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".
- stage: VALIDATE_AND_CREATE_${{ rg }}
jobs:
- job: VALIDATE_AND_CREATE_${{ rg }}
displayName: VALIDATE AND CREATE ${{ rg }}
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). |
Hope You Enjoyed the Session!!!
Stay Safe | Keep Learning | Spread Knowledge
Top comments (0)