Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for The Art of Iteration: Starting the Cycle
Arindam Mitra
Arindam Mitra

Posted on

     

The Art of Iteration: Starting the Cycle

Greetings my fellow Technology Advocates and Specialists.

This is theChapter #1 of myMastering Loops in Azure DevOps Series.

In this Session, I will walk you throughThe Art of Iteration: Starting the Cycle.

This is a quick start guide to introduce the topic and how it is relevant to the real world.

IMPORTANT NOTE:-

The YAML Pipeline is tested onWINDOWS 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
MILLION DOLLAR QUESTION:-
Question:Can we use "Loop" in Azure Devops YAML Pipeline ?
Answer:YES
HOW ?
Concise summary follows below:-
1.) We can use"Loop" in Azure Devops YAML Pipeline by using the"each" keyword.
2.) We can use the"each" keyword to loop through a)Parameters with the type as b)Object.
3.) Additionally, you can iterate throughNested elements within an Object.
EXAMPLE:-
PIPELINE CODE SNIPPET:-
AZURE DEVOPS YAML PIPELINE (azure-pipelines-Leverage-Loops-For-Automation-Initial-Test.yml):-
trigger:  none#######################DECLARE PARAMETERS:-######################parameters:- name: RGNAME  displayName: Please Provide the Resource Group Name:-  type: object  default: [AMRG001,AMRG002,AMRG003]#######################DECLARE VARIABLES:-######################variables:  BuildAgent: windows-latest########################## Declare Build Agents:-#########################pool:  vmImage: $(BuildAgent)#################### Declare Stages:-###################stages:  - ${{ each rg in parameters.RGNAME }}:    - stage: CREATE_${{ rg }}      jobs:        - job: CreateResourceGroup          steps:            - script: echo "Creating "${{ rg }}""
Enter fullscreen modeExit fullscreen mode
EXPLANATION:-

I.) Declare"Parameter" of type"Object". In this example, I have declared 3 values (Resource Group Names) as an array.

#######################DECLARE PARAMETERS:-######################parameters:- name: RGNAME  displayName: Please Provide the Resource Group Name:-  type: object  default: [AMRG001,AMRG002,AMRG003]
Enter fullscreen modeExit fullscreen mode

II.) Declare"Each" at beginning of the Pipeline stage. The"rg" variable stores all the values that the"each" keyword iterates over in theparameter of type object.

stages:  - ${{ each rg in parameters.RGNAME }}:
Enter fullscreen modeExit fullscreen mode

III.) As there are 3 values declared in"Parameter" of type"Object", there will be3 STAGES CREATED.

IV.) Name of each stage will be one of iterated value from the array declared in"Parameter" of type"Object".

- stage: CREATE_${{ rg }}
Enter fullscreen modeExit fullscreen mode

V.) Finally, each stage displays the Value in the console.

steps:            - script: echo "Creating "${{ rg }}""
Enter fullscreen modeExit fullscreen mode
OUTPUT OF EXAMPLE:-
1. As in the example, there were 3 values declared in"Parameter" of type"Object", hence3 STAGES got created.
Image description
2. Name of each stage is the iterated value from the array declared in"Parameter" of type"Object".
Image description
3. Each stage displays the Value in the console.
STAGE #1:CREATE_AMRG001
Image description
STAGE #2:CREATE_AMRG002
Image description
STAGE #3:CREATE_AMRG003
Image description

Hope You Enjoyed the Session!!!

Stay Safe | Keep Learning | Spread Knowledge

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

2 x Microsoft MVP - IaC & Devops | 2 x Sessionize Active Speaker | Blogger ✍️ | Public Speaker 🔊 | Long Distance Runner🏃‍♂️ | Hiking 🥾 | Traveler 🧳 | Citizen of the 🌎
  • Location
    Ennetbaden, Switzerland
  • Education
    Bachelor in Computer Science Engineering (CSE)
  • Joined

More fromArindam Mitra

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp