Azure Devops:模板使用序列类型的参数来生成作业和阶段

时间:2020-11-09 23:04:34

标签: azure-devops

似乎template变量不支持序列类型,但是template参数支持它(在Microsoft doc中称为对象类型),以及JobList类型和StageList类型,但它们似乎只是具有定义模式的序列。

当遍历对象类型的参数以生成步骤时,我没有遇到任何问题,但是当我尝试执行此操作以生成作业和阶段时,却遇到了一个错误:“未预期到序列”。

所以结果必须是这样的:

# main.yml:
trigger: none

extends:
  template: template.yml
  parameters:
    PROJECTNAME: foo.bar
    DEPLOYMENTTARGETS:
      - stage:
        jobs:
          - deployment:
            environment:
              name: TEST
              tags: Web # tag must be comma separated string which is also kinda weird
      - stage:
        jobs:
          - deployment:
            environment:
              name: PROD
              tags: Web



# template.yml:
parameters:
  - name: PROJECTNAME
    type: string
    default: ""
  - name: DEPLOYMENTTARGETS
    type: stageList # I was expecting object to be working
    default: []

variables:
  - group: LoginSecrets

stages:
  - ${{ each  deploymentTarget in parameters.DEPLOYMENTTARGETS }}: # iterating through an object here will result in error: 'A sequence was not expected', iterating through  a StageList is OK
      - stage: Deploy_${{replace(parameters.PROJECTNAME,'.','_')}}_${{replace(deploymentTarget.jobs[0].environment.name,'-','')}}_${{replace(replace(deploymentTarget.jobs[0].environment.tags,',','_'),'-','')}}_Stage # assuming each stage contains only one deployment job. We also iterate through jobs here if required.
        dependsOn: BuildAndPublish_${{replace(parameters.PROJECTNAME,'.','_')}}_Stage
        jobs:
          - deployment: Deploy_${{replace(parameters.PROJECTNAME,'.','_')}}_Job
            environment:
              name: ${{deploymentTarget.jobs[0].environment.name}}
              resourceType: VirtualMachine
              tags: ${{deploymentTarget.jobs[0].environment.tags}}
            strategy:
              runOnce:
                deploy:
                  steps:
                    - pwsh: $(Pipeline.Workspace)/${{parameters.PROJECTNAME}}/pipelineRelease.ps1
                      env:
                        LOGINNAME: $(loginName)
                        LOGINPASSWORD: $(loginPassword)
                        PROJECTNAME: ${{parameters.PROJECTNAME}}

但是我期望这样的事情会起作用:

# main.yml:
trigger: none

extends:
  template: template.yml
  parameters:
    PROJECTNAME: foo.bar
    DEPLOYMENTTARGETS:
      - EnvName: TEST
        Tags: Web
      - EnvName: PROD
        Tags: Web

# template.yml:
parameters:
  - name: PROJECTNAME
    type: string
    default: ""
  - name: DEPLOYMENTTARGETS
    type: object # instead of StageList, I passed an object here
    default: []

variables:
  - group: LoginSecrets

stages:
  - ${{ each  deploymentTarget in parameters.DEPLOYMENTTARGETS }}: # error here
      - stage: Deploy_${{replace(parameters.PROJECTNAME,'.','_')}}_${{replace(deploymentTarget.EnvName,'-','')}}_${{replace(replace(deploymentTargetTags,',','_'),'-','')}}_Stage
        dependsOn: BuildAndPublish_${{replace(parameters.PROJECTNAME,'.','_')}}_Stage
        jobs:
          - deployment: Deploy_${{replace(parameters.PROJECTNAME,'.','_')}}_Job
            environment:
              name: ${{deploymentTarget.EnvName}}
              resourceType: VirtualMachine
              tags: ${{deploymentTarget.Tags}}
            strategy:
              runOnce:
                deploy:
                  steps:
                    - pwsh: $(Pipeline.Workspace)/${{parameters.PROJECTNAME}}/pipelineRelease.ps1
                      env:
                        LOGINNAME: $(loginName)
                        LOGINPASSWORD: $(loginPassword)
                        PROJECTNAME: ${{parameters.PROJECTNAME}}

1 个答案:

答案 0 :(得分:0)

Azure Devops:模板使用序列类型的参数来生成作业和阶段

恐怕我们必须使用stageList而不是object来传输二维数组。

那是因为参数在main.yml中定义为二维数组,但是如果我们在object文件中将这些数组定义为template.yml。在这种情况下,我们无法在模板文件中循环对象。

您可以查看文档Loop through parameters了解更多详细信息。

此外,您可以检查similar thread以获得一些详细信息。