azure - 如何在 Azure DevOps YAML 的条件中使用数组

标签 azure yaml devops

我有一个模板,它为每个环境定义了一组分支,以便我可以控制哪些分支导致部署到特定环境。例如,我想要部署到生产的唯一分支是 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/

相关文章:

amazon-web-services - 如何处理在 AWS 上从 CloudFormation 模板创建失败的错误?

php - Symfony FK 约束问题

azure - 在ARM模板中获取Azure存储dacpac文件

amazon-web-services - 从已删除的 opensearch 域中恢复数据

node.js - 在 Docker 中使用私有(private) npm 仓库

具有 DataType.Complex 及其子字段的 Azure 搜索建议

azure - AWVERIFY 未进行属性(property)化

azure - 如何一致且成功地将 Azure Service Fabric 应用程序部署到本地群集?

azure - 将 NextJs SSR 部署到 Azure 静态 Web 应用

在 Go 中解析 YAML : map in list