【问题标题】:CircleCI: Create Workflow with separate jobs. One build and different Deploys per environmentCircleCI:创建具有单独作业的工作流。每个环境一个构建和不同的部署
【发布时间】:2017-12-19 18:17:56
【问题描述】:

每个人。我需要一些帮助来解决我在为我的 Angular 项目配置 circleCi 时遇到的一些问题。

下面详细介绍了我用于构建和部署过程的 config.yml。目前我决定为每个环境做单独的工作,每个工作都包括构建和部署。这种方法的问题是我在重复自己,我找不到正确的方法来部署在之前的工作中为相同的工作流构建的工件。

version: 2
jobs:
  build:
    docker:
      - image: circleci/node:8-browsers
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - run:
          name: Install dependencies
          command: npm install
      - save_cache:
          key: dependency-cache-{{ checksum "package.json" }}
          paths:
            - .node_modules
      - run:
          name: Build Application (Production mode - aot enabled)
          command: npm run build:prod
      - store_artifacts:
          path: dist
          destination: dist
  deploy_prod:
    docker:
      - image: circleci/node:8-browsers
    environment:
      - FIREBASE_TOKEN: "1/AFF2414141ASdASDAKDA4141421sxscq"
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - run:
          name: Install dependencies
          command: npm install
      - save_cache:
          key: dependency-cache-{{ checksum "package.json" }}
          paths:
            - .node_modules
      - run:
          name: Build Application (Production mode - aot enabled)
          command: npm run build:prod
      - store_artifacts:
          path: dist
          destination: dist
      - run:
          command: ./node_modules/.bin/firebase use default
      - deploy:
          command: ./node_modules/.bin/firebase deploy --token=$FIREBASE_TOKEN
  deploy_qa:
    docker:
      - image: circleci/node:8-browsers
    environment:
      - FIREBASE_TOKEN: "1/AFF2414141ASdASDAKDA4141421sxscq"
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - run:
          name: Install dependencies
          command: npm install
      - save_cache:
          key: dependency-cache-{{ checksum "package.json" }}
          paths:
            - .node_modules
      - run:
          name: Build Application (Production mode - aot enabled)
          command: npm run build:prod
      - store_artifacts:
          path: dist
          destination: dist
      - run:
          command: ./node_modules/.bin/firebase use qa
      - deploy:
          command: ./node_modules/.bin/firebase deploy --token=$FIREBASE_TOKEN
workflows:
  version: 2
  build-and-deploy:
    jobs:
      - build:
          filters:
            branches:
              only:
                - master
              ignore:
                - /feat-.*/
      - deploy_prod:
          filters:
            branches:
              ignore:
                - /.*/
            tags:
              only:
                - /v[0-9]+(\.[0-9]+){2}/
      - deploy_qa:
          filters:
            branches:
              ignore:
                - /.*/
            tags:
              only:
                - /v[0-9]+(\.[0-9]+){2}-BETA-([0-9]*)/
  • 我了解每个作业都使用不同的 docker 映像,因此这会阻止我在同一个工作区工作。

    问:如何在同一个工作流程中为不同的作业使用同一个 docker 镜像?

  • 我认为 store_artifacts 对我有帮助,但我读到它只适用于通过仪表板或 API 使用。

    问:我能否恢复需要存储工件的不同作业的作业上的工件?

  • 我知道我在重复自己,我的目标是根据标签的名称为每个环境的部署作业提供一个构建作业。所以我的 deploy_{env} 工作主要是 firebase 命令。

    workflows:
      version: 2
      build-and-deploy:
        jobs:
        - build:
            filters:
              branches:
                only:
                  - master
                ignore:
                  - /feat-.*/
              tags:
                only:
                  - /v[0-9]+(\.[0-9]+){2}/
                  - /v[0-9]+(\.[0-9]+){2}-BETA-([0-9]*)/
        - deploy_prod:
            requires:
              - build
            filters:
              branches:
                ignore:
                  - /.*/
              tags:
                only:
                  - /v[0-9]+(\.[0-9]+){2}/
        - deploy_qa:
            requires:
              - build
            filters:
              branches:
                ignore:
                  - /.*/
              tags:
                only:
                  - /v[0-9]+(\.[0-9]+){2}-BETA-([0-9]*)/
    

    问:此解决方案的推荐步骤(最佳做法)是什么?

【问题讨论】:

    标签: yaml circleci


    【解决方案1】:

    问:如何在同一个工作流程中为不同的作业使用同一个 docker 镜像?

    可能有两种方法可以解决这个问题:

    1.) Docker 层缓存:https://circleci.com/docs/2.0/docker-layer-caching/

    2.) 缓存 .tar 文件:https://circleci.com/blog/how-to-build-a-docker-image-on-circleci-2-0/

    问:我能否在需要不同操作的工作中恢复工件? 存储工件的作业?

    persist_to_workspaceattach_workspace 键在这里应该会有所帮助:https://circleci.com/docs/2.0/workflows/#using-workspaces-to-share-data-among-jobs

    问:此解决方案的推荐步骤(最佳做法)是什么?

    这里不确定!无论哪种方式对您来说都是最快和最干净的。 :)

    【讨论】:

    • 感谢您的回答!几周前我发现了持久化和附加工作区的选项,它对某些情况很有帮助(我在不同的工作流程中使用它)。在这种特殊情况下,firebase deploy 需要运行未安装的 npm 命令,我认为使用它不是一个好主意。 Docker 层缓存看起来非常适合我的需要。我会看看它是如何工作的。
    • @Roda 我遇到了完全相同的问题,我需要在部署工作流程中调用 ./node_modules/.bin/firebase ,并且我已经在构建工作流程中安装了该问题。你找到解决办法了吗?
    • 最快的解决方案是持久连接工作区。虽然不是我想要的花哨的解决方案。那个项目是一个小型 POC,所以我没有花更多时间调查。但是,现在我会考虑创建自己的 docker 映像并在全局范围内安装 firebase-tools。
    猜你喜欢
    • 1970-01-01
    • 2022-09-27
    • 2014-08-04
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多