azure - 有没有办法为 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
解决方案
恐怕无法在参数设置中为其中一个属性设置默认值。
但是你可以使用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
是否设置为假或不使用,这个表达式将始终为真。
推荐阅读
- python - Python OpenCV - 是否可以更改整个 numpy 数组的图像尺寸/颜色类型?
- mysql - 如何编写 mysql 查询以从一个表中获取记录列表,其中列与多个其他表连接
- r - 使用循环检索R中向量中每个元素的数据并组装一个新的数据框
- elasticsearch - logstash geo_point 错误麋鹿纬度必须是数字
- android - 如何使 Jetpack 撰写复选框四舍五入
- java - 已经赋值后如何修改对象属性的值?
- java - 当结果是 Map 类型(不是 ConcurrentHashMap)时,执行“并发减少”是否有效?
- c++ - 如何检查所有向量值是否等于确定值
- python - 如何从行中提取列名并将它们提升为标题?
- c++ - 具有多个可执行文件的 C++ Makefile 自动依赖