defaults: &defaults
  working_directory: ~/repo
  docker:
    - image: circleci/node:10.16.3
version: 2
jobs:
  build:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - run:
          name: Install NPM Packages
          command: yarn
      - save_cache:
          key: dependency-cache-{{ checksum "package.json" }}
          paths:
            - node_modules
      - run:
          name: Build Project
          command: yarn build:mojo
      - persist_to_workspace:
          root: ./
          paths:
            - release/
  deploy-s3:
    <<: *defaults
    docker:
      - image: circleci/python:2.7-jessie
    steps:
      - checkout
      - attach_workspace:
          at: ./
      - run:
          name: Install AWS CLI
          command: sudo pip install awscli
      - run:
          name: Deploy to s3
          command: ./shell/upload_assets.sh
  purge-cache:
    <<: *defaults
    steps:
      - run:
          name: Purge the Instart cache
          command: |
            curl -X POST \
              $INSTART_ENDPOINT \
              -H "Authorization: Basic $INSTART_API_KEY" \
              -H "Content-Type: application/json" \
              -H "cache-control: no-cache" \
              -d '{
              "purge_request":	{
                "action": "PURGE",
                "uris": [
                  {
                    "match_mode": "PREFIX_LITERAL",
                    "uri_pattern": "https://assets10.modaoperandi.com/assets/display-lib/mo-display-'"$CIRCLE_BRANCH"'.min.js"
                  },
                  {
                    "match_mode": "PREFIX_LITERAL",
                    "uri_pattern": "https://assets10.modaoperandi.com/assets/display-lib/mo-display-'"$CIRCLE_BRANCH"'.css"
                  },
                  {
                    "match_mode": "PREFIX_LITERAL",
                    "uri_pattern": "https://assets10.staging.modaoperandi.com/assets/display-lib/mo-display-'"$CIRCLE_BRANCH"'.min.js"
                  },
                  {
                    "match_mode": "PREFIX_LITERAL",
                    "uri_pattern": "https://assets10.staging.modaoperandi.com/assets/display-lib/mo-display-'"$CIRCLE_BRANCH"'.css"
                  }
                ]
              }
            }'
  deploy-storybook:
    docker:
      - image: circleci/python:2.7-jessie-node
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ./
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - run:
          name: Install AWS CLI
          command: sudo pip install awscli
      - run:
          name: Rebuild node-sass
          command: npm rebuild node-sass
      - run:
          name: Build storybook
          command: yarn build:storybook
      - run:
          name: Deploying storybook
          command: |
            aws s3 rm s3://storybook.modaoperandi.com/ --recursive
            aws s3 cp ./docs s3://storybook.modaoperandi.com/ --recursive
  release:
    docker:
      - image: 'circleci/node:latest'
    steps:
      - checkout
      # Download and cache dependencies
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "package.json" }}
            # Fallback to using the latest cache if no exact match is found
            - v1-dependencies-
      - run:
          name: install
          command: yarn install
      - run:
          name: build
          command: yarn build
      - run:
          name: release
          command: yarn run semantic-release || true

workflows:
  version: 2
  release:
    jobs:
      - release:
          filters:
            branches:
              only:
                - master
  build-deploy:
    jobs:
      - build
      - deploy-s3:
          requires:
            - build
      - purge-cache:
          requires:
            - build
            - deploy-s3
      - deploy-storybook:
          requires:
            - build
            - deploy-s3
            - purge-cache
          filters:
            branches:
              only:
                - master
