如何在 AzureDevops 管道中再次循环一种对象类型参数

问题描述 投票:0回答:1

有没有办法在 Azuredevops 中再次循环一个对象类型参数

我计划使用 Azuredevops 管道自动创建/更新资源标签,并且我决定使用 Azure CLI 命令来实现相同目的(不确定这是否是正确的选择)

所以我创建了一个模板(template.yaml)文件,如下所示。

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 resource ${{ 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

              

我的管道输入如下

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     

            

但是我能够循环遍历 envlist 和 taglist 元素,但无法循环遍历每个资源的标签值以一次性创建它们。

shell yaml azure-pipelines azure-pipelines-yaml azure-pipelines-build-task
1个回答
5
投票
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}}

对于第一阶段,管道将 foreach 标记列表中的标记名并输出:

tagname1_1
tagname2_1
tagname1_2
tagname2_2
tagname1_3
tagname2_3

所以关键是'object.Key'和'object.Value',使用它们来获取yaml对象中的其他内容。

© www.soinside.com 2019 - 2024. All rights reserved.