如果变量不等于 azure pipeline yaml 中的预期值,如何跳过模板?

How to skip the template if the variable is not equal expected value in azure pipeline yaml?

我有一个 .yaml 文件

variables:
- name: command1
  value: none 

- scripts: |
  echo '##vso[task.setvariable variable=command1]new_value'

- ${{ if ne(variables['command1'], 'none') }}: 
  - template: templates/run.yml@temp1  # Template reference  
    parameters:
      COMMAND: '$(command1)'

我创建变量有两个原因,

  1. 成为全球
  2. 我不希望它显示在用户的变量列表中

我希望仅当 'command1' 的变量值不是 'none'

时才执行模板

目前不是跳过,即使变量里面的值不是none.

也会一直执行

我使用的另一种 if 条件格式是

- ${{ if ne(variables['taskName.command1'], 'none') }}: 
- ${{ if ne('$(command1)', 'none') }}: 
以上的

None有效

请帮助解决这个问题。

如上所写here:

The difference between runtime and compile time expression syntaxes is primarily what context is available. In a compile-time expression (${{ }}), you have access to parameters and statically defined variables. In a runtime expression ($[ ]), you have access to more variables but no parameters.

variables:
  staticVar: 'my value' # static variable
  compileVar: ${{ variables.staticVar }} # compile time expression
  isMain: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')] # runtime expression

steps:
  - script: |
      echo ${{variables.staticVar}} # outputs my value
      echo $(compileVar) # outputs my value
      echo $(isMain) # outputs True

因此它适用于您的 YAML 值:

variables:
- name: command1
  value: none     

steps:
- scripts: |
  echo '##vso[task.setvariable variable=command1]new_value'

- ${{ if ne(variables.command1, 'none') }}: 
  - template: templates/run.yml@temp1  # Template reference  
    parameters:
      COMMAND: '$(command1)'

但是,它会选择这个值:

variables:
- name: command1
  value: none     

没有机会拿这个:

- scripts: |
  echo '##vso[task.setvariable variable=command1]new_value'

因为${{}}表达式是编译时的,echo '##vso[task.setvariable variable=command1]new_value'是运行时的