Learn Ruby on Rails Book

Setting up 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.

CircleCI runs those tests and provides feedback in our pull request.

CircleCI Setup

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
10    #application directory change it with your application directory name
11    working_directory: ~/granite
12    environment:
13      TZ: "/usr/share/zoneinfo/America/New_York"
14      RAILS_ENV: "test"
15      RACK_ENV: "test"
17    steps:
18      - checkout
20      #install sqlite dependency
22      - run: sudo apt update && sudo apt install zlib1g-dev libsqlite3-dev
24      # Bundle install
26      - run: bundle check --path=vendor/bundle || bundle install
28          --path=vendor/bundle --jobs=4 --retry=3
30      # Setup database
32      - run: cp config/database.yml.ci config/database.yml
33      - run: bundle exec rake db:create db:schema:load --trace
35      # Yarn install
37      - run: bin/yarn install --cache-folder vendor/node_modules
39      # Generate assets
41      - run: bin/webpack
42      - run: bundle exec rails webpacker:compile
44      # Unit tests
46      - run:
47          shell: /bin/bash
48          command: bundle exec rake test --trace
50  version: 2
51  commit:
52    jobs:
53      - 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 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 (build fails) 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.

3  + checkout
4  + 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 can keep the ruby version consistent in the project. Here are some solutions.

  1. Please make sure that the ruby version tag is used in the docker image.
1version: 2
3  build:
4    docker:
6      - image: circleci/ruby:2.7.2
  1. Update Gemfile with correct ruby version.
1ruby "2.7.2"
  1. CircleCI uses ruby-install to , so we can create a file .ruby-version in the project directory and add ruby version.
  1. If .ruby-version is not committed in the project.
2  name: Set Ruby Version
3  command:  echo "2.7.2" > ~/.ruby-version