version: 2

node_key: &node_key
  key: node-deps-v2-{{ arch }}-{{ checksum "yarn.lock" }}-{{ checksum "circle.yml" }}

restore_node: &restore_node
  restore_cache:
    <<: *node_key

install_node: &install_node
  run:
    name: node deps
    command: |
      if [ ! -d node_modules ]; then
        yarn --pure-lockfile
      fi

save_node: &save_node
  save_cache:
    <<: *node_key
    paths:
      - node_modules

container_defaults: &defaults
  docker:
    - image: circleci/node:10-browsers
      environment:
        JOBS: 2

jobs:
  install:
    <<: *defaults
    steps:
      - checkout

      - <<: *restore_node
      - <<: *install_node
      - <<: *save_node

  test:
    <<: *defaults
    environment:
      CIRCLE_TEST_REPORTS: test-results

    steps:
      - checkout
      - <<: *restore_node
      - run:
          name: Lint Styles
          command: yarn run lint:styles
      - run:
          name: Run Ember Tests
          command: npx ember test

      - store_test_results:
          path: test-results/

  build-storybook:
    <<: *defaults
    steps:
      - checkout
      - <<: *restore_node
      - run:
          name: Build Ember
          command: npx ember build
      - run:
          name: Build Docs
          command: |
            [[ $CIRCLE_BRANCH = "main" ]] && RELEASE="latest" || { [[ -n $CIRCLE_BRANCH ]] && RELEASE=$CIRCLE_BRANCH || RELEASE=$CIRCLE_TAG; }
            npm run build-storybook -- -o storybook-static/$RELEASE
            cp index.html storybook-config.json storybook-static/
      - persist_to_workspace:
          root: .
          paths:
            - storybook-static


  deploy-storybook-to-s3:
    <<: *defaults
    steps:
      - checkout
      - <<: *restore_node
      - attach_workspace:
          at: .
      - run:
          name: Disable jekyll builds
          command: touch storybook-static/.nojekyll
      - run:
          name: Install AWSCLI
          command: |
            sudo curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
            sudo unzip awscliv2.zip
            sudo ./aws/install
            aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
            aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
            aws configure set default.region $AWS_DEFAULT_REGION

      - run:
          name: Deploy docs to aws s3
          command: |
            [[ $CIRCLE_BRANCH = "main" ]] && RELEASE="latest" || { [[ -n $CIRCLE_BRANCH ]] && RELEASE=$CIRCLE_BRANCH || RELEASE=$CIRCLE_TAG; }
            yarn storybook-to-aws-s3 --bucket-path=$S3_BUCKET/$RELEASE -e=./storybook-static/$RELEASE
            aws s3 cp storybook-static/index.html s3://$S3_BUCKET
            aws s3 cp storybook-static/storybook-config.json s3://$S3_BUCKET
            AWS_PAGER="" aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_ID --paths "/index.html" "/storybook-config.json" "/$RELEASE/*"

workflows:
  version: 2
  just-test:
    jobs:
      - install:
          filters:
            branches:
              ignore: main
      - test:
          requires:
            - install

  deploy-storybook-to-s3:
    jobs:
      - install:
          filters:
            tags:
              only: /^v[0-9]+\.[0-9]+\.[0-9]+/
            branches:
              only:
                - main
                - /qa\/.*/
      - test:
          requires:
            - install
      - build-storybook:
          requires:
            - test
      - deploy-storybook-to-s3:
          requires:
            - build-storybook
