尝试使用数组执行任务时出现 Azure DevOps 错误

Azure DevOps error when trying to execute a task using an array

我有一个如下所示的 AZD 部署模板。我正在尝试多次循环执行一个任务 (Kubernetes@1),循环一个在参数中定义的数组。

  parameters:
  - name: env
  - name: serviceConnection
  - name: 'serviceNames'
    type: object
    default:
    - audit
    - export
    - admin

jobs:

  - deployment: Deployment
    displayName: Deploy to ${{ parameters.env }}
    environment: ${{ parameters.env }}
    pool: on-prem-pool
    variables:
      - template: azure-deploy-vars.yaml
        parameters:
          env: ${{ parameters.env }}
    timeoutInMinutes: 10
    strategy:
      runOnce:
        deploy:
          steps:
            - script: |
                echo "Prepare to deploy config for ${{ parameters.serviceNames}}. clean workspace"
                ls -la
                cd ..
                ls -la
                rm -rf config
                rm -rf devops
                rm -rf TestResults
                rm -rf helm
                rm -f config.sh
                rm -f *.properties
              displayName: 'Clean Workspace'

            - checkout: config
              path: config

          - ${{ each service in parameters.serviceNames }}:
            - task: Kubernetes@1
              displayName: Deploy Config
              inputs:
                connectionType: Kubernetes Service Connection
                kubernetesServiceEndpoint: '${{ parameters.serviceConnection }}'
                namespace: '$(PROJECT_NAMESPACE)'
                configMapName: '${{ service }}'
                forceUpdateConfigMap: true
                useConfigMapFile: true
                configMapFile: '$(Agent.BuildDirectory)/config/${{ service }}/${{ parameters.env }}/application-${{ parameters.env }}.properties'

但是当我尝试 运行 管道时出现此错误。 如果我的模板有错误,谁能指点我?

错误:

/ci/azure-deploy.tpl.yaml: (Line: 41, Col: 11, Idx: 1048) - (Line: 41, Col: 12, Idx: 1049): While parsing a block mapping, did not find expected key.

您需要缩进该行

- ${{ each service in parameters.serviceNames }}:

使其与上面的 - script:- checkout: 行匹配,然后也增加以下行的缩进。

更正后的模板:

parameters:
  - name: env
  - name: serviceConnection
  - name: 'serviceNames'
    type: object
    default:
    - audit
    - export
    - admin

jobs:

  - deployment: Deployment
    displayName: Deploy to ${{ parameters.env }}
    environment: ${{ parameters.env }}
    pool: on-prem-pool
    variables:
      - template: azure-deploy-vars.yaml
        parameters:
          env: ${{ parameters.env }}
    timeoutInMinutes: 10
    strategy:
      runOnce:
        deploy:
          steps:
            - script: |
                echo "Prepare to deploy config for ${{ parameters.serviceNames}}. clean workspace"
                ls -la
                cd ..
                ls -la
                rm -rf config
                rm -rf devops
                rm -rf TestResults
                rm -rf helm
                rm -f config.sh
                rm -f *.properties
              displayName: 'Clean Workspace'

            - checkout: config
              path: config

            - ${{ each service in parameters.serviceNames }}:
            - task: Kubernetes@1
              displayName: Deploy Config
              inputs:
                connectionType: Kubernetes Service Connection
                kubernetesServiceEndpoint: '${{ parameters.serviceConnection }}'
                namespace: '$(PROJECT_NAMESPACE)'
                configMapName: '${{ service }}'
                forceUpdateConfigMap: true
                useConfigMapFile: true
                configMapFile: '$(Agent.BuildDirectory)/config/${{ service }}/${{ parameters.env }}/application-${{ parameters.env }}.properties'