Search
⌘K
    to navigateEnterto select Escto close

    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.

    Note: Before setting up CircleCI make sure to push your code to a Github repo to see it listed in the CircleCI projects tab.

    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
    2jobs:
    3  build:
    4    #No. of servers/machines
    5    parallelism: 1
    6    docker:
    7      - image: circleci/ruby:3.0.2-node-browsers
    8      - image: circleci/postgres:9.6
    9        environment:
    10          POSTGRES_HOST_AUTH_METHOD: trust
    11
    12    #application directory change it with your application directory name
    13    working_directory: ~/granite
    14    environment:
    15      TZ: "/usr/share/zoneinfo/America/New_York"
    16      RAILS_ENV: "test"
    17      RACK_ENV: "test"
    18
    19    steps:
    20      - checkout
    21      - run: gem install bundler:2.2.20
    22      #install sqlite dependency
    23      - run: sudo apt update && sudo apt install zlib1g-dev libsqlite3-dev
    24      # Bundle install
    25      - run:
    26          bundle check --path=vendor/bundle || bundle install
    27          --path=vendor/bundle --jobs=4 --retry=3
    28      # Yarn install
    29      - run:
    30          chmod a+x ./bin/yarn && ./bin/yarn install --cache-folder
    31          vendor/node_modules
    32      # Ensure Rubocop has linted all Ruby files
    33      - run: bundle exec rubocop
    34      # Ensure Prettier+ESLint has been run on all JS files
    35      - run:
    36          npx prettier "./app/javascript/src/**/*.{js,jsx,json}" && npx eslint
    37          "./app/javascript/src/**/*.{js,jsx,json}"
    38      # Setup database
    39      - run: cp config/database.yml.ci config/database.yml
    40      - run: bundle exec rails db:create db:schema:load --trace
    41      # Generate assets
    42      - run: chmod a+x ./bin/webpack && ./bin/webpack
    43      - run: bundle exec rails webpacker:compile
    44      # Unit tests
    45      - run:
    46          shell: /bin/bash
    47          command: bundle exec rake db:test:prepare && bundle exec rails t -v
    48workflows:
    49  version: 2
    50  commit:
    51    jobs:
    52      - build

    Note that in order to ensure linting and formatting is strictly done using the configs mentioned in the linting and formatting chapter, we added relevant tasks in CircleCI too.

    If the CircleCI build fails, then you will get an email for the same. Or you can check the last commit in GitHub and confirm whether there's a green tick mark or not. Green tick mark means build has passed successfully.

    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
    5
    6development:
    7  <<: *default
    8  database: db/development.sqlite3
    9
    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.
    13test:
    14  <<: *default
    15  database: granite_test<%= ENV['CIRCLE_NODE_INDEX'] %>
    16
    17production:
    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

    Rubocop failing in CircleCI

    This is quite possible to happen in the first run with CircleCI.

    The reason is that when we create a Rails application in the first place, a lot of Ruby files will be added, but no linting nor formatting is applied to it.

    Thus what we can do is to run Rubocop on all relevant files, like so, from our terminal:

    1bundle exec rubocop -A

    Once done, verify all offenses are corrected. You check if anymore offenses are remaining by committing the already modified files using Git, since it will invoke our Git hooks.

    You might've to manually correct some Rubocop offenses in the pointed out files.

    Example: Rubocop uncorrectable offenses.

    Once everything is done, re-push to the GitHub branch with CircleCI tracks and things should be working.

    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:

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

    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.

    Now, let's commit the changes:

    1git add -A
    2git commit -m "Setup the CircleCI"
    Previous
    Next