shell - 如何在 Azure Devops 管道中再次循环一种对象类型参数

标签 shell yaml azure-pipelines azure-pipelines-yaml azure-pipelines-build-task

有没有办法在 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 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 元素,但无法循环遍历每个资源的标签值以一次性创建它们。

最佳答案

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对象中的其他内容。

关于shell - 如何在 Azure Devops 管道中再次循环一种对象类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74684267/

相关文章:

linux - 在 Fortran 代码中执行 execute_command_line() 时出错

python - bash:复制具有相同模式的文件

kubernetes - 在 kubernetes kubeconfig yaml 文件中渲染 env-var

python - 尝试播种 YAML 数据时出现 "value must be an integer"

bash - 将前缀 key 发送到 tmux session

linux - 如何使用shell变量更改特定位置的字符

python - 为什么在有上下文和没有上下文的情况下捕获异常关闭打开的文件?

node.js - 如何使用 Docker 在我的 CI 中运行 jshint

azure - 如何在azure devops创建管道API中指定默认代理池?

Azure Pipeline 使用模板表达式和排队变量