1

有没有办法使用管道(仪表板)中定义的变量在 YAML 模板上使用条件插入来执行步骤:

我的意思是我有这个步骤:


- ${{ if eq(variables.['somevar'], '')}}:
    - task: Bash@3
      displayName: Var does not being declared
      inputs:
        targetType: 'inline'
        script: |
         echo "This is a step where the var 'somevar' does not being declared'
    - task: Bash@3
      displayName: Another Step
      inputs:
        targetType: 'inline'
        script: |
         echo "This is another step where the var 'somevar' does not being declared'

这应该在未声明变量时运行

在此处输入图像描述


- ${{ if ne(variables.['somevar'], '')}}:
    - task: Bash@3
      displayName: Var is being declared
      inputs:
        targetType: 'inline'
        script: |
         echo "This is a step where the var 'somevar' is being declared with some value'
    - task: Bash@3
      displayName: Another Step
      inputs:
        targetType: 'inline'
        script: |
         echo "This is another step where the var 'somevar' is being declared with some value'

这应该在声明变量时运行

在此处输入图像描述


我知道它存在运行时参数,但我不想每次运行管道(手动)时都使用它们。我希望一些管道在我声明变量时运行一些步骤,而其他一些管道不'当变量没有被声明时,不要运行一些步骤。

我也知道它存在每一步里面的条件,比如

condition: eq(variables.somevar, 'value')

但我想在某些情况下使用条件插入来运行,如上面示例中的许多步骤。不止一步

4

1 回答 1

4

我希望一些管道在我声明变量时运行一些步骤,而其他一些管道在未声明变量时不运行一些步骤。

为什么不使用运行时参数?通过使用运行时参数,我们可以轻松满足您的要求。

无论如何,要通过使用变量来实现这一点,我们可以尝试为 Yaml 模板指定条件:

创建 Yaml 模板:

# File: somevar-nondeclared.yml
steps:
- task: Bash@3
  displayName: Var does not being declared
  inputs:
     targetType: 'inline'
     script: |
      echo "This is a step where the var 'somevar' does not being declared"
- task: Bash@3
  displayName: Another Step
  inputs:
     targetType: 'inline'
     script: |
      echo "This is another step where the var 'somevar' does not being declared"

# File: somevar-declared.yml
steps:
- task: Bash@3
  displayName: Var is being declared
  inputs:
    targetType: 'inline'
    script: |
     echo "This is a step where the var 'somevar' is being declared with some value"
- task: Bash@3
  displayName: Another Step
  inputs:
    targetType: 'inline'
    script: |
     echo "This is another step where the var 'somevar' is being declared with some value"

azure-pipelines.yml您参考:

# File: azure-pipelines.yml

trigger: none

jobs:
- job: Linux
  pool:
    vmImage: 'ubuntu-latest'
  steps:
  - template: somevar-nondeclared.yml 
  condition: eq(variables['somevar'], '')

- job: Windows
  pool:
    vmImage: 'windows-latest'
  steps:
  - template: somevar-declared.yml
  condition: ne(variables['somevar'], '')

因此,如果您在运行管道时声明了变量,它将运行 Windows 作业。否则它将运行本示例中的 Linux 作业。

在此处输入图像描述

于 2020-07-20T08:02:26.750 回答