Set up CircleCI for Rails project

Post by Lưu Đại at 02-02-2024

1. About CircleCI and CI

- "CI refers to the practice of automatically and frequently integrating code changes into a shared source code repository" by redhat. CI can be understood as a collection of commands that will be executed when an event trigger in source code. One of the famous tool is github action (it's free and github fully support it). 
- Usually CI used to run unit test, check if code convention is correct and check if there is deprecated gem or security issues. 
- For set up CI you need to build environment base on your source code in a separated server (for example: install gem, nodejs, run migrate command, run test, ...). If you use github action the config file will be stored on .circleci folder. 
- CircleCI provide a seperated server to run CI. It also possible to setup Continuous Delivery (CD) using CircleCI. 

2. Set up CircleCI for Rails project

- Requirements: 
  • A Rails project with unitest or at least a rubocop setting file to test if the job run successfully and docker. 
  • A CircleCI account, if you haven't had it yet you can create one in CircleCI page. 
- After account is created, CircleCI will ask you the location of the source code that need to be integrated with CircleCI.
image.png

Because I store my source code on Github so I choose Github and then choose the project I want to integrate Github with. After that CircleCI asked me to create a project in their app and I need to provide a ssh private key so CircleCI can access and create pull request to my source code. 
Then CircleCI generated a sample config file. I modified the file, the result is below: 
# This config was automatically generated from your source code
# Stacks detected: deps:node:.,deps:ruby:.,package_manager:yarn:
version: 2.1
orbs:
  ruby: circleci/ruby@2.0.1
jobs:
  test-ruby:
    # Install gems, run rails tests
    docker:
      - image: cimg/ruby:3.0.0-node
        environment:
          - RAILS_ENV=test
      - image: circleci/postgres:9.5-alpine
    steps:
      - checkout
      - ruby/install-deps
      - run:
          name: Wail for db
          command: dockerize -wait tcp://localhost:5432 -timeout 1m
      - run:
          name: prepare db
          command: bundle exec rails db:prepare
      - run:
          name: rails test
          command: bundle exec rspec

workflows:
  build-and-test:
    jobs:
      - test-ruby

Explain more about CircleCI config options: 
  • version: this is the CircleCI's version, currently the version is 2.1
  • orbs: this is a CircleCI template, it has all the common set up so user don't have to write it several times. 
  • jobs: this is the step will be execute when create commit, push code to github. CircleCI use docker to build environment that's the reason why it has docker on jobs option. I skip the redis config since I didn't write test for write / read redis service. 
  • steps: The commands will be executed on jobs. 
- The last step is move the environment variable to config in webapp CircleCI. You should not expose Rails credentials to CircleCI config file, the better way is manually import each variable to CircleCI page. 
image.png