Roda
Roda

Reputation: 13

CircleCI: Create Workflow with separate jobs. One build and different Deploys per environment

everyone. I need some help with some issues that I am facing configuring circleCi for my Angular project.

The config.yml that I am using for a build and deploy process is detailed below. Currently I have decided to do separate jobs for each environment and each one includes the building and deploy. The problem with this approach is that I am repeating myself and I can't find the correct way to deploy an artifact builded in the previous job for the same workflow.

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]*)/

Upvotes: 1

Views: 2704

Answers (1)

Isaac Askew
Isaac Askew

Reputation: 1291

Q: How can I use the same docker image for different jobs in the same workflow?

There might be two methods of going about this:

1.) Docker Layer Caching: https://circleci.com/docs/2.0/docker-layer-caching/

2.) Caching the .tar file: https://circleci.com/blog/how-to-build-a-docker-image-on-circleci-2-0/

Q: Am I able to recover an artifact on a job that requires a different job that stored the artifact?

The persist_to_workspace and attach_workspace keys should be helpful here: https://circleci.com/docs/2.0/workflows/#using-workspaces-to-share-data-among-jobs

Q: What are the recommended steps (best practices) for this solution?

Not sure here! Whatever works the fastest and cleanest for you. :)

Upvotes: 1

Related Questions