如何使用Azure DevOps管道表达式检查数组是否包含字符串 如何检查字符串是否属于定义为参数的数组的一部分?

问题描述

我想使用以下管道模板,根据输入参数stages有条件地执行阶段。

parameters:
- name: dryRun
  default: false
  type: boolean
- name: path
  type: string
  default: terraform
- name: stages
  type: object
  default:
  - test
  - prod

stages:
  - stage:
    pool: 
      vmImage: 'ubuntu-latest'
    displayName: "Deploy to test"
    condition: in('test',${{ parameters.stages }})
    jobs:
    - template: terraform-job.yaml
      parameters:
        stage: test
        path: ${{ parameters.path }}
        dryRun: ${{ parameters.dryRun }}
  - stage:
    pool: 
      vmImage: 'ubuntu-latest'
    displayName: "Deploy to production"
    condition: in('prod','${{ join(',',parameters.stages) }}')
    jobs:
    - template: terraform-job.yaml
      parameters:
        stage: production
        path: ${{ parameters.path }}
        dryRun: ${{ parameters.dryRun }}

在示例中,您可以看到我尝试过的两种方法(我尝试了很多……)。最后一个in('prod',parameters.stages) }}'))实际上可以编译,但是当数组转换为单个字符串'test,prod'时,该检查仅部分起作用:in('test','test,prod')将使in('test',${{ parameters.stages }})检查失败。

一个示例(/terraform-deployment.yml (Line: 19,Col: 16): Unable to convert from Array to String. Value: Array)是我认为应该与逻辑思维结合使用的示例,但是在编译模板时出现以下错误aws s3 ls <BCKT_NAME> --recursive | sort | grep "2020-08-*" | cut -b 32- > a.txt

现在是问题:

如何检查字符串是否属于定义为参数的数组的一部分?

解决方法

改为尝试contains

condition: contains('${{ join(';',parameters.stages) }}','test')