如何解决如何使用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')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。