【问题标题】:Using a variable set in a script in an if statement in an Azure YAML pipeline在 Azure YAML 管道的 if 语句中使用脚本中设置的变量
【发布时间】:2021-08-05 14:04:44
【问题描述】:

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

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

【问题讨论】:

  • 我已经接受了答案。

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


【解决方案1】:

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

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

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

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

这就是为什么它取初始值而不是更新值的原因。

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

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

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 2019-03-16
    • 2020-07-13
    • 2020-05-28
    • 2014-05-01
    • 1970-01-01
    相关资源
    最近更新 更多