如何在CircleCI 2.0的部署步骤中的构建步骤中使用Docker映像?

时间:2019-02-06 08:10:30

标签: docker deployment circleci-2.0 circleci-workflows

为从CircleCI 1.0迁移到2.0而苦苦挣扎的最后几天,虽然构建过程完成了,但部署仍然是一个大问题。 CircleCI文档并没有太大帮助。

这与我的config.yml类似:

version 2

jobs:
  build:
    docker:
      - image: circleci/node:8.9.1

    steps:
      - checkout
      - setup_remote_docker

      - run
          name: Install required stuff
          command: [...]

      - run:
          name: Build
          command: docker build -t project .

  deploy:
    docker:
      - image: circleci/node:8.9.1
    steps:
      - checkout
      - run:
          name: Deploy
          command: |
            bash scripts/deploy/deploy.sh
            docker tag project [...]
            docker push [...]

workflows:
  version: 2
  build-deploy:
    jobs:
      - build
      - deploy:
          requires:
            - build
          filters:
            branches:
              only: develop

问题出在deploy工作中。我必须指定docker: -image点,但是我想重用build作业中已经安装了所有必需内容的环境。当然,我可以将它们安装在deploy作业中,但是拥有多个deploy作业会导致代码重复,这是我不希望的。

2 个答案:

答案 0 :(得分:0)

如果标记由build阶段构建的图像,则可以在deploy阶段引用它:https://docs.docker.com/compose/compose-file/#labels

答案 1 :(得分:0)

您可能想保留到工作区并将其附加到部署作业中。 之后,您将不需要使用“-结帐”

https://circleci.com/docs/2.0/configuration-reference/#persist_to_workspace

jobs:
    build:
        docker:
            - image: circleci/node:8.9.1

        steps:
            - checkout
            - setup_remote_docker

            - run
                name: Install required stuff
                command: [...]

            - run:
                  name: Build
                  command: docker build -t project .
            - persist_to_workspace:
                    root: ./
                    paths:
                        - ./

    deploy:
        docker:
            - image: circleci/node:8.9.1
        steps:
            - attach_workspace:
                at: ./
            - run:
                  name: Deploy
                  command: |
                      bash scripts/deploy/deploy.sh
                      docker tag project [...]
                      docker push [...]

workflows:
    version: 2
    build-deploy:
        jobs:
            - build
            - deploy:
                  requires:
                      - build
                  filters:
                      branches:
                          only: develop