如何在Azure DevOps Pipeline中编写if else条件

Dat*_*ing 8 azure-devops azure-pipelines azure-pipelines-yaml

我有一个天蓝色的管道并使用参数运行,其中有多个选项,如下所示:

选择测试执行

  • 产品
  • 产品与成本
  • 带附件的产品

如果我选择Product,则执行product.js文件,如果选择,Product with Cost则执行"productCost.js",依此类推。我编写了一个天蓝色管道脚本来执行此操作。我有另一个条件"Generate Test Data"复选框,它返回布尔值true,或者false如果该值是true,那么我必须选择一个文件productWithTestData.js(如果Product选择)-我不知道如何在Azure管道代码中编写if else条件。请找到我的伪代码

有人可以帮助我如何为我的用例编写 if else 条件 - 提前感谢您的帮助!谢谢!

variables:
  - name: fileName
    ${{ if eq(parameters.selectTestRun, 'Product') }}:
      value: 'product.js'
    else ${{ if eq(parameters.selectTestRun, 'Product') } && { if eq(parameters.testData, True) }}:   
      value: 'productWithTestData.js'
Run Code Online (Sandbox Code Playgroud)

我的管道代码:

trigger:
  none

parameters:

- name: selectTestRun
  displayName: Select test execution
  type: string
  default: Product
  values:
  - Product
  - Product with Cost
  - Product with Attachments

- name: testData
  displayName: Generate Test Data
  type: boolean
  default: false

variables:
  - name: fileName
    ${{ if eq(parameters.selectTestRun, 'Product') }}:
      value: 'product.js'
    ${{ if eq(parameters.selectTestRun, 'Product with Cost') }}:
      value: 'productCost.js'
    ${{ if eq(parameters.selectTestRun, 'Product with Attachments') }}:
      value: 'productAttachment.js'      
  
jobs:
  - job: testJob
    pool:
      vmImage: ubuntu-latest
    displayName: Run sample tests
    steps:
      - task: Bash@3
        displayName: Test variable
        inputs:
          targetType: inline
          script: |
            echo "Hello world"
            echo ${{variables.fileName}}
                
Run Code Online (Sandbox Code Playgroud)

Krz*_*tof 20

更新:2021 年 9 月 9 日

现在我们还有 if else 条件可用:

variables:
  ${{ if eq(parameters.os, 'win') }}:
    testsFolder: windows
  ${{ elseif eq(parameters.os, 'linux' }}:
    testsFolder: linux
  ${{ else }}:
    testsFolder: mac
Run Code Online (Sandbox Code Playgroud)

所以我们可以写

variables:
  ${{ if eq(parameters.os, 'win') }}:
    testsFolder: windows
  ${{ elseif eq(parameters.os, 'linux' }}:
    testsFolder: linux
  ${{ else }}:
    testsFolder: mac
Run Code Online (Sandbox Code Playgroud)

原回复

在这种情况下你应该使用notIn表达式:

variables:
  - name: fileName
    ${{ if eq(parameters.selectTestRun, 'Product') }}:
      value: 'product.js'
    ${{ if eq(parameters.selectTestRun, 'Product with Cost') }}:
      value: 'productCost.js'
    ${{ if eq(parameters.selectTestRun, 'Product with Attachments') }}:
      value: 'productAttachment.js'   
    ${{ if notIn(parameters.selectTestRun, 'Product', 'Product with Cost', 'Product with Attachments') }}:
      value: 'something-else.js' 
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您需要每次都重复此操作,如下所示:

variables:
  - name: fileName
    ${{ if eq(parameters.selectTestRun, 'Product') }}:
      value: 'product.js'
    ${{ elseif eq(parameters.selectTestRun, 'Product with Cost') }}:
      value: 'productCost.js'
    ${{ elseif eq(parameters.selectTestRun, 'Product with Attachments') }}:
      value: 'productAttachment.js'   
    ${{ else }}:
      value: 'something-else.js' 
Run Code Online (Sandbox Code Playgroud)

对于其他 if 也是如此。