1

I am trying to conditionally execute a step in Azure DevOps Server 2019. I followed the instructions here.

I am passing a runTests parameter to my template. At first I tried a condition on the Job, but that doesn't work, because it will always use the default in the template, so I switched to a conditional expression. My job YAML now looks like

- job: TestJob
  pool: 'mypool'    
  #condition: eq('${{ parameters.runTests }}', 'true')
  workspace:
    clean: all
  steps:
  - script: echo ${{ parameters.runTests }}
  - script: echo ${{ eq(parameters.runTests, 'true') }}
  - ${{ if eq(parameters.runTests, 'true') }}:
    - task: UiPathTest@2
      inputs:
        testTarget: 'TestProject'
        orchestratorConnection: 'Orch'
        testProjectPath: '$(Build.SourcesDirectory)\$(projectPath)'
        folderName: $(folderName)

I added the lines to echo the parameter, and the result of the expression. The parameter value is 'true', which is correct, but the result of eq(parameters.runTests, 'true') is always false. My YAML is almost identical to the example though. Any ideas what might be wrong? I do not have any errors. The step just does not exist.

user1279914
  • 185
  • 1
  • 5
  • 19
  • In your condition, remove the `'${{ ... }}'` around the parameter name. The condition syntax will understand you're trying to reference a variable there. Optionally add `$[ ...]` around the complete condition. – jessehouwing Oct 22 '21 at 11:57
  • That didn't work unfortunately. "Could not queue the build because there were validation errors or warnings. /project-pipeline-template.yaml@templates (Line: 29, Col: 5): Unexpected value 'if eq(parameters.runTests, 'true')'" – user1279914 Nov 01 '21 at 22:34
  • This is Azure DevOps Server 2019 – user1279914 Nov 01 '21 at 22:35
  • why is your condition line commented out with the # ? – GavinB Nov 09 '21 at 00:47
  • Because that doesn't work with arguments in DevOps Server 2019. I was testing it out and left that in. Server 2019 will always use the default value in that condition, instead of what is passed in. – user1279914 Nov 09 '21 at 21:28

2 Answers2

1

I've created a similar pipeline, however, added parameter definition and specified type for parameter value.

parameters:
- name: runTests
  type: boolean

jobs:
  - job: TestJob
    #condition: eq('${{ parameters.runTests }}', 'true')
    workspace:
      clean: all
    steps:
    - script: echo ${{ parameters.runTests }}
    - script: echo ${{ eq(parameters.runTests, True) }}
    - ${{ if eq(parameters.runTests, True) }}:
      - task: Bash@3
        inputs:
          targetType: 'inline'
          script: |
            echo 'Hello world'

When triggering pipeline I check runTests box runTests-box-checked

The following steps execute as expected and both echo steps return True passed-steps-example Plus conditional bash script is executed as well.

I suggest setting this parameter as boolean and comparing it to boolean True instead of string 'true'

  • Is that on Azure DevOps Server 2019? That is where I am having the issue. I don't think I can define the parameters like that in 2019. I am also passing the argument by using the file as a template. – user1279914 Nov 01 '21 at 22:57
0

Turns out, what made the differences was

variables:
  runTests: 'true'

---

jobs:
- template: template.yaml@templates  # Template reference
  parameters:
    runTests: $(runTests)

vs skipping the variable

---

jobs:
- template: template.yaml@templates  # Template reference
  parameters:
    runTests: 'true'

I don't know why.

user1279914
  • 185
  • 1
  • 5
  • 19