Learn Ruby on Rails Book

Setting up CircleCI

What is CircleCI

When we send pull request with code change we want to ensure that the changed code does not break our test. So we need to run all the tests of the application every single time we send a pull request or every single time we make a commit to the pull request.

Here, CircleCI comes into the picture. CircleCI runs those tests and provides feedback in our pull request.

CircleCI is a modern continuous integration and continuous delivery (CI/CD) platform. CircleCI automates build, test, and deployment of software.

CircleCI setup

To do CircleCI setup, we need to follow below steps:

Login at CircleCI

Visit CircleCI and login using your GitHub account.

Add project

Click the projects button from the left-hand side menu. Then select the project you want Circle-CI to test for you, and click the Setup Project button.

Then we would be navigated to a new page which would display a config file.

Build project

We should see Add config button click on it. After that click a new branch would be created in your repository as circleci-project-setup .

And it would add a generic config.yml file under the .circleci directory in root folder of the application .circleci/config.yml.

Updating config.yml file

Add the following code to .circleci/config.yml :

1version: 2.1
3  build:
4    #No. of servers/machines
5    parallelism: 1
7    docker:
8      - image: circleci/ruby:2.7.2-node-browsers
9      - image: circleci/postgres:9.6
10        environment:
11          POSTGRES_HOST_AUTH_METHOD: trust
13    #application directory change it with your application directory name
14    working_directory: ~/granite
15    environment:
16      TZ: "/usr/share/zoneinfo/America/New_York"
17      RAILS_ENV: "test"
18      RACK_ENV: "test"
20    steps:
21      - checkout
23      #install sqlite dependency
25      - run: sudo apt update && sudo apt install zlib1g-dev libsqlite3-dev
27      # Bundle install
29      - run:
30          bundle check --path=vendor/bundle || bundle install
31          --path=vendor/bundle --jobs=4 --retry=3
33      # Setup database
35      - run: cp config/database.yml.ci config/database.yml
36      - run: bundle exec rails db:create db:schema:load --trace
38      # Yarn install
40      - run:
41          chmod a+x ./bin/yarn && ./bin/yarn install --cache-folder
42          vendor/node_modules
44      # Generate assets
46      - run: chmod a+x ./bin/webpack && ./bin/webpack
47      - run: bundle exec rails webpacker:compile
49      # Unit tests
51      - run:
52          shell: /bin/bash
53          command: bundle exec rake db:test:prepare && bundle exec rails t -v
55  version: 2
56  commit:
57    jobs:
58      - build

Updating database.yml.ci

This config file expects a file at config/database.yml.ci . We don't have that file right now. Let's add that file.

Open terminal and execute following command.

1touch config/database.yml.ci

Now open config/database.yml.ci and add the following code:

1default: &default
2  adapter: sqlite3
3  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
4  timeout: 5000
7  <<: *default
8  database: db/development.sqlite3
10# Warning: The database defined as "test" will be erased and
11# re-generated from your development database when you run "rake".
12# Do not set this db to the same as development or production.
14  <<: *default
15  database: granite_test<%= ENV['CIRCLE_NODE_INDEX'] %>
18  <<: *default
19  database: db/production.sqlite3

Check in all the changes and push the changed code to github. Before proceeding further please ensure that these changes are pushed to github and these changes are there in the master branch of the repository.

Running a build through a pull request

Create a pull request and we will see that as soon as pull request is raised the CircleCI starts running the tests.

Common CircleCI errors and their solutions

Bundle check: Could not find 'bundler' (version)

To resolve this issue, make sure that the right version of bundler is used before building. Add the following line in your .circleci/config.yml file under steps - run: gem install bundler:version . Here version is the version with which your Gemfile.lock is bundled. It is at last line of your Gemfile.lock.

2  - checkout
3  - run: gem install bundler:2.1.4

Bundler: command not found: bundle-audit

This issue is because of two missing gems in your Gemfile. Add the following two lines in your Gemfile.

1gem 'bundler-audit', require: false
2gem 'ruby_audit', require: false

Run bundle update then bundle install.

Your Ruby version is X, but your Gemfile specified Y

To fix this issue, we need to keep the Ruby version consistent everywhere. Here are some solutions.

  1. Ensure that the Ruby version mentioned in ".circleci/config.yml" under jobs -> build -> docker -> image has the right Ruby version.
  2. Ensure that the Ruby version at the top of Gemfile has the right Ruby version.
  3. Ensure that the Ruby version in .ruby-version file has the same value.
  4. Ensure that the Ruby version file .ruby-version is present in Github. Some folks ignore this file.
    to navigateEnterto select Escto close