如何使用 Azure DevOps 管道表达式检查数组是否包含字符串

Sil*_*hus 5 yaml azure-devops azure-yaml-pipelines

我有以下管道模板,我想用它来根据输入参数有条件地执行阶段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 }}
Run Code Online (Sandbox Code Playgroud)

在示例中,您可以看到我尝试过的两种方法(我尝试了很多......)。最后一个 ( in('prod', '${{ join(', ', 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

那么现在的问题是:

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

pat*_*iml 11

2022年更新

您现在可以使用containsValue

condition: ${{ containsValue(parameters.stages, 'test') }}
Run Code Online (Sandbox Code Playgroud)


Cec*_*SFT 6

尝试包含

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