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

How to write if else condition in Azure DevOps Pipeline

我有一个 azure 管道和 运行 参数,其中我有多个选项,如下所示:

Select 测试执行

如果我 select Product 然后我执行 product.js 文件,如果我 select Product with Cost 然后执行"productCost.js" 等等。我写了一个天蓝色的管道脚本来做到这一点。我有另一个条件 "Generate Test Data" 复选框,其中 returns 布尔值 true false 如果值为 true 那么我必须 select 一个文件 productWithTestData.js 如果 Product 是 selected - 我不知道如何在 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'

我的管道代码:

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}}
                

没有else。如果你正在测试平等,“其他”将是测试不平等:

${{ if eq(parameters.selectTestRun, 'Product') }}:
  value: 'product.js'
${{ if ne(parameters.selectTestRun, 'Product') }}:
  value: 'something else'

更新:2021 年 9 月 9 日

Now we have also if else condition available:

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

所以我们可以写

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' 

原回复

在这种情况下您应该使用 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' 

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

 ${{ if and(eq(parameters.selectTestRun, 'Product'), ne(parameters.testData, True)) }}:
      value: 'product.js'
 ${{ if and(eq(parameters.selectTestRun, 'Product'),eq(parameters.testData, True)) }}:
      value: 'productWithTestData.js'

其他 if 也一样。