.circleci/config.yml

Summary

Maintainability
Test Coverage
version: 2.1

defaults: &defaults
  parameters: &default_parameters
    restore_cache_key_1:
      type: string
      default: 'spree-bundle-v10-ruby-{{ checksum ".ruby-version" }}-{{ .Branch }}'
    restore_cache_key_2:
      type: string
      default: 'spree-bundle-v10-ruby-{{ checksum ".ruby-version" }}'
    save_cache_key:
      type: string
      default: 'spree-bundle-v10-ruby-{{ checksum ".ruby-version" }}-{{ .Branch }}'
    run_file_path:
      type: string
    rails_version:
      type: string
      default: '~> 7.1.0'
    store_artefacts:
      type: boolean
      default: false
  environment: &environment
      CIRCLE_TEST_REPORTS: /tmp/test-results
      CIRCLE_ARTIFACTS: /tmp/test-artifacts
      BUNDLE_JOBS: 4
      BUNDLE_RETRY: 3
      BUNDLE_PATH: ~/spree/vendor/bundle
      RAILS_VERSION: << parameters.rails_version >>
      DB: postgres # default value, this will speed up bundle install for all postgres builds
  working_directory: ~/spree
  docker:
    - image: &ruby_image cimg/ruby:3.3.0-browsers


jobs:
  run_test: &run_test
    <<: *defaults
    parallelism: 8
    steps: &default_steps
      - checkout
      - attach_workspace:
          at: /tmp
      - restore_cache:
          keys:
            - << parameters.restore_cache_key_1 >>
            - << parameters.restore_cache_key_2 >>
      - run:
          name: Allow executing given file
          command: chmod +x << parameters.run_file_path >>
      - run:
          name: Run << parameters.run_file_path >> file
          command: bash << parameters.run_file_path >>
      - when:
          condition: << parameters.save_cache_key >>
          steps:
              - save_cache:
                  key: << parameters.save_cache_key >>
                  paths:
                    - ~/spree/vendor/bundle
      - when:
          condition: <<  parameters.store_artefacts >>
          steps:
              - store_artifacts:
                  path: /tmp/test-artifacts
                  destination: test-artifacts
              - store_artifacts:
                  path: /tmp/failed_tests
                  destination: failed_tests
              - store_artifacts:
                  path: /tmp/test-results
                  destination: raw-test-output
              - store_test_results:
                  path: /tmp/test-results

  tests_postgres:
    <<: *run_test
    parameters:
        <<: *default_parameters
    environment:
      <<: *environment
      DB_HOST: localhost
      DB_USERNAME: postgres
    docker:
      - image: *ruby_image
      - image: cimg/postgres:16.2
        environment:
          POSTGRES_USER: postgres

  tests_mysql:
    <<: *run_test
    parameters:
        <<: *default_parameters
    environment:
      <<: *environment
      DB: mysql
      DB_HOST: 127.0.0.1
      DB_USERNAME: root
      COVERAGE: true
      COVERAGE_DIR: /tmp/workspace/simplecov
    docker:
      - image: *ruby_image
      - image: cimg/mysql:8.0
        command: [--default-authentication-plugin=mysql_native_password]

  send_test_coverage:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: /tmp/workspace
      - run:
          name: Merge coverage reports
          command:
              bundle check || bundle install &&
              bundle exec rake coverage:report
      - run:
          name: Allow executing given file
          command: chmod +x << parameters.run_file_path >>
      - run:
          name:  Send test coverage
          command: bash << parameters.run_file_path >>


workflows:
  main:
    jobs:
      - run_test:
          name: bundle_ruby
          run_file_path: ./bin/bundle_ruby.sh
      - run_test:
          name: brakeman
          restore_cache_key_1: spree-brakeman-{{ .Branch }}
          restore_cache_key_2: spree-brakeman
          save_cache_key: spree-brakeman-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
          run_file_path: ./bin/brakeman.sh
          requires:
              - bundle_ruby
      - tests_postgres:
          name: tests_rails_postgres
          store_artefacts: true
          run_file_path: ./bin/tests_database_ci.sh
          requires:
              - bundle_ruby
      - tests_postgres:
          name: tests_rails_7_0_postgres
          rails_version: '~> 7.0.0'
          store_artefacts: true
          run_file_path: ./bin/tests_database_ci.sh
          requires:
              - bundle_ruby
      - tests_postgres:
          name: tests_rails_6_1_postgres
          rails_version: '~> 6.1.0'
          store_artefacts: true
          run_file_path: ./bin/tests_database_ci.sh
          requires:
              - bundle_ruby
      - tests_mysql:
          name: tests_rails_mysql
          store_artefacts: true
          run_file_path: ./bin/tests_database_ci.sh
          post-steps:
              - persist_to_workspace:
                  root: /tmp/workspace
                  paths:
                    - simplecov
          requires:
              - bundle_ruby
      - send_test_coverage:
          name: send_test_coverage
          run_file_path: ./bin/tests_coverage.sh
          requires:
              - tests_rails_mysql