version: 2.1

orbs:

pushover: masutaka/pushover@dev:beta3

executors:

default:
  parameters:
    ruby_version:
      type: enum
      enum: ['2.4', '2.5', '2.6', '2.7']
  docker:
    - image: cimg/ruby:<< parameters.ruby_version >>
  working_directory: ~/repo

commands:

notify_on_failure_on_default_branch:
  steps:
    - pushover/notify:
        title: "Failed: ${CIRCLE_PROJECT_REPONAME}'s job (${CIRCLE_JOB})"
        message: "failed ${CIRCLE_USERNAME:Bot}'s build (#${CIRCLE_BUILD_NUM}) in ${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME} (${CIRCLE_BRANCH})"
        priority: 1 # high
        fail_only: true
        only_for_branches: master
setup_requirements:
  steps:
    - run:
        name: Install System Dependencies
        command: |
          # for cimg/ruby:2.4
          sudo apt update -q
          sudo apt install -q tzdata
    - run:
        name: Set timezone to Asia/Tokyo
        command: sudo cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
restore_bundle_cache:
  parameters:
    ruby_version:
      type: enum
      enum: ['2.4', '2.5', '2.6', '2.7']
  steps:
    - restore_cache:
        name: Restore bundle cache
        keys:
          - gems-ruby<< parameters.ruby_version >>-{{ .Environment.COMMON_CACHE_KEY }}-{{ checksum "Gemfile.lock" }}
          - gems-ruby<< parameters.ruby_version >>-{{ .Environment.COMMON_CACHE_KEY }}-
save_bundle_cache:
  parameters:
    ruby_version:
      type: enum
      enum: ['2.4', '2.5', '2.6', '2.7']
  steps:
    - save_cache:
        name: Save bundle cache
        key: gems-ruby<< parameters.ruby_version >>-{{ .Environment.COMMON_CACHE_KEY }}-{{ checksum "Gemfile.lock" }}
        paths:
          - ~/repo/vendor/bundle
install_bundler:
  steps:
    - run:
        name: install bundler
        command: gem install -N bundler:1.17.3 # same version as Gemfile.lock

jobs:

build:
  parameters:
    ruby_version:
      type: enum
      enum: ['2.4', '2.5', '2.6', '2.7']
  executor:
    name: default
    ruby_version: << parameters.ruby_version >>
  steps:
    - setup_requirements
    - checkout
    - restore_bundle_cache:
        ruby_version: << parameters.ruby_version >>
    - install_bundler
    - run:
        name: bundle install
        command: |
          bundle check || bundle install --jobs=4 --retry=3 --path=vendor/bundle
          bundle clean
    - save_bundle_cache:
        ruby_version: << parameters.ruby_version >>
    - run: bundle exec rubocop
    - run: bundle exec rspec
    - notify_on_failure_on_default_branch
continuous_bundle_update:
  executor:
    name: default
    ruby_version: '2.7'
  steps:
    - setup_requirements
    - checkout
    - restore_bundle_cache:
        ruby_version: '2.7'
    - install_bundler
    - run:
        name: Install edge circleci-bundle-update-pr
        command: |
          gem install -N specific_install
          gem specific_install https://github.com/masutaka/circleci-bundle-update-pr.git
    - deploy:
        name: Run circleci-bundle-update-pr
        command: circleci-bundle-update-pr CircleCI circleci@example.com $CIRCLE_BRANCH
    - notify_on_failure_on_default_branch

workflows:

version: 2
ordinary:
  jobs:
    - build:
        name: ruby-2.4
        ruby_version: '2.4'
    - build:
        name: ruby-2.5
        ruby_version: '2.5'
    - build:
        name: ruby-2.6
        ruby_version: '2.6'
    - build:
        name: ruby-2.7
        ruby_version: '2.7'
nightly:
  triggers:
    - schedule:
        cron: "00 10 * * 2"
        filters:
          branches:
            only: master
  jobs:
    - continuous_bundle_update