如何检查数组是否包含带有 Azure DevOps 管道表达式的字符串
How to check if array contains string with Azure DevOps pipeline expressions
我有以下管道模板,我想使用它根据输入参数 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', '${{ 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
.
那么现在的问题是:
如何检查字符串是否是定义为参数的数组的一部分?
请尝试 contains:
condition: contains('${{ join(';',parameters.stages) }}', 'test')
2022 年更新
您现在可以使用 containsValue:
condition: ${{ containsValue(parameters.stages, 'test') }}
我有以下管道模板,我想使用它根据输入参数 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', '${{ 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
.
那么现在的问题是:
如何检查字符串是否是定义为参数的数组的一部分?
请尝试 contains:
condition: contains('${{ join(';',parameters.stages) }}', 'test')
2022 年更新
您现在可以使用 containsValue:
condition: ${{ containsValue(parameters.stages, 'test') }}