# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
jobs:
  lint:
    working_directory: ~/repo
    docker:
      - image: circleci/node:9
    steps:
      - checkout
      - restore_cache: # special step to restore the dependency cache
          keys:
            # Find a cache corresponding to this specific yarn.lock checksum
            # when this file is changed, this key will fail
            - yarn-cache-{{ checksum "yarn.lock" }}
            # Find the most recently generated cache used from any branch
            - yarn-cache-
      - run: yarn install --frozen-lockfile
      - save_cache:
          key: yarn-cache-{{ checksum "yarn.lock" }}
          paths:
            - node_modules
      - run: yarn run lint
  build:
    working_directory: ~/repo
    docker:
      - image: circleci/node:9
    steps:
      - checkout
      - restore_cache: # special step to restore the dependency cache
          keys:
            # Find a cache corresponding to this specific yarn.lock checksum
            # when this file is changed, this key will fail
            - yarn-cache-{{ checksum "yarn.lock" }}
            # Find the most recently generated cache used from any branch
            - dependency-cache-
      - run: yarn install --frozen-lockfile
      - save_cache:
          key: yarn-cache-{{ checksum "yarn.lock" }}
          paths:
            - node_modules
      - run: yarn run build
      - persist_to_workspace:
          # Must be an absolute path, or relative path from working_directory. This is a directory on the container which is
          # taken to be the root directory of the workspace.
          root: ~/repo
          paths:
            - node_modules
            - build
            - server/build
  firebase_staging:
    working_directory: ~/repo
    docker:
      - image: circleci/node:9
    steps:
      - checkout
      - attach_workspace:
          # Must be absolute path or relative path from working_directory
          at: ~/repo
      - run:
          name: Deploy Release to Firebase
          command: ./node_modules/.bin/firebase deploy --token=$FIREBASE_DEPLOY_TOKEN -P staging
  firebase_develop:
    working_directory: ~/repo
    docker:
      - image: circleci/node:9
    steps:
      - checkout
      - attach_workspace:
          # Must be absolute path or relative path from working_directory
          at: ~/repo
      - run:
          name: Deploy Develop to Firebase
          command: ./node_modules/.bin/firebase deploy --token=$FIREBASE_DEPLOY_TOKEN -P dev
  firebase_master:
    working_directory: ~/repo
    docker:
      - image: circleci/node:9
    steps:
      - checkout
      - attach_workspace:
          # Must be absolute path or relative path from working_directory
          at: ~/repo
      - run:
          name: Deploy Master to Firebase
          command: ./node_modules/.bin/firebase deploy --token=$FIREBASE_DEPLOY_TOKEN -P production
  release:
    working_directory: ~/repo
    docker:
      - image: adactive/docker-electron-installer-windows
    steps:
      - checkout
      - attach_workspace:
          # Must be absolute path or relative path from working_directory
          at: ~/repo
      - run: yarn run adloader
workflows:
  version: 2
  deploy:
    jobs:
      - lint:
          filters:
            tags: # Runs on every branch & every tags
              only: /.*/
      - build:
          requires:
            - lint
          filters:
            tags:
              only: /.*/
      - firebase_staging:
          requires:
            - build
          filters:
            branches:
              only: /(release|hotfix).*/
      - firebase_develop:
          requires:
            - build
          filters:
            branches:
              only: develop
      - firebase_master:
          requires:
            - build
          filters:
            branches:
              only: master
      - release:
          requires:
            - build
          filters:
            branches:
              ignore: /.*/
            tags:
              only: /v[0-9]+\.[0-9]+\.[0-9]+.*/
