在 Azure YAML 管道的 if 语句中使用脚本中的变量集

Using a variable set in a script in an if statement in an Azure YAML pipeline

我正在尝试使用在 Azure 管道的 if 语句中使用日志命令在脚本中设置的变量,如下所示,但是,它采用初始值。

parameters:
- name: aParameter
  type: string
  default: dothis
  values:
  - dothis
  - dothat

variables:
- name: aVariable
  value: 0
  
schedules:
- cron:
...

steps:
- bash: |
    updateVariable=$(<an expression>)
    echo "##vso[task.setvariable variable=aVariable]$updateVariable"
  displayName: Set the variable value

- bash: echo $(aVariable) # It actually prints the updated value

- ${{ if or(eq(parameters.aParameter, 'dothis'), and(eq(variables['Build.Reason'], 'Schedule'), lt(variables['aVariable'], 30))) }}:
  - template: templates/do-this.yaml

- ${{ if or(eq(parameters.aParameter, 'dothat'), and(eq(variables['Build.Reason'], 'Schedule'), ge(variables['aVariable'], 30))) }}:
  - template: templates/do-that.yaml

您知道是否可以实现这种行为吗?

Do you know if it is possible to achieve this behavior?

恐怕目前无法实现这种行为。

这是因为模板表达式变量 (${{ variables.var }}) 在 编译时 得到处理,然后 运行 时间在管道中开始.

但是在使用日志命令的脚本中设置变量 aVariable 需要 运行 管道期间的任务 运行s。

这就是它采用初始值而不是更新值的原因。

您可以查看文档 Understand variable syntax 了解更多详情:

In a pipeline, template expression variables (${{ variables.var }}) get processed at compile time, before runtime starts. Macro syntax variables ($(var)) get processed during runtime before a task runs.