version: 2
jobs:
  build:
    docker:
      - image: circleci/ruby:2.6.1-node-browsers
    working_directory: ~/repo
    steps:
      - checkout
      - setup_remote_docker:
          reusable: true
      - run:
          name: Install dependencies
          command: |
            .circleci/install_dep.sh
      - run:
          name: Builder
          command: |
            rake build -t -v
            cp -R pkg /tmp
      - persist_to_workspace:
          root: /tmp
          paths:
            - pkg

  test:
    docker:
       - image: circleci/ruby:2.6.1-node-browsers
    working_directory: ~/repo
    steps:
      - attach_workspace:
          at: /tmp
      - checkout
      - setup_remote_docker:
           reusable: true
      - run:
          name: Install dependencies
          command: |
            .circleci/install_dep.sh
      - run:
          name: Run unit tests
          command: |
            rake test -t -v

  push:
    docker:
      - image: circleci/ruby:2.6.1-node-browsers
    working_directory: ~/repo
    steps:
      - attach_workspace:
          at: /tmp
      - checkout
      - setup_remote_docker:
          reusable: true
      - run:
          name: Push rubygem to s3
          command: |
            .circleci/push_gem.sh
      - run:
          name: Build and push docker image to ecr
          command: |
            .circleci/build_and_push.sh

  release:
    docker:
      - image: circleci/ruby:2.6.1-node-browsers
    working_directory: ~/repo
    steps:
      - attach_workspace:
          at: /tmp
      - checkout
      - setup_remote_docker:
          reusable: true
      - run:
          name: Install dependencies
          command: |
            .circleci/install_dep.sh
      - run:
          name: Build and push docker image to dockerhub
          command: |
            .circleci/build_and_push_to_dockerhub.sh
      - run:
          name: Upload gem to Github
          command: |
            .circleci/build_and_push_to_github_release.sh
      - run:
          name: Upload gem to Ruby Gem
          command: |
            .circleci/push_gems_to_rubygems.sh

  check_version:
    docker:
      - image: circleci/ruby:2.6.1-node-browsers
    working_directory: ~/repo
    steps:
      - attach_workspace:
          at: /tmp
      - checkout          
      - setup_remote_docker:
          reusable: true
      - run:
          name: Check VERSION file for change
          command: |
            .circleci/check_version_trigger_release.sh

workflows:
  version: 2
  build_test_push:
    jobs:
      - build:
          filters:
            branches:
              ignore: /^release\/.*/
      - test:
          requires:
            - build
      - push:
          requires:
            - test
          filters:
            branches:
              only: develop
      - check_version:
          requires:
            - push
  release:
    jobs:
      - build:
          filters:
            branches:
              only: master
      - release:
          requires:
          - build