将字典传递到Azure DevOps YAML模板

3

我想在模板中运行一个循环,以下载具有特定版本的两个工件。

我一直在尝试制定解决方案,但是到目前为止还没有成功,这是我想到的,但我认为它不被支持。

如果可能的话,有人能否指点我正确的方向?

pipeline.yml

variables:
- template: project.variables.yml

jobs:
- job: 'Deploy'
  steps:
  - template: instantclient.template.yml
    parameters:
      artifacts:
        oracle-instantclient:
          package: 'oracle-instantclient'
          packageVersion: ${{ variables.oracle-instantclient }}
        oracle-data-access-components:
          package: 'oracle-data-access-components'
          packageVersion: ${{ variables.oracle-data-access-components }}

instantclient.template.yml

parameters:
- name: artifacts
  type: object
- name: feed
  default: ahitapplicationteam
- name: downloadDirectory
  default: deployment/s

steps:
  - ${{ each artifact in parameters.artifacts}}:
    - template: artifacts.template.yml
      parameters:
        packageVersion: ${{ packageVersion }}
        feed:  ${{ parameters.feed }}
        package: ${{ package }}
        downloadDirectory: ${{ parameters.downloadDirectory }}

artifacts.template.yml

parameters:
- name: packageVersion
- name: feed
- name: package
- name: downloadDirectory

steps:
- task: UniversalPackages@0
  displayName: 'Downloading | Feed: ${{ parameters.feed }} | Package: ${{ parameters.package }}' #| PackageVersion: ${{ parameters.packageVersion }}
  inputs:
    command: 'download'
    downloadDirectory: ${{ parameters.downloadDirectory }}
    vstsFeed:  ${{ parameters.feed }}
    vstsFeedPackage: ${{ parameters.package }}
    vstsPackageVersion: ${{ parameters.packageVersion }}
    verbosity: 'Debug'

你可以将artifacts参数定义为一个数组。当你在 ${{ each artifact in parameters.artifacts }} 中操作时,你可以通过 ${{ artifact.package }} 的方式访问对象。 - Thomas
我的意思是,object 类型也可以是一个数组。 - Thomas
1个回答

2

你走在正确的道路上。你需要将每个object中的项目添加-字符,以将其转换为数组。这个object可以是简单字符串或复杂对象的数组。作为object,你可以在each循环中访问你的对象属性。

使用${{ variables.oracle-data-access-components }}假定在编译时流水线最初处理时,oracle-data-access-components变量可用。

是否要将其拆分成3个模板是一种风格上的决定。我选择了2个模板来简化可读性,但第三个模板将为你提供一些必需参数的验证。

pipeline.yml

variables:
- template: project.variables.yml

jobs:
- job: 'Deploy'
  steps:
  - template: instantclient.template.yml
    parameters:
      artifacts:
        - name: 'oracle-instantclient'
          version: ${{ variables.oracle-instantclient }}
        - name: 'oracle-data-access-components'
          version: ${{ variables.oracle-data-access-components }}

instantclient.template.yml

parameters:
# list of package to download (name, version)
- name: artifacts
  type: object

# azure artifact feed name
- name: feed
  type: string
  default: 'ahitapplicationteam'

# download path for artifacts
- name: downloadDirectory
  type: string
  default: 'deployment/s'

steps:
# loop through the artifacts (name, version)
- ${{ each artifact in parameters.artifacts}}:
 
  # download the artifact
  - task: UniversalPackages@0
    displayName: 'Downloading | Feed: ${{ parameters.feed }} | Package: ${{ artifact.name }}' #| PackageVersion: ${{ artifact.version }}
    inputs:
      command: 'download'
      downloadDirectory: ${{ parameters.downloadDirectory }}
      vstsFeed:  ${{ parameters.feed }}
      vstsFeedPackage: ${{ artifact.name }}
      vstsPackageVersion: ${{ artifact.version }}
      verbosity: 'Debug'

1
谢谢 @bryanbcook 提供快速且有建设性的解决方案,解决了我的问题。我确实非常接近答案了。 - Martin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接