首页 > 解决方案 > 有没有办法为 yaml 模板(Azure Pipelines)中的对象数组设置默认值

问题描述

我有一个包含如下参数的 yaml 模板。在数组/列表中,我希望能够为其中一个属性设置默认值(对象有 3 个)。原因是我有一些 if 语句检查这些属性,我希望它们运行,无论该属性是否在使用模板的 yaml 中设置(第三个属性是稍后添加的,我不想有更新使用此模板的每个存储库)。

这可以在参数设置中完成吗?

注意:如果不使用属性 WebProject 使用下面的示例,我希望所有内容都通过模板,就好像它被设置为 false 一样。我知道在检查属性和复制文件/发布工件时存在一些重复,但目前我想让它工作并在之后提高效率。

yaml 模板

parameters:
- name: ArtifactPublish
  type: boolean
  default: false
- name: Solution
  type: string
  default: '**/*.sln'
- name: Artifacts
  type: object
  default: []
jobs:
- job: Build
  displayName: 'Build, Pack, and Publish'
  pool:
    vmImage: 'windows-latest'
  variables:
    solution: ${{ parameters.Solution }}
    buildPlatform: 'Any CPU'
    buildConfiguration: 'Release'

  - task: VSBuild@1
    displayName: 'Build Solution'
    inputs:
      solution: '$(solution)'
      msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(Build.ArtifactStagingDirectory)"'
      platform: '$(buildPlatform)'
      configuration: '$(buildConfiguration)'

  - ${{ if eq(parameters.ArtifactPublish, true) }}:
    - ${{ each artifact in parameters.Artifacts }}:
      - ${{ if eq(artifact.WebProject, false) }}:
        - task: CopyFiles@2
          displayName: 'Copy .artifactignore: ${{ artifact.ArtifactPath }}'
          inputs:
            SourceFolder: '$(Build.SourcesDirectory)'
            Contents: '.artifactignore'
            TargetFolder: '$(Build.SourcesDirectory)/${{ artifact.ArtifactPath }}'

        - task: PublishPipelineArtifact@1
          displayName: 'Publish Artifact: ${{ artifact.ArtifactName }}'
          inputs:
            targetPath: '$(Build.SourcesDirectory)/${{ artifact.ArtifactPath }}'
            artifactName: '${{ artifact.ArtifactName }}'

      - ${{ if eq(artifact.WebProject, true) }}:
        - task: CopyFiles@2
          displayName: '${{ artifact.ArtifactPath }}*'
          inputs:
            SourceFolder: $(Build.ArtifactStagingDirectory)
            Contents: '${{ artifact.ArtifactPath }}*'
            TargetFolder: '$(Build.ArtifactStagingDirectory)/${{ artifact.ArtifactPath }}'
            
        - task: PublishPipelineArtifact@1
          displayName: 'Publish Artifact: ${{ artifact.ArtifactName }} (WEB)'
          inputs:
            targetPath: $(Build.ArtifactStagingDirectory)/${{ artifact.ArtifactPath }}
            artifactName: '${{ artifact.ArtifactName }}'

使用模板的示例 yaml:

trigger:
  - release
  - development
  - master
  - feature/*
  - task/*

resources:
  repositories:
    - repository: templates
      name: Project/RepoName
      type: git
      ref: refs/heads/release
  
jobs:
- template: Templates/example.yml@templates
  parameters:
    ArtifactPublish: true
    Artifacts:
    - ArtifactPath: 'example/path'
      ArtifactName: 'exampleName'
    - ArtifactPath: 'example/path'
      ArtifactName: 'exampleName'
      WebProject: true

标签: azureazure-devopsyamlazure-pipelinesazure-pipelines-yaml

解决方案


恐怕无法在参数设置中为其中一个属性设置默认值。

但是你可以使用coalesce 表达式来检查WebProject参数是否被设置:

  • 按顺序计算参数,并返回不等于 null 或空字符串的值。最小参数:
  • 最大参数:N
  • 示例: coalesce(variables.couldBeNull, variables.couldAlsoBeNull, 'literal so it always works')

coalesce(artifact.WebProject, false)因此,您可以在模板 yaml 中使用表达式 ,如下所示:

- ${{ if eq(parameters.ArtifactPublish, true) }}:
  - ${{ each artifact in parameters.Artifacts }}:
    - ${{ if eq(coalesce(artifact.WebProject, false),false) }}:

有了这个表达式- ${{ if eq(coalesce(artifact.WebProject, false),false) }},无论参数WebProject是否设置为假或不使用,这个表达式将始终为真。


推荐阅读