Run Rubocop and RSpec on CircleCI and deploy to ECS

What to do in this article

--Automatic testing with Rubocop and RSpec on CircleCI --Building a CI / CD pipeline using CircleCI orbs

Do not handle

--CircleCI initial settings --How to install Rubocop and RSpec

Completed form of config.yml

I refer to the following official page https://circleci.com/docs/ja/2.0/configuration-reference/#requires

version: 2.1

orbs:
  aws-ecr: circleci/[email protected]
  aws-ecs: circleci/[email protected]

references:
  defaults: &defaults
    working_directory: ~/myqpp

  ruby-docker-image: &ruby-docker-image
    #steps Common primary container
    image: circleci/ruby:2.6.4-node-browsers
    #Environment variables of the primary container
    environment:
      RAILS_ENV: test
      MYSQL_HOST: 127.0.0.1
      MYSQL_USERNAME: "db_name"
      MYSQL_PASSWORD: ""
      MYSQL_PORT: 3306

  mysql-docker-image: &mysql-docker-image
    image: circleci/mysql:8.0.19
    command: [--default-authentication-plugin=mysql_native_password]
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: true
      MYSQL_ROOT_HOST: "%"

jobs:
  checkout_code:
    <<: *defaults
    docker:
      - *ruby-docker-image
      - *mysql-docker-image
    steps:
      - checkout
      - save_cache:
          key: v1-repo-{{ .Environment.CIRCLE_SHA1 }}
          paths:
            - ~/project

  test:
    <<: *defaults
    docker:
      - *ruby-docker-image
      - *mysql-docker-image
    steps:
      #Check out the source code to working dir
      - checkout
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "Gemfile.lock" }}
            - v1-dependencies-
      #ruby dependency
      - run: gem install bundler -v 2.1.4
      - run: bundle install --jobs=4 --retry=3 --path vendor/bundle
      - save_cache:
          key: vi-dependencies-{{ checksum "Gemfile.lock" }}
          paths:
            - ./vendor/bundle
      #DB wait/setup
      - run: mv -f ./config/database.yml.ci ./config/database.yml
      - run: dockerize -wait tcp://127.0.0.1:3306 -timeout 120s
      - run: bundle exec rails db:create db:schema:load --trace
      #Rubocop automation
      - run:
          name: start RuboCop
          command: bundle exec rubocop
      #RSpec automation
      - run:
          name: start RSpec
          command: |
            mkdir /tmp/test-reports
            TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
              circleci tests split --split-by=timings)"
            bundle exec rspec \
              --format progress \
              --format RspecJunitFormatter \
              --out /tmp/test-results/rspec.xml \
              --format progress \
              $TEST_FILES
      #Save reports and results
      - store_artifacts:
          path: /tmp/test-reports
          destination: tr1
      - store_test_results:
          path: /tmp/test-reports

workflows:
  version: 2.1
  test_code:
    jobs:
      - checkout_code
      - test:
          requires:
            - checkout_code

  #Deploy Rails using ECR and ECS
  build-and-deploy-rails:
    jobs:
      - aws-ecr/build-and-push-image:
          filters:
            branches:
              only: master
          account-url: AWS_ECR_ACCOUNT_URL
          region: AWS_REGION
          aws-access-key-id: AWS_ACCESS_KEY_ID
          aws-secret-access-key: AWS_SECRET_ACCESS_KEY
          extra-build-args: "--build-arg RAILS_MASTER_KEY=${RAILS_MASTER_KEY}"
          create-repo: true
          dockerfile: ./Dockerfile
          repo: "${AWS_RESOURCE_NAME_PREFIX_WEB}"
          tag: "${CIRCLE_SHA1}"
      - aws-ecs/deploy-service-update:
          requires:
            - aws-ecr/build-and-push-image
          family: "${ECS_TASK}"
          cluster-name: "${ECS_ARN}"
          service-name: "${ECS_SERVICE}"
          container-image-name-updates: "container=${AWS_RESOURCE_NAME_PREFIX_WEB},tag=${CIRCLE_SHA1}"

  #deploy nginx
  build-and-deploy-nginx:
    jobs:
      - aws-ecr/build-and-push-image:
          filters:
            branches:
              only: master
          account-url: AWS_ECR_ACCOUNT_URL
          region: AWS_REGION
          aws-access-key-id: AWS_ACCESS_KEY_ID
          aws-secret-access-key: AWS_SECRET_ACCESS_KEY
          create-repo: true
          dockerfile: ./nginx/Dockerfile
          repo: "${AWS_RESOURCE_NAME_PREFIX_NGINX}"
          tag: "${CIRCLE_SHA1}"
      - aws-ecs/deploy-service-update:
          requires:
            - aws-ecr/build-and-push-image
          family: "${ECS_TASK}"
          cluster-name: "${ECS_ARN}"
          service-name: "${ECS_SERVICE}"
          container-image-name-updates: "container=${AWS_RESOURCE_NAME_PREFIX_NGINX},tag=${CIRCLE_SHA1}"

Two execution processes are set in jobs, and parallel execution of two jobs is defined in workflow.

Automatic test

Run RSpec

The following official references are useful. https://circleci.com/docs/ja/2.0/language-ruby/

At this time, you need to be careful if you are using MySQL 8 or above. It seems that the authentication method has changed since 8.0, so add the following to handle it.

image: circleci/mysql:8.0.19
    command: [--default-authentication-plugin=mysql_native_password] #add to

Build & deploy using orbs

You can deploy to ECS by reading the official reference below https://circleci.com/docs/ja/2.0/ecs-ecr/

What you should be careful about here is to make sure that the environment variables set in CircleCI correspond firmly. You can build a CI / CD pipeline as long as the environment variables are set correctly.

Refactor config.yml

The parts that are used repeatedly are refactored to improve readability.

&(anchor)Defined in *(alias)Execute what is defined in

bonus

Install CircleCI CLI from the formula below. https://circleci.com/docs/ja/2.0/local-cli/

(Mac/For Linux)
curl -fLSs https://circle.ci/cli | bash

You can check if the configuration file is valid with this CircleCI CLI.

$ circleci config validate

Thank you for reading the article

CircleCI also has a Japanese document, how to write a configuration file, Furthermore, there is a thorough description of test execution settings such as RSpec.

I wrote this article while feeling the gratitude for the Japanese documentation.

reference

https://discuss.circleci.com/t/solved-mysql-8-0-without-mysql2-authentication-plugin-caching-sha2-password-cannot-be-loaded/25791

https://patorash.hatenablog.com/entry/2018/06/24/122210

https://circleci.com/docs/ja/2.0/configuration-reference/#requires

https://circleci.com/docs/ja/2.0/workflows/

Recommended Posts

Run Rubocop and RSpec on CircleCI and deploy to ECS
Run SystemSpec (RSpec) and Rubocop on CircleCI
Enable Docker build cache on GitHub Action and deploy to Amazon ECS
How to check CircleCI code and automatically deploy to Heroku
[Part 1] How to deploy Docker containers and static files with CircleCI + ECS + ECR + CloudFront
How to deploy on heroku
Until you run Quarkus and run docker image on Amazon ECS
How to run React and Rails on the same server
How to deploy jQuery on Rails
How to deploy Laravel on CentOS 7
How to install and use Composer on an ECS instance on Ubuntu 16.04
Steps to run docker on Mac
[Core ML] Convert Cycle GAN to Core ML and run it on iOS
How to deploy Bootstrap on Rails
How to run JavaFX on Docker
Deploy to EC2 with CircleCi + Capistrano
Notes on migrating from CircleCI 1.0 to 2.0
Install Ubuntu20.04 on RaspberryPi 4 and build Kubernetes to run the container
Deploy Rails on Docker to heroku
Install Webpacker and Yarn to run Rails
Run Embulk on Docker to convert files
Deploy to Heroku [Ruby on Rails] Beginner
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
Points to review when Rubocop is slow to run
How to deploy a container on AWS Lambda
Settings to bypass Docker Hub restrictions on CircleCI
Deploy to Heroku (for second and subsequent edits)
Deploy Rails to ECS Fargate with AWS Copilot
Compile and run Java on the command line
How to run NullpoMino 7.5.0 on Ubuntu 20.04.1 64bit version
I couldn't run Processing on jdk-9, so I reverted to jdk-1.8 and it worked on Eclipse OXYGEN
[AWS] What are ECR and ECS? About each difference and how to operate Docker on AWS.
[Part 2] Automatically test and deploy WEB services created with Rails + Nuxt + MySQL + Docker with ECS / ECR / CircleCI to make them terraform