[Rails] Setup CircleCI 2.0

January 03, 2021

Introduction

Automate your development process with continuous integration in our cloud or on your own infrastructure.

Details from CircleCI website.

With Boring Generator

Boring Generator installation guide.

> rails generate boring:ci:circleci:install [options]
OPTIONS:
  ruby-version:    Tell us the ruby version to which you use for the application.
                   Default to Ruby 2.7.1
  skip-node:       Skips the node configuration required for webpacker based Rails application.
  repository-name: Tell us the repository name to be used as database name on circleci.
                   Defaults to boring_generators

Without Boring Generator

You can refer to the CircleCI ruby language guide to setup CircleCI or stick with us and do it in step by step manner.

1. Create CirleCI configuration file.

In your repository, create the .circleci/config.yml file to configure CircleCI.

> mkdir .circleci/
> touch config.yml

2. Create workflow to run test-suite

Add the following configuration to the .circleci/config.yml. The following configuration setup CI with Ruby setup on the image based on the docker images specified in the config.yml. Here is a list of CircleCI ruby images.

version: 2.1

orbs:
  ruby: circleci/ruby@1.1.1
  node: circleci/node@2

jobs:
  test:
    parallelism: 1
    docker:
      - image: cimg/ruby:<ruby_version>-node # TODO: Update the ruby version.
    steps:
      - checkout
      - run:
          name: Run test
          command: bundle exec rails test # TODO: Change the command as per your requirement.

workflows:
  version: 2
  test:
    jobs:
      - test

3. Add database.yml.ci file

# config/database.yml.ci

default: &default
  adapter: postgresql
  host: 127.0.0.1
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 10 } %>

development:
  <<: *default
  database: "<repository_name>_development"

test:
  <<: *default
  user: postgres
  database: "<repository_name>_test"

4. Setup Database

Every Rails application needs database setup and when CI is configured the database setup is must. Add following code to setup PostgreSQL on CI. Other databases must work in similar fashion.

jobs:
  test:
    parallelism: 1
    docker:
      - image: cimg/ruby:<ruby_version>-node # TODO: Update the ruby version.
      - image: circleci/postgres:11.2
        environment:
          POSTGRES_USER: postgres
          POSTGRES_DB: <repository_name>_test # TODO: Update the database name to `repository_name`_test.
    environment:
      BUNDLE_JOBS: "3"
      BUNDLE_RETRY: "3"
      PGHOST: 127.0.0.1
      PGUSER: postgres
      RAILS_ENV: test
    steps:
      - checkout
      - run: cp config/database.yml.ci config/database.yml
      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:5432 -timeout 1m
      - run:
          name: Database setup
          command: bundle exec rails db:schema:load --trace
      - run:
          name: Run test
          command: bundle exec rails test # TODO: Change the command as per your requirement.

5. Install and cache Ruby and Node dependencies

Installing the Ruby and Node dependencies after code is checkout on the docker images is necessary but everytime running dependencies can slow down build process and take long time to complete CI.

Add following steps to the config.yml after - checkout

- checkout
- ruby/install-deps
- node/install-packages:
    pkg-manager: yarn
    cache-key: "yarn.lock"

6. After setup

The configuration file should looks like following after setup.

version: 2.1

orbs:
  ruby: circleci/ruby@1.1.1
  node: circleci/node@2

jobs:
  test:
    parallelism: 1
    docker:
      - image: cimg/ruby:<ruby_version>-node
      - image: circleci/postgres:11.2
        environment:
          POSTGRES_USER: postgres
          POSTGRES_DB: <repository_name>_test
    environment:
      BUNDLE_JOBS: "3"
      BUNDLE_RETRY: "3"
      PGHOST: 127.0.0.1
      PGUSER: postgres
      RAILS_ENV: test
    steps:
      - checkout
      - ruby/install-deps
      # Store bundle cache
      - node/install-packages:
          pkg-manager: yarn
          cache-key: "yarn.lock"
      - run: cp config/database.yml.ci config/database.yml
      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:5432 -timeout 1m
      - run:
          name: Database setup
          command: bundle exec rails db:schema:load --trace
      - run:
          name: Run test
          command: bundle exec rails test

workflows:
  version: 2
  test:
    jobs:
      - test

Happy Coding!!!