version: 2.1

executors:
  docker-publisher:
    environment:
      IMAGE_NAME: outlinewiki/outline
    docker:
      - image: circleci/buildpack-deps:stretch

jobs:
  build:
    working_directory: ~/outline
    docker:
      - image: circleci/node:14
      - image: circleci/redis:latest
      - image: circleci/postgres:9.6.5-alpine-ram
    environment:
      NODE_ENV: test
      SECRET_KEY: F0E5AD933D7F6FD8F4DBB3E038C501C052DC0593C686D21ACB30AE205D2F634B
      DATABASE_URL_TEST: postgres://root@localhost:5432/circle_test
      DATABASE_URL: postgres://root@localhost:5432/circle_test
      URL: http://localhost:3000
      SMTP_FROM_EMAIL: hello@example.com
      AWS_S3_UPLOAD_BUCKET_URL: https://s3.amazonaws.com
      AWS_S3_UPLOAD_BUCKET_NAME: outline-circle
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - run:
          name: install-deps
          command: yarn install --pure-lockfile
      - save_cache:
          key: dependency-cache-{{ checksum "package.json" }}
          paths:
            - ./node_modules
      - run:
          name: migrate
          command: ./node_modules/.bin/sequelize db:migrate --url $DATABASE_URL_TEST
      - run:
          name: lint
          command: yarn lint
      - run:
          name: flow
          command: yarn flow check --max-workers 4
      - run:
          name: test
          command: yarn test
      - run:
          name: build-webpack
          command: yarn build:webpack
  build-image:
    executor: docker-publisher
    steps:
      - checkout
      - setup_remote_docker:
          version: 20.10.6
      - run:
          name: Build Docker image
          command: docker build -t $IMAGE_NAME:latest .
      - run:
          name: Archive Docker image
          command: docker save -o image.tar $IMAGE_NAME
      - persist_to_workspace:
          root: .
          paths:
            - ./image.tar
  publish-latest:
    executor: docker-publisher
    steps:
      - attach_workspace:
          at: /tmp/workspace
      - setup_remote_docker:
          version: 20.10.6
      - run:
          name: Load archived Docker image
          command: docker load -i /tmp/workspace/image.tar
      - run:
          name: Publish Docker Image to Docker Hub
          command: |
            echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
            IMAGE_TAG=${CIRCLE_TAG/v/''}
            docker tag $IMAGE_NAME:latest $IMAGE_NAME:$IMAGE_TAG
            docker push $IMAGE_NAME:latest
            docker push $IMAGE_NAME:$IMAGE_TAG
  publish-tag:
    executor: docker-publisher
    steps:
      - attach_workspace:
          at: /tmp/workspace
      - setup_remote_docker:
          version: 20.10.6
      - run:
          name: Load archived Docker image
          command: docker load -i /tmp/workspace/image.tar
      - run:
          name: Publish Docker Image to Docker Hub
          command: |
            echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
            IMAGE_TAG=${CIRCLE_TAG/v/''}
            docker tag $IMAGE_NAME:latest $IMAGE_NAME:$IMAGE_TAG
            docker push $IMAGE_NAME:$IMAGE_TAG

workflows:
  version: 2
  build-and-test:
    jobs:
      - build:
          filters:
            tags:
              ignore: /^v.*/
  build-docker:
    jobs:
      - build-image:
          filters:
            tags:
              only: /^v.*/
            branches:
              ignore: /.*/
      - publish-latest:
          requires:
            - build-image
          filters:
            tags:
              only: /^v\d+\.\d+\.\d+$/
            branches:
              ignore: /.*/
      - publish-tag:
          requires:
            - build-image
          filters:
            tags:
              only: /^v\d+\.\d+\.\d+-.*$/
            branches:
              ignore: /.*/
