Home > Software engineering >  How to loop inside one object type Parameters again in AzureDevops pipeline
How to loop inside one object type Parameters again in AzureDevops pipeline

Time:12-07

is there any way to loop inside one object type Parameters again in Azuredevops

I am planning to automate tag create/update to resources using Azuredevops pipeline and I decided to use Azure CLI command for the same(not sure if this is the right choice)

So I created a template (template.yaml) file as below.

parameters:
- name: myEnvironments
  type: object
- name: tagList
  type: object

stages:
  - ${{ each environment in parameters.myEnvironments }}:  
    - stage: Create_Tag_${{ environment }}
      displayName: 'Create Tag in ${{ environment }}'
      pool:
          name: my-spoke
      jobs:
        - ${{ each tag in parameters.tagList }}:
          - ${{ if eq(tag.todeploy, 'yes') }}:
            - job: Create_Tag_For_${{ tag.resourcename }_${{ environment }}}
              displayName: 'Tag the reource ${{ tag.resourcename }'
              condition: eq('${{ tag.todeploy }}', 'yes')  
              workspace:
                clean: all
              pool:
                name: myspoke
              steps:
              - task: AzureCLI@2
                displayName: "Tag the resource"
                inputs:
                  azureSubscription: ${{ variables.subscription }}
                  scriptType: 'bash'
                  scriptLocation: 'inlineScript'
                  inlineScript: az tag update --resource-id ${{ tag.resourceid }} --operation replace --tags key1=value1 key3=value3

              

and my pipeline input as below

stages:
  - template: template.yaml
    parameters:
      myEnvironments:
      - development
################################################################################################
#                 Tag List                                                                   #
################################################################################################
      tagList:
      - resourcename: myaksservice
        todeploy: yes
        tagname1: tagvalue of 1
        tagname2: tagvalue of 2
        .
        .
        .
        .   
        tagn    : tagvalue of n
        
      - resourcename: myappservice
        todeploy: yes       
        tagname1: tagvalue of 1
        tagname2: tagvalue of 2
        .
        .
        .
        .   
        tagn    : tagvalue of n     
        
      - resourcename: mystorageaccount
        todeploy: yes     
        tagname1: tagvalue of 1
        tagname2: tagvalue of 2
        .
        .
        .
        .   
        tagn    : tagvalue of n     

            

But I was able to loop through the envlist , and the taglist elelments, but not able to loop through the tag values for each resources to crate them at a shot.

CodePudding user response:

trigger:
- none

pool:
  vmImage: ubuntu-latest
parameters:
- name: myEnvironments
  type: object
  default:
  - 111
  - 222
  - 333
- name: tagList
  type: object
  default:
  - resourcename: myaksservice
    todeploy: yes
    tagname1_1: tagvalue of 1
    tagname2_1: tagvalue of 2
  - resourcename: myappservice
    todeploy: yes
    tagname1_2: tagvalue of 1
    tagname2_2: tagvalue of 2
  - resourcename: mystorageaccount
    todeploy: yes
    tagname1_3: tagvalue of 1
    tagname2_3: tagvalue of 2

stages:
- ${{ each environment in parameters.myEnvironments }}:
  - stage: 
    displayName: 'Create Tag in ${{ environment }}'
    pool:
      vmImage: ubuntu-latest
    jobs:
      - ${{ each tag in parameters.tagList }}:
        - ${{ each tagcontent in tag }}:
          - ${{ if and(ne(tagcontent.Key, 'resourcename'),ne(tagcontent.Key, 'todeploy')) }}:
            - job:
              displayName: 'Tag the reource ${{ tag.resourcename }}'
              steps:
              - task: PowerShell@2
                inputs:
                  targetType: 'inline'
                  script: |
                    # Write your PowerShell commands here.
                    
                    Write-Host "Hello World"
                    Write-Host ${{tagcontent.Key}}

For the first stage, the pipeline will foreach the tagname in taglist and output:

tagname1_1
tagname2_1
tagname1_2
tagname2_2
tagname1_3
tagname2_3

So the key is 'object.Key' and 'object.Value', use them to get other contents in yaml object.

  • Related