Azure YAML管道条件插入不起作用

goo*_*eno 5 yaml azure-devops azure-pipelines azure-pipelines-yaml

考虑以下管道片段,这是模板的一部分。

- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      echo "##vso[task.setvariable variable=AppType;]WebJob"
      echo "##[debug] AppType set to WebJob"

# This works, using the task condition
- task: DotNetCoreCLI@2
  condition: eq(variables['AppType'], 'WebJob')
  displayName: 'net publish for WebJob'
  inputs:
    command: 'publish'

# This doesn't work, using the conditional insertion, index syntax
- ${{ if eq(variables['AppType'], 'WebJob') }}:
  - task: DotNetCoreCLI@2
    displayName: 'net publish for WebJob'
    inputs:
      command: 'publish'

# This also doesn't work, using the conditional insertion, property dereference syntax
- ${{ if eq(variables.AppType, 'WebJob') }}:
  - task: DotNetCoreCLI@2
    displayName: 'net publish for WebJob'
    inputs:
      command: 'publish'
Run Code Online (Sandbox Code Playgroud)

为什么任务条件有效但条件插入无效?对于第二个,我没有收到任何错误,任务不存在,就像不满足if条件一样。

Leo*_*SFT 5

Azure YAML管道条件插入不起作用

众所周知,${{}}编译时的语法:

表达式

# Note the syntax ${{}} for compile time and $[] for runtime expressions.
Run Code Online (Sandbox Code Playgroud)

因此,当我们执行管道时,条件插入${{ if eq(variables['AppType'], 'WebJob') }}已经被评估,但是Bash任务尚未运行, 的值AppType将始终为null。这就是条件插入不起作用的原因。

为了解决这个问题,我们可以直接定义变量:

 variables:
   AppType: WebJob
Run Code Online (Sandbox Code Playgroud)

或者我们可以定义运行时参数:

parameters:
  - name: AppType
    displayName: AppType
    default: WebJob
Run Code Online (Sandbox Code Playgroud)


jes*_*ing 2

语法${{}}是在模板编译时评估的,而不是在执行期间评估的,因此当设置作业时,由于变量尚不存在,任务将从工作流中删除,它不会等到实际需要运行任务时来评估病情。

您可以使用$[]条件语法进行运行时评估,或依赖condition:语法。

来自文档:

# Two examples of expressions used to define variables
# The first one, a, is evaluated when the YAML file is compiled into a plan.
# The second one, b, is evaluated at runtime.
# Note the syntax ${{}} for compile time and $[] for runtime expressions.
variables:
  a: ${{ <expression> }}
  b: $[ <expression> ]
Run Code Online (Sandbox Code Playgroud)

看: