DEV Community

Cover image for The Art of Iteration: Loop in Pipeline Stage
Arindam Mitra
Arindam Mitra

Posted on

The Art of Iteration: Loop in Pipeline Stage

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:-
  1. Azure Subscription.
  2. Azure DevOps Organisation and Project.
  3. Service Principal with Required RBAC (Contributor) applied on Subscription or Resource Group(s).
  4. Azure Resource Manager Service Connection in Azure DevOps.
CODE REPOSITORY:-



HOW DOES MY CODE PLACEHOLDER LOOKS LIKE:-:-
Image description
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 "#############################################################"
                }
Enter fullscreen mode Exit fullscreen mode
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]
Enter fullscreen mode Exit fullscreen mode

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 }}:
Enter fullscreen mode Exit fullscreen mode

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 }}
Enter fullscreen mode Exit fullscreen mode

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 "#############################################################"
                }
Enter fullscreen mode Exit fullscreen mode
OUTPUT OF EXAMPLE:-
1. As in the example, there were 3 values declared in "Parameter" of type "Object", hence 3 STAGES got created.
Image description
2. Name of each stage and Job is the iterated value from the array declared in "Parameter" of type "Object".
Image description
3. Below is how it looks when the Pipeline iterates and creates resource group(s).
Image description
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).
Image description

Hope You Enjoyed the Session!!!

Stay Safe | Keep Learning | Spread Knowledge

Top comments (0)