version: 2.1

executors:
  default:
    working_directory: /tmp/workspace
    docker:
      - image: misskey/ci:latest
      - image: circleci/redis:latest
  docker:
    working_directory: /tmp/workspace
    docker:
      - image: docker:latest

jobs:
  build:
    executor: default
    steps:
      - checkout
      - run:
          name: Ensure yarn.lock
          command: |
            touch yarn.lock
      - restore_cache:
          name: Restore npm package caches
          keys:
            - yarn-v1-arch-{{ arch }}-env-{{ .Environment.variableName }}-package-{{ checksum "package.json" }}-lock-{{ checksum "yarn.lock" }}
            - yarn-v1-arch-{{ arch }}-env-{{ .Environment.variableName }}-package-{{ checksum "package.json" }}-
            - yarn-v1-arch-{{ arch }}-env-{{ .Environment.variableName }}-
            - yarn-v1-arch-{{ arch }}-
            - yarn-v1-
      - run:
          name: Install Dependencies
          command: |
            yarn install
      - run:
          name: Configure
          command: |
            cp .circleci/misskey/default.yml .config
            cp .circleci/misskey/test.yml .config
      - run:
          name: Build
          command: |
            yarn build
            touch yarn.lock
      - save_cache:
          name: Cache npm packages
          key: yarn-v1-arch-{{ arch }}-env-{{ .Environment.variableName }}-package-{{ checksum "package.json" }}-lock-{{ checksum "yarn.lock" }}
          paths:
            - node_modules
      - persist_to_workspace:
          root: .
          paths:
            - .
  test:
    parameters:
      executor:
        type: string
        default: "default"
    executor: <<parameters.executor>>
    steps:
      - attach_workspace:
          at: /tmp/workspace
      - run:
          name: Test
          command: |
            yarn test
            touch yarn.lock
      - save_cache:
          name: Cache npm packages
          key: yarn-v1-arch-{{ arch }}-env-{{ .Environment.variableName }}-package-{{ checksum "package.json" }}-lock-{{ checksum "yarn.lock" }}
          paths:
            - node_modules
  docker:
    parameters:
      with_deploy:
        type: boolean
        default: false
    executor: docker
    steps:
      - checkout
      - setup_remote_docker
      - run:
          name: Build
          command: |
            docker build -t misskey/misskey .
      - when:
          condition: <<parameters.with_deploy>>
          steps:
            - run:
                name: Deploy
                command: |
                  if [ "$DOCKERHUB_USERNAME$DOCKERHUB_PASSWORD" ]
                   then
                    apk update && apk add jq
                    docker tag misskey/misskey misskey/misskey:$(cat package.json | jq -r .version)
                    docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD
                    docker push misskey/misskey
                   else
                    echo -e '\033[0;33mAborted deploying to Docker Hub\033[0;39m'
                  fi

workflows:
  version: 2
  nodejs:
    jobs:
      - hold:
          name: manual-build-trigger
          type: approval
          filters:
            branches:
              ignore: master
      - build:
          name: manual-build
          requires:
            - manual-build-trigger
          filters:
            branches:
              ignore: master
      - build:
          name: auto-build
          filters:
            branches:
              only: master
      - test:
          name: manual-test
          requires:
            - manual-build
          filters:
            branches:
              ignore: master
      - test:
          name: auto-test
          requires:
            - auto-build
          filters:
            branches:
              only: master
  docker:
    jobs:
      - hold:
          name: manual-build-trigger
          type: approval
          filters:
            branches:
              ignore: master
      - docker:
          name: manual-build
          requires:
            - manual-build-trigger
          filters:
            branches:
              ignore: master
      - docker:
          name: auto-build
          with_deploy: true
          filters:
            branches:
              only: master
