我有一个模板,它为每个环境定义了一组分支,以便我可以控制哪些分支导致部署到特定环境。例如,我想要部署到生产的唯一分支是 master,但对于 UAT,我想要发布、修补程序和 master。我已经设置了一个父模板,它在“每个”循环中调用下游模板。在部署模板内,我想将允许的分支数组与当前分支进行比较,以确定是否继续。这是传递分支数组的父模板的一部分:
- template: pipeline-templates/environment-pipeline.yml
parameters:
envs:
- name: devtest
branches:
- 'refs/heads/master'
- 'refs/heads/develop'
- 'refs/heads/hotfix/'
- 'refs/heads/release/'
- name: nightly
branches:
- 'refs/heads/master'
- 'refs/heads/develop'
- 'refs/heads/hotfix/'
- 'refs/heads/release/'
- name: qa
branches:
- 'refs/heads/master'
- 'refs/heads/hotfix/'
- 'refs/heads/release/'
- name: prod
branches:
- 'refs/heads/master'
下面的environment-pipeline.yml
然后调用每个环境的部署模板。
parameters:
- name: envs # collection of environments to create/deploy
type: object
default:
env:
name: ''
branches: []
stages:
- ${{ each env in parameters.envs }}:
- template: deploy.yml
parameters:
environmentName: ${{ env.name }}
onlyForBranches: ${{ env.branches }}
以上一切都工作正常(注意,我删除了很多 YAML 以简化示例)。下一阶段是在部署模板中使用带有分支数组的条件。这就是我希望它能够发挥作用的方式,但事实并非如此。
parameters:
environmentName: ''
onlyForBranches: []
stages:
- stage: branch_gate
condition: and(succeeded(), in(variables['Build.SourceBranch'], ${{ parameters.onlyForBranches }}))
# then the deploy stages go here if above condition passes
这会导致以下错误:
Unable to convert from Array to String.
是否有某种方法可以实现此目的,或者我应该使用不同的方法?
最佳答案
in
不是要使用的函数。它需要 n 个参数,并且如果第一个参数等于其余任何一个参数,则为 true:in('B', 'A', 'B', 'C') => true
尝试containsvalue相反:
条件:and(succeeded(), containsvalue(${{parameters.onlyForBranches }},variables['Build.SourceBranch']))
关于azure - 如何在 Azure DevOps YAML 的条件中使用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61727244/