version: 2
defaults: &defaults
  docker:
    - image: circleci/node:8.10
  working_directory: ~/circleci-deployment
jobs:
  install:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          key: yarn-cache-{{ .Branch }}-{{ checksum "yarn.lock" }}
      - run: yarn
      - save_cache:
          paths:
            - node_modules
          key: yarn-cache-{{ .Branch }}-{{ checksum "yarn.lock" }}

  test:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          key: yarn-cache-{{ .Branch }}-{{ checksum "yarn.lock" }}
      - run: yarn run validate
      - save_cache:
          key: v1-dist-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_SHA1 }}
          paths:
            - dist

  deploy:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          keys:
            - yarn-cache-{{ .Branch }}-{{ checksum "yarn.lock" }}
            - v1-dist-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_SHA1 }}
      - run: cp .npmrc-ci .npmrc
      - run: yarn run release

  publish-njs810:
    docker:
      - image: circleci/node:8-stretch
    working_directory: ~/circleci-deployment
    steps:
      - checkout
      - run: sudo apt-get install -y python3-pip
      - run:
          name: Install publish dependencies
          command: sudo pip3 install -U awscli
      - run:
          name: Publish layers
          command: ./publish-layers.sh nodejs8.10

  publish-njs10:
    docker:
      - image: circleci/node:10-stretch
    working_directory: ~/circleci-deployment
    steps:
      - checkout
      - run: sudo apt-get install -y python3-pip
      - run:
          name: Install publish dependencies
          command: sudo pip3 install -U awscli
      - run:
          name: Publish layers
          command: ./publish-layers.sh nodejs10.x

workflows:
  version: 2
  all:
    jobs:
      - install:
          filters:
            branches:
              only: /.*/
            tags:
              only: /.*/
      - test:
          requires:
            - install
          filters:
            branches:
              only: /.*/
            tags:
              only: /.*/
      - deploy:
          requires:
            - install
            - test
          filters:
            branches:
              only: master
            tags:
              ignore: /.*/
      - publish-njs810:
          requires:
            - install
            - test
          filters:
            branches:
              ignore: /.*/
            tags:
              only: /^v.*/
      - publish-njs10:
          requires:
            - install
            - test
          filters:
            branches:
              ignore: /.*/
            tags:
              only: /^v.*/
