【问题标题】:Is there a way to use custom variables in Azure Pipelines conditions有没有办法在 Azure Pipelines 条件中使用自定义变量
【发布时间】:2021-09-29 14:34:43
【问题描述】:

我正在尝试做这样的事情

variables:
  ${{ if eq(variables['abc'], 'dev') }}:
    someOtherVariable: '123'

这里有一个通过 UI 定义的变量:

它不起作用。 someOtherVariable 在此之后未定义。
有没有办法在条件下使用这个变量?应该是什么语法?

谢谢

【问题讨论】:

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


    【解决方案1】:

    语法是正确的,但似乎不适用于依赖于为自定义变量定义变量的块中的变量的条件。

    这是 Azure Pipelines YAML 处理管道的(许多)怪癖之一。某些条件、变量、模板、语法仅在 YAML 处理的特定阶段可用,这取决于您是在管道、模板还是装饰器中。

    最简单的解决方案是使用脚本步骤来设置变量并选择性地使该步骤成为条件:

        ${{ if eq(variables['condition'], 'true') }}:
          script: echo '##vso[task.setvariable variable=someOtherVariable]123'
    

    或依靠 one of my tasks 代表您执行此操作:

    - task: SetVariable@1
      inputs:
        name: 'someOtherVariable'
        value: '123'
      condition: eq(variables['condition'], 'true')
    

    或:

    ${{ if eq(variables['condition'], 'true') }}:
    - task: SetVariable@1
      inputs:
        name: 'someOtherVariable'
        value: '123' 
    

    【讨论】:

      猜你喜欢
      • 2021-09-12
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      • 1970-01-01
      • 2020-06-02
      相关资源
      最近更新 更多