如何在Azure YAML管道中将布尔值指定为变量?

时间:2020-10-13 11:45:10

标签: azure-devops yaml azure-pipelines

我正在尝试设置YAML以支持Azure DevOps中的调试和发布管道。我希望能够为不同的版本禁用和启用测试运行(由于触发了在Debug版本上再次运行它的方式是多余的。

我已经尝试过这样做...

- 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)'

变量“ enableTesting”在“变量”窗口中定义为false。

但是出现以下错误:无效的值“ $(enableTesting)”已启用或类似错误。似乎将变量值视为字符串而不是布尔值。

我也尝试过这种方法...

- 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)'

并通过适当更改的错误消息获得了相同的结果。

如果我这样做...

- 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)'

然后它可以正常工作,但是这不可重用。

1 个答案:

答案 0 :(得分:1)

您不能在enable:属性中放置变量,最好的方法是使用a custom condition

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

将其放入测试任务中

- 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'))