【问题标题】:Azure DevOps Server 2019: condition for task executionAzure DevOps Server 2019:任务执行的条件
【发布时间】:2020-10-08 11:53:36
【问题描述】:

在 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

Azure DevOps Server 2019 不支持参数,有什么想法可以添加这样的条件吗?

【问题讨论】:

    标签: azure-devops-server-2019


    【解决方案1】:

    Azure DevOps Server 2019 不支持参数,任何想法如何 可以加这样的条件吗?

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

    由于 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'
    

    您可以在 Job/Step 级别添加条件来确定一个 Job/Step 是否会运行。

    两个不同方向的示例:

    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'
    

    那么第一个cmd任务会默认运行,当我们把WhetherToRunCmd:true改成WhetherToRunCmd:false时会跳过运行。

    2.使用queue time variable,不需要在yml文件中定义变量:

    编辑 yaml 管道并选择变量:

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

    然后在 yml 中使用类似这样的东西:

    - job: test
      condition: ne(variables.WhetherToRunJob,false)
    

    那么这个job会默认运行,当我们使用Queue with parameters option将值改为false时就跳转运行:

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

    【讨论】:

      猜你喜欢
      • 2020-03-02
      • 2020-07-11
      • 2021-04-01
      • 2021-06-28
      • 1970-01-01
      • 2020-08-27
      • 2020-08-06
      • 2021-04-05
      • 1970-01-01
      相关资源
      最近更新 更多