version: 2.1

executors:
  working_directory: /root/jpostcode-rb
  ruby_3_1:
    docker:
      - image: rubylang/ruby:3.1.0-focal
        auth:
          username: smarthrinc
          password: $DOCKER_HUB_ACCESS_TOKEN
  ruby_3_0:
    docker:
      - image: rubylang/ruby:3.0.2-focal
        auth:
          username: smarthrinc
          password: $DOCKER_HUB_ACCESS_TOKEN
  ruby_2_7:
    docker:
      - image: rubylang/ruby:2.7.4-bionic
        auth:
          username: smarthrinc
          password: $DOCKER_HUB_ACCESS_TOKEN
  ruby_2_6:
    docker:
      - image: rubylang/ruby:2.6.8-bionic
        auth:
          username: smarthrinc
          password: $DOCKER_HUB_ACCESS_TOKEN

commands:
  install_system_dependencies:
    description: "Install system dependencies"
    steps:
      - run:
          name: Install System Dependencies
          # https://circleci.com/docs/2.0/custom-images/#required-tools-for-primary-containers
          command: apt-get update -y && apt-get install -y ssh

  submodule_sync:
    description: "git submodule init & update"
    steps:
      - run:
          name: git submodule init & update
          command: |
            git submodule init
            git submodule update

  bundle_gems:
    description: "Bundle gems"
    steps:
      - run:
          name: Bundle gems
          command: |
            gem install bundler -v 2.2.31 --no-document --force
            bundle install
  run_tests:
    description: "Run tests"
    steps:
      - run:
          name: Run tests
          command: |
            bundle exec rake
            bundle exec rubocop

jobs:
  run_tests_on_ruby_3_1:
    executor: ruby_3_1
    steps:
      - install_system_dependencies
      - checkout
      - submodule_sync
      - bundle_gems
      - run_tests
  run_tests_on_ruby_3_0:
    executor: ruby_3_0
    steps:
      - install_system_dependencies
      - checkout
      - submodule_sync
      - bundle_gems
      - run_tests
  run_tests_on_ruby_2_7:
    executor: ruby_2_7
    steps:
      - install_system_dependencies
      - checkout
      - submodule_sync
      - bundle_gems
      - run_tests
  run_tests_on_ruby_2_6:
    executor: ruby_2_6
    steps:
      - install_system_dependencies
      - checkout
      - submodule_sync
      - bundle_gems
      - run_tests

workflows:
  version: 2
  test:
    jobs:
      - run_tests_on_ruby_3_1:
          context: smarthr-dockerhub
      - run_tests_on_ruby_3_0:
          context: smarthr-dockerhub
      - run_tests_on_ruby_2_7:
          context: smarthr-dockerhub
      - run_tests_on_ruby_2_6:
          context: smarthr-dockerhub