# Use the latest 2.1 version of CircleCI pipeline process engine. See: https://circleci.com/docs/2.0/configuration-reference
version: 2.1
# Use a package of configuration called an orb.
orbs:
  ruby: circleci/ruby@1.0

jobs:
  build: # our first job, named "build"
    docker:
      - image: cimg/ruby:2.7 # use a tailored CircleCI docker image.
    steps:
      - checkout # pull down our git code.
      - ruby/install-deps # use the ruby orb to install dependencies
  test:  # our next job, called "test"
    # here we set TWO docker images.
    docker:
      - image: cimg/ruby:2.7 # this is our primary docker image, where step commands run.
        auth:
          username: mydockerhub-user
          password: $DOCKERHUB_PASSWORD  # context / project UI env-var reference
    # A series of steps to run, some are similar to those in "build".
    steps:
      - checkout 
      - ruby/install-deps 
      # Run rspec in parallel
      - ruby/rspec-test
      - run:
          name: Run unit tests using Rails
          command: |
            RAILS_ENV=test bundle exec rake
      - store_test_results:
          path: test/reports

# We use workflows to orchestrate the jobs that we declared above.
workflows:
  version: 2
  build_and_test:     # The name of our workflow is "build_and_test"
    jobs:             # The list of jobs we run as part of this workflow.
      - build         # Run build first.
      - test:         # Then run test,
          requires:   # Test requires that build passes for it to run.
            - build   # Finally, run the build job.
