shell 如何在AzureDevops管道中再次在一个对象类型Parameters内循环

jfewjypa  于 2022-12-13  发布在  Shell
关注(0)|答案(1)|浏览(88)

在Azuredevops中,是否有办法在一个对象类型Parameters内再次循环
我计划使用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 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

我的管道输入如下

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元素,但是不能循环遍历每个资源的标记值,以便一次性创建它们。

cwtwac6a

cwtwac6a1#

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

所以key是'object.key'和'object.value',用它们来获取yaml对象中的其他内容。

相关问题