user1279914
user1279914

Reputation: 185

Conditional Expression not working in Azure DevOps 2019

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.

Upvotes: 1

Views: 1663

Answers (2)

user1279914
user1279914

Reputation: 185

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.

Upvotes: 1

user10160236
user10160236

Reputation:

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'

Upvotes: 2

Related Questions