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

时间:2021-05-15 19:34:02

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

我正在尝试使用在 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

你知道是否有可能实现这种行为?

1 个答案:

答案 0 :(得分:1)

<块引用>

你知道是否有可能实现这种行为?

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

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

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

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

您可以查看文档 Understand variable syntax 以获取更多详细信息:

<块引用>

在管道中,模板表达式变量 (${{ variables.var }}) 在运行时开始之前在编译时处理。宏语法 变量 ($(var)) 在任务运行之前在运行时得到处理。