如何有条件地设置 Azure DevOps 参数的值?

Dav*_*ard 5 yaml expression azure-devops

我有一个用于轮换某些密钥的 Azure DevOps 管道。有两个主要要求 -

  • 一次只能旋转一个键。
  • 任何键都可以通过手动运行临时轮换。

为此,我计划使用在不同日期运行的 cron 计划。然后应该使用一个参数,默认设置为基于星期几的特定“密钥种类”。使用参数意味着用户还可以在手动运行管道时指定要轮换的键。

不幸的是,我想出的方法不起作用。参数中的四个表达式中的每一个都产生以下错误 -

A template expression is not allowed in this context
Run Code Online (Sandbox Code Playgroud)

根据文档...

编译时表达式可以在任何地方使用

……但这似乎不正确。我希望我遗漏了一些东西,而不是文档不正确,但无论哪种方式,我都不确定如何实现我的目标。

pr: none
trigger: none
schedules:
- cron: 0 2 * * 0-3,6
  displayName: Rotate a key
  branches:
    include:
    - develop
  always: true

parameters:
- name: keyKinds
  displayName: Key Kinds
  type: string
  ${{ if not(in(format('{0:dddd}', pipeline.startTime), 'Sunday', 'Monday', 'Tuesday')) }}:
    default: primary
  ${{ if eq(format('{0:dddd}', pipeline.startTime), 'Sunday') }}:
    default: secondary
  ${{ if eq(format('{0:dddd}', pipeline.startTime), 'Monday') }}:
    default: primaryReadonly
  ${{ if eq(format('{0:dddd}', pipeline.startTime), 'Tuesday') }}:
    default: secondaryReadonly
  values:
  - primary
  - secondary
  - primaryReadonly
  - secondaryReadonly
Run Code Online (Sandbox Code Playgroud)

Krz*_*tof 1

我认为可以回答这个问题:

使用模板表达式指定在管道初始化期间如何动态解析值。将模板表达式包含在以下语法中:${{ }}。

模板表达式可以扩展模板参数,也可以扩展变量。您可以使用参数来影响模板的扩展方式。参数对象的工作方式类似于表达式中的变量对象。模板表达式中只能使用预定义的变量。

表达式仅针对阶段、作业、步骤和容器(资源内部)进行扩展。例如,您不能在触发器内使用表达式或存储库等资源。此外,在 Azure DevOps 2020 RTW 上,不能在容器内使用模板表达式。