如何在CircleCI 2.0工作流程中进行条件手动批准

时间:2017-11-02 11:57:08

标签: continuous-integration yaml circleci circleci-workflows

我有一个简单的用例,我只想为特定的分支和/或标记创建manual approval

type:approval的工作流作业具有与所有其他作业类似的过滤器,但需要手动批准(或不需要)的作业 foo 将使用类似requires: ['approve']的内容,然后是与之密切相关。

这意味着如果批准步骤与过滤器不匹配, foo 将永远不会发生。

那么..任何干净的解决方法,在yaml文件中没有多少重复项?

修改:Same question on CircleCI Discuss

2 个答案:

答案 0 :(得分:4)

使用YAML alias map

这是一种黑客攻击,但YAML alias map您重复使用步骤并使用不同的过滤器制作两个单独的工作流路径:一个获得批准,另一个没有。

以下是一个完整的例子:

version: 2.0

# Run job (defined in a YAML alias map, see http://yaml.org/type/merge.html)
run-core: &run-core
    docker:
      - image: circleci/node:8
    steps:
      - checkout
      - restore_cache: { key: 'xxxxx' }
      - run: npm install
      - save_cache: { key: 'xxxxx', paths: ['xxxx'] }
      - run: npm run build
      - run: npm run validate
      - deploy: ./scripts/deploy.sh

# Jobs (duplicate the same job, but with different names)
jobs:
  run:
    <<: *run-core
  run-with-approval:
    <<: *run-core

# This will allow manual approval and context
# See https://circleci.com/docs/2.0/workflows/#git-tag-job-execution
workflows:
  version: 2
  run:
    jobs:
      # Without approval (for all branches except staging)
      - run:
          context: org-global
          filters:
            branches: { ignore: 'staging' } # All branches except staging
            tags: { ignore: '/.*/' }        # Ignore all tags
      # With approval (only for tags and staging branch)
      - run-with-approval:
          context: org-global
          filters:
            tags: { only: '/.*/' }          # All branches and all tags
          requires: ['approve']             # But requires approval (which is filtering)
      - approve:
          type: approval
          filters:
            branches: { only: 'staging' }   # Ignore all branches except staging
            tags: { only: '/.*/' }          # All tags

我希望这会有所帮助

答案 1 :(得分:2)

使用最新答案更新此问题: CircleCI在预览中具有v2 API(以支持CircleCI 2.1),该API除其他外还支持条件工作流(即条件保持步骤)

conditional workflow formatting

- when:
    condition: << pipeline.parameters.test >>
    steps:
      - hold-step