【问题标题】:Is there a way to set a default value for an array of objects in a yaml template (Azure Pipelines)有没有办法为 yaml 模板(Azure Pipelines)中的对象数组设置默认值
【发布时间】:2020-12-03 14:38:22
【问题描述】:

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

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

注意:如果不使用属性 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

【问题讨论】:

    标签: azure azure-devops yaml azure-pipelines azure-pipelines-yaml


    【解决方案1】:

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

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

    • 按顺序计算参数,并返回不等于 null 或空字符串的值。 最小参数:
    • 最大参数:N
    • 示例: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,这个表达式永远为true。

    【讨论】:

    • 谢谢,不能像我想要的那样在参数中设置默认值,这让我很沮丧,但这让我到达了我需要的地方。
    猜你喜欢
    • 2020-05-26
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    相关资源
    最近更新 更多