Azure devops Azure YAML管道条件插入不';行不通

Azure devops Azure YAML管道条件插入不';行不通,azure-devops,yaml,azure-pipelines,azure-pipelines-yaml,Azure Devops,Yaml,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: 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'
为什么任务条件有效而条件插入无效?
对于第二个,我没有得到任何错误,任务只是不存在,就像if条件不满足一样。

在模板编译时,而不是在执行期间,对
${{}
语法进行评估,因此任务将在作业设置时从工作流中删除,因为变量还不存在,它不会等到实际需要运行任务时才评估条件

您可以使用
$[]
条件语法进行运行时计算,也可以依赖
条件:
语法

从文档中:

# 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> ]
#两个用于定义变量的表达式示例
#当YAML文件被编译成一个计划时,将对第一个文件a进行评估。
#第二个是b,在运行时进行计算。
#注意编译时的语法${},运行时表达式的语法$[]。
变量:
a:${}
b:$[]
见:

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

正如我们所知,编译时的语法是
${{}

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

要解决此问题,我们可以直接定义变量:

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

parameters:
  - name: AppType
    displayName: AppType
    default: WebJob

我已经在一条管道上测试了你的情况,并像这样改变了它,它工作正常。您应该更改条件语法,一切都将是正确的

- task: Bash@3
  inputs:
   targetType: 'inline'
   script: |
     echo "##vso[task.setvariable variable=AppType;]WebJob"
     echo "##[debug] AppType set to WebJob"
- task: CmdLine@2
  inputs:
    script: echo Key:'$(AppType)' 
# This doesn't work, using the conditional insertion, index syntax
  ${{ if eq(variables['AppType'], 'WebJob') }}:
- task: CmdLine@2
  inputs:
    script: echo Key:'$(AppType)'
# This also doesn't work, using the conditional insertion, property dereference syntax
  ${{ if eq(variables.AppType, 'WebJob') }}:
- task: CmdLine@2
  inputs:
    script: echo Key:'$(AppType)'

我建议您尝试使用visual studio代码和扩展名或任何ide来更正YAML文件。

谢谢您的解释。语法
-$[如果eq(variables.AppType,'WebJob')]:
无效。您使用azure devops服务器吗?我认为运行时条件在该上下文中无效。条件模板必须使用编译时表达式语法。没有得到你的最新信息。只是想看看下面的答案是否对你有帮助?如果是的话,你可以接受一个对其他和你有相同谜题的人也有好处的谜题,我们可以存档这个线程。另外,如果还有任何疑问,请随时在下面留言:-)谢谢,你为我节省了很多时间。您应该在Azure管道的“表达式”、“变量”和“条件”文档顶部用红色书写解释。
- task: Bash@3
  inputs:
   targetType: 'inline'
   script: |
     echo "##vso[task.setvariable variable=AppType;]WebJob"
     echo "##[debug] AppType set to WebJob"
- task: CmdLine@2
  inputs:
    script: echo Key:'$(AppType)' 
# This doesn't work, using the conditional insertion, index syntax
  ${{ if eq(variables['AppType'], 'WebJob') }}:
- task: CmdLine@2
  inputs:
    script: echo Key:'$(AppType)'
# This also doesn't work, using the conditional insertion, property dereference syntax
  ${{ if eq(variables.AppType, 'WebJob') }}:
- task: CmdLine@2
  inputs:
    script: echo Key:'$(AppType)'