Keith Jackson
Keith Jackson

Reputation: 3259

How can I specify a boolean value as a variable in an Azure YAML Pipeline?

I'm trying to set up a YAML to support a Debug and Release pipeline in Azure DevOps. I want to be able to disable and enable the test runs for the different builds (due to the way it's triggered running it again on the Debug build is redundant).

I've tried doing it like this...

- task: VSTest@2
  displayName: test (net framework)
  enabled: $(enableTesting)
  inputs:
    vsTestVersion: '16.0'
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **/*Tests.dll
      !**/*TestAdapter.dll
      !**/obj/**
    searchFolder: '$(System.DefaultWorkingDirectory)'
    codeCoverageEnabled: true
    testRunTitle: '.net Framework Back End Tests'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

The variable 'enableTesting' is defined in the Variables window as false.

But this errors with Invalid value '$(enableTesting)' for enabled or similar. It looks like it's treating the variable value as a string and not as a boolean.

I've tried like this too...

- task: VSTest@2
  displayName: test (net framework)
  enabled: eq(variables.enableTesting, true)
  inputs:
    vsTestVersion: '16.0'
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **/*Tests.dll
      !**/*TestAdapter.dll
      !**/obj/**
    searchFolder: '$(System.DefaultWorkingDirectory)'
    codeCoverageEnabled: true
    testRunTitle: '.net Framework Back End Tests'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

and got the same result with an appropriately altered error message.

If I do this...

- task: VSTest@2
  displayName: test (net framework)
  enabled: false
  inputs:
    vsTestVersion: '16.0'
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **/*Tests.dll
      !**/*TestAdapter.dll
      !**/obj/**
    searchFolder: '$(System.DefaultWorkingDirectory)'
    codeCoverageEnabled: true
    testRunTitle: '.net Framework Back End Tests'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

Then it works fine, but then this isn't reusable.

Upvotes: 1

Views: 9439

Answers (1)

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41715

You can't put a variable in the enable: property, the best way is to use a custom condition:

and(succeeded(), eq(variables['enableTesting'], 'true'))

Put it inside the tests task:

- task: VSTest@2
  displayName: test (net framework)
  inputs:
    vsTestVersion: '16.0'
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **/*Tests.dll
      !**/*TestAdapter.dll
      !**/obj/**
    searchFolder: '$(System.DefaultWorkingDirectory)'
    codeCoverageEnabled: true
    testRunTitle: '.net Framework Back End Tests'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
  condition: and(succeeded(), eq(variables['enableTesting'], 'true'))

Upvotes: 1

Related Questions