version: 2
jobs:
  awesome:
    docker:
      - image: circleci/ruby:2.4.2-jessie-node
    steps:
      - checkout
      - run: gem install awesome_bot
      - run: awesome_bot --allow-redirect --allow-dupe --allow-ssl -w ipfs.io README.md

  build:
    docker:
      - image: circleci/node:10.15.1-browsers
        environment:
          NO_SANDBOX: true
    steps:
      - checkout
      - run: npm ci
      - run: npm run build
      - persist_to_workspace:
          root: .
          paths:
            - public

  deploy:
    docker:
      - image: olizilla/ipfs-dns-deploy
        environment:
          DOMAIN: awesome.ipfs.io
          BUILD_DIR: public
    steps:
      - attach_workspace:
          at: /tmp/workspace
      - run:
          name: Deploy website to IPFS
          command: |
            pin_name="$DOMAIN build $CIRCLE_BUILD_NUMBER"

            hash=$(pin-to-cluster.sh "$pin_name" /tmp/workspace/$BUILD_DIR)

            echo "Website added to IPFS: https://ipfs.io/ipfs/$hash"

            if [ "$CIRCLE_BRANCH" == "master" ] ; then
              dnslink-dnsimple -d $DOMAIN -r _dnslink -l /ipfs/$hash
            fi

workflows:
  version: 2
  check-links:
    triggers:
      - schedule:
          cron: "0 0 * * *"
          filters:
            branches:
              only:
                - master
    jobs:
      - awesome
  build-deploy:
    jobs:
      - awesome
      - build
      - deploy:
          context: ipfs-dns-deploy
          requires:
            - build
