tfsazure-devops-server-2019

Azure DevOps Server 2019: condition for task execution


In Azure DevOps Services I use parameters to make task execution optional, e.g:

...
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 doesn't support parameters, any ideas how such condition could be added?


Solution

  • Azure DevOps Server 2019 doesn't support parameters, any ideas how such condition could be added?

    Yes, according to this ticket Azure DevOps Server 2019 doesn't support parameters well. So I suggest you can try conditional jobs/steps via variables instead parameters, for more details about Conditions syntax.

    Since parameters are not supported well for now in Azure Devops Server, it's not recommended to use templates in your scenario. (Variables can't be used for conditional template). You can expand those steps directly in your azure-pipeline.yml file like this:

    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'
    

    You can add condition in Job/Step level to determine whether one Job/Step will run.

    Examples for two different directions:

    1.Define variable(hard-code) in 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'
    

    Then the first cmd task will run by default, and it will skip to run when we change the WhetherToRunCmd:true to WhetherToRunCmd:false.

    2.Use queue time variable, don't need to define the variable in yml file:

    Edit the yaml pipeline and choose Variables:

    enter image description here

    Define the variable WhetherToRunJob and enable settable at queue time:

    enter image description here

    Then use something like this in yml:

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

    Then this job will run by default, and skip to run when we change the value to false using Queue with parameters option:

    enter image description here

    I think the variables+condition can also satisfy your needs that run steps/jobs conditionally. Also you can modify the conditions if you want, like and(succeed(),eq(...)...) or what.