CircleCI UP

# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
references:
  container_config: &container_config
    docker:
      - image: circleci/node:9
    working_directory: ~/repo
    
jobs:
  checkout_code:
    <<: *container_config
    steps:
      - checkout
      - save_cache:
          key: v1-repo-{{ .Environment.CIRCLE_SHA1 }}
          paths: ~/repo
  bundle_dependencies:
    <<: *container_config
    steps:
      - restore_cache:
          key: v1-repo-{{ .Environment.CIRCLE_SHA1 }}
      - restore_cache:
          key: v1-bundle-{{ checksum "yarn.lock" }}
      - run:
          name: install-dependencies
          command: yarn install
      - save_cache:
          key: v1-bundle-{{ checksum "yarn.lock" }}
          paths:
            - node_modules
  install_up:
    <<: *container_config
    steps:
      - run:
          name: install-up-runtime
          command: |
            curl -sf https://up.apex.sh/install | bash -s -- -b ~/up_bin
            ~/up_bin/up -v upgrade
      - save_cache:
          key: v1-bin-{{ .Environment.CIRCLE_SHA1 }}
          paths:
            - ~/up_bin
  deploy_to_staging:
    <<: *container_config
    steps:
      - restore_cache:
          key: v1-repo-{{ .Environment.CIRCLE_SHA1 }}
      - restore_cache:
          key: v1-bundle-{{ checksum "yarn.lock" }}
      - restore_cache:
          key: v1-bin-{{ .Environment.CIRCLE_SHA1 }}
      - deploy:
          name: deploy-with-up
          command: |
            ~/up_bin/up deploy staging
  deploy_to_production:
    <<: *container_config
    steps:
      - restore_cache:
          key: v1-repo-{{ .Environment.CIRCLE_SHA1 }}
      - restore_cache:
          key: v1-bundle-{{ checksum "yarn.lock" }}
      - restore_cache:
          key: v1-bin-{{ .Environment.CIRCLE_SHA1 }}
      - deploy:
          name: deploy-with-up
          command: |
            ~/up_bin/up deploy production
workflows:
  version: 2
  build_and_deploy:
    jobs:
      - checkout_code
      - bundle_dependencies:
          requires:
            - checkout_code
      - install_up
      - deploy_to_staging:
          filters:
            branches:
              only:
                - develop
          requires:
            - install_up
            - bundle_dependencies
      - deploy_to_production:
          filters:
            branches:
              only:
                - master
          requires:
            - install_up
            - bundle_dependencies