Azure DevOps Server 2019:任务执行条件

kag*_*kij 4 azure-devops-server-2019

在 Azure DevOps Services 中,我使用参数使任务执行成为可选,例如:

...
parameters:
- name: createObj
  displayName: 'Create Object?'
  type: boolean
  default: true

...
jobs:
- job: build
  pool:
    name: Default
  steps:
  - ${{ if eq(parameters.createObj, true) }}:
    - template: ./templates/create-object.yml

Run Code Online (Sandbox Code Playgroud)

Azure DevOps Server 2019 不支持参数,您知道如何添加此类条件吗?

Lan*_*SFT 7

Azure DevOps Server 2019 不支持参数,您知道如何添加此类条件吗?

是的,根据此票证, Azure DevOps Server 2019 不能很好地支持参数。因此,我建议您可以通过变量而不是参数尝试条件作业/步骤,以获取有关条件语法的更多详细信息。

由于目前 Azure Devops Server 中还不能很好地支持参数,因此不建议在您的场景中使用模板。(变量不能用于条件模板)。您可以直接在文件中扩展这些步骤,azure-pipeline.yml如下所示:

jobs:
- job: build
  pool:
    name: Default
  steps:
    - task: CmdLine@2
      inputs:
        script: 'echo This is first build task'
      condition: {Add your custom condition here in Step level.}
    - task: CmdLine@2
      inputs:
        script: 'echo This is second build task'

- job: test
  condition: {Add your custom condition here in Job level.}
  pool:
    name: Default
  steps:
    - task: CmdLine@2
      inputs:
        script: 'echo This is first test task'
    - task: CmdLine@2
      inputs:
        script: 'echo This is second test task'
Run Code Online (Sandbox Code Playgroud)

您可以在作业/步骤级别添加条件来确定是否运行一个作业/步骤。

两个不同方向的示例:

1.在yaml中定义变量(硬编码):

variables:
  WhetherToRunCmd:true

jobs:
- job: build
  pool:
    name: Default
  steps:
    - task: CmdLine@2
      inputs:
        script: 'echo This is first build task'
      condition: ne(variables.WhetherToRunCmd,false)
    - task: CmdLine@2
      inputs:
        script: 'echo This is second build task'
Run Code Online (Sandbox Code Playgroud)

那么第一个cmd任务就会默认运行,当我们把 改为 时,它就会跳到WhetherToRunCmd:true运行WhetherToRunCmd:false

2.使用队列时间变量,不需要在yml文件中定义变量:

编辑 yaml 管道并选择变量:

在此输入图像描述

定义变量WhetherToRunJob并在队列时启用可设置:

在此输入图像描述

然后在 yml 中使用类似的内容:

- job: test
  condition: ne(variables.WhetherToRunJob,false)
Run Code Online (Sandbox Code Playgroud)

然后这个作业将默认运行,并在我们使用以下命令将值更改为 false 时跳到运行Queue with parameters option

在此输入图像描述

我认为它variables+condition也可以满足您有条件地运行步骤/作业的需求。如果需要,您也可以修改条件,例如 and(succeed(),eq(...)...) 或其他内容。