如何将字符串参数列表传递给Azure DevOps模板

时间:2020-06-15 07:33:18

标签: azure azure-devops

previous SO question中,我试图创建一个Azure DevOps模板。

现在我知道该怎么做了,我正在尝试将字符串列表传递给模板,并在模板中使用它们,就像这样:

azure_pipelines.yml

extends:
  template: AzurePipelines/job.yml
  parameters:
    projects:
      - FirstProject.csproj
      - SecondProject.csproj

以及在此模板中...

job.yml

steps:
 (for each projectFileName in projects)
 - bash echo $(projectFileName)
 - run code in the build.yml template using $(projectFileName)
 - run code in the test.yml template using $(projectFileName)
 - run code in the publish.yml template using $(projectFileName)

因此,对于提交给模板的每个项目名称,请运行模板步骤。

我不确定如何传递字符串数组以及如何测试其工作原理?

2 个答案:

答案 0 :(得分:5)

您似乎正在寻找each function

# my-template.yml
parameters:
- name: 'projects'
  type: projectList
  default: []
steps:
- ${{ each project in parameters.projects }}:
  - task: PublishBuildArtifacts@1
    displayName: 'Publish ${{ project }}
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)/${{ project }}.zip'
# ci.yml
steps:
- template: my-template.yml
  parameters:
    projects:
    - test1
    - test2

答案 1 :(得分:2)

我发现完成模板使用的最简单方法是使用主要yaml文件管道中的模板。这样,您将可以访问主模板中定义的所有变量和变量组。使用这种架构,您将能够将几个步骤捆绑到一个模板中,然后从主要yaml文件运行它。例如:

azure_pipelines.yml

variables:
- name: projectFileName
  value: "Variable 1 value"
- name: VAR2
  value: "Variable 2 value"

trigger:
  - master

pool:
  vmImage: 'ubuntu-latest'

steps:
- template: templates/job.yml
- template: templates/test.yml

templates / job.yml

steps:
- task: Bash@3
  displayName: 'Run Bash Command'
  inputs:
    targetType: 'inline'
    script: |
      print "Variable value: " $(projectFileName)

希望有帮助