使用 Azure DevOps 管道将标签应用到 CFN 堆栈 - 列表?

问题描述

根据我前几天的问题...

Passing an object into a template in Azure DevOps pipeline

我现在面临的问题是,我尝试使用管道将标签应用于从 Azure DevOps 管道创建的 AWS CloudFormation 堆栈。

这是管道(其中的一些内容):-

# azure-pipelines.yml
parameters:
- name: stackparams
  type: object
  default:
    - displayname: AWS Test User Account
      awscredentials: TESTAWS-BuildUser
      templatefile: ./Test/TestApp/ConsoleSwitchRolesGroupsPolicies.yml
      templateparametersfile: ./Test/TestApp/demoparams.json
      tags:
        - tag1
        - tag2
        - Managed=Cloudformation
stages:
- stage: ProdDeploy  # name of the stage (A-Z,a-z,0-9,and underscore)
  displayName: Production Deploy  # friendly name to display in the UI
  jobs:
    - deployment: DeveloperRoles   # name of the deployment job (A-Z,and underscore)
      displayName: Manage Developer Roles using Cloudformation  # friendly name to display in the UI
      pool:
        vmImage: 'ubuntu-latest'
      environment: aws-test-appaccount-structure  # target environment name and optionally a resource name to record the deployment history; format: <environment-name>.<resource-name>
      strategy:
        runOnce:    #rolling,canary are the other strategies that are supported
          deploy:
            steps:
              - template : ./Templates/CfnUpdateStack/CfnUpdateStack.yml
                parameters:
                  stackparams: ${{ parameters.stackparams }}
                  runRolesGroupsPolicies: ${{ parameters.runRolesGroupsPolicies }}

这是模板:-

# CfnUpdateStack.yml
parameters:
- name: stackparams
  type: object
- name: runRolesGroupsPolicies
  type: boolean

steps:
- ${{ each stackparam in parameters.stackparams }}:
  - task: CloudFormationCreateOrUpdateStack@1
    displayName: ${{ stackparam.displayname }}
    inputs:
      awsCredentials: ${{ stackparam.awscredentials }}
      regionName: 'eu-west-1'
      stackName: 'ConsoleSwitchRolesGroupsPolicies'
      templateSource: 'file'
      templateFile: ${{ stackparam.templatefile }}
      templateParametersFile: ${{ stackparam.templateparametersfile }}
      tags: |
            Component=RoleUserAccess
            ${{ stackparam.tags }}
    condition: eq('${{ parameters.runRolesGroupsPolicies }}',true)

当我运行它时,我得到第一个标签 Component=RoleUserAccess。我设法让它工作,但使用这种使用变量(不是参数)并在每个标签之间放置一个空行的可怕的丑陋方法。不过确实有效!

Versions
variables:
- name: tags
  value: "tag1

          tag2

          Managed=Cloudformation"

我们为每个资源设置了大约 20 个标签,所以它变得非常混乱。为了让问题更容易阅读,我已将其删减。

有什么建议吗?

谢谢,

解决方法

您似乎正在尝试将数组(标签)添加到数组(stackparams)中,我认为这是不正确的。您可以尝试以下语法:

    # CfnUpdateStack.yml
    
    parameters:
    - name: 'tags'
      type: object
      default: {}
    - name: 'displayname'
      type: string
      default: ''
    - name: 'awscredentials'
      type: string
      default: ''
    - name: 'templatefile'
      type: string
      default: ''
    - name: 'templateparametersfile'
      type: string
      default: ''
   steps:
    
    
    
    # azure-pipelines.yml
    
    
    - template: CfnUpdateStack.yml
      parameters:
        tags: 
        - tag1
        - tag2
        - Managed=Cloudformation  
        displayname: AWS Test User Account
        awscredentials: TESTAWS-BuildUser
        templatefile: ./Test/TestApp/ConsoleSwitchRolesGroupsPolicies.yml
        templateparametersfile: ./Test/TestApp/demoparams.json
    
    - template: CfnUpdateStack.yml
      parameters:
        tags: 
        - tag1
        - tag2
        - Managed=Cloudformation  
        displayname: AWS Test User Account2
        awscredentials: TESTAWS-BuildUser2
        templatefile: ./Test/TestApp/ConsoleSwitchRolesGroupsPolicies2.yml
        templateparametersfile: ./Test/TestApp/demoparams2.json