cloudfront_distribution: &cloudfront_distribution E2CFWD46Q7S68D

install_yarn: &install_yarn
  run:
    name: Install Yarn
    command: |
      curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
      echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
      sudo apt-get install -y apt-transport-https
      sudo apt-get update && sudo apt-get install yarn

install_webpack: &install_webpack
  run:
    name: Install Webpack
    command: sudo npm install -g webpack

version: 2
jobs:
  setup:
    docker:
      - image: cimg/node:16.17.1
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - <<: *install_yarn
      - run:
          name: Install Application Deps
          command: yarn install
      - save_cache:
          key: dependency-cache-{{ checksum "package.json" }}
          paths:
            - node_modules
  build:
    docker:
      - image: cimg/node:16.17.1
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - <<: *install_yarn
      - <<: *install_webpack
      - run:
          name: Build Application
          command: yarn run build
      - save_cache:
          key: built-application-{{ .Environment.CIRCLE_SHA1 }}
          paths:
            - dist
  deploy-script:
    docker:
      - image: circleci/python:2.7-stretch-node
    environment:
      CLOUDFROUNT_DISTRIBUTION: *cloudfront_distribution
    steps:
      - checkout
      - restore_cache:
          key: built-application-{{ .Environment.CIRCLE_SHA1 }}
      - run:
          name: Install AWS CLI
          command: sudo pip install awscli
      - run:
          name: Publish to S3
          command: |
            aws s3 sync ./dist/ s3://static.forthepeople.com/engineering/icarus/$CIRCLE_TAG/
            aws s3 sync ./dist/ s3://static.forthepeople.com/engineering/icarus/$(python ./bin/extract-minor-version.py $CIRCLE_TAG)-latest/
            aws configure set preview.cloudfront true
            aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_DISTRIBUTION --paths /engineering/icarus/$(python ./bin/extract-minor-version.py $CIRCLE_TAG)-latest
  deploy-module:
    docker:
      - image: cimg/node:16.17.1
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - restore_cache:
          key: built-application-{{ .Environment.CIRCLE_SHA1 }}
      - run:
          name: NPM login
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
      - run:
          name: Publish to NPM
          command: npm publish
workflows:
  version: 2
  build_and_deploy:
    jobs:
      - setup:
          filters:
            tags:
              only: /^v[0-9]+(\.[0-9]+)*/
      - build:
          requires:
            - setup
          filters:
            tags:
              only: /^v[0-9]+(\.[0-9]+)*/
      - deploy-script:
          requires:
            - build
          filters:
            branches:
              ignore: /.*/
            tags:
              only: /^v[0-9]+(\.[0-9]+)*/
      - deploy-module:
          requires:
            - build
          context: NPM
          filters:
            branches:
              ignore: /.*/
            tags:
              only: /^v[0-9]+(\.[0-9]+)*/
