有没有办法在 yaml 模板(Azure Pipelines)中为对象数组设置默认值

Is there a way to set a default value for an array of objects in a yaml template (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

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

但是你可以使用coalesce expression来检查WebProject参数是否设置:

  • Evaluates the parameters in order, and returns the value that does not equal null or empty-string. Min parameters:
  • Max parameters: N
  • Example: coalesce(variables.couldBeNull, variables.couldAlsoBeNull, 'literal so it always works')

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

- ${{ 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是否设置为false或没有使用,这个表达式总是为真。