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
:
Note that 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:
Now open config/database.yml.ci
and add the following code:
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.
CircleCI config for PostgreSQL
In the Granite project, we are using SQLite
in the development environment but
if we use PostgreSQL
then we need to install postgresql-client
in place of
libsqlite3-dev
in the CircleCI
container and the .circleci/config.yml
should be updated like this:
If the project uses PostgreSQL
in the development environment then we would
have to provide some extra configurations like host
and user
, and update the
config/database.yml.ci
file like this:
If we don't add the host
and user
in the database.yml.ci
file, then
CircleCI will fail with an error of role "circleci" does not exist
. As no role
is mentioned in the CircleCI's database config file that is database.yml.ci
,
the CircleCI will try to create the database with the circleci
role and will
fail with an error like this:
We don't have to make the above mentioned changes in our Granite app as we are
using SQLite
in the development env.
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 this, from our
terminal:
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:
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:
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:
- Ensure that the Ruby version mentioned in ".circleci/config.yml" under jobs
-> build -> docker -> image has the right Ruby version.
- Ensure that the Ruby version at the top of Gemfile has the right Ruby
version.
- Ensure that the Ruby version in
.ruby-version
file has the same value.
- Ensure that the Ruby version file
.ruby-version
is present in GitHub. Some
folks ignore this file.
Error connecting to redis
While setting CircleCI we can face an error mentioning "Error connecting to
Redis on localhost". It can happen if we are testing some services which require
the Redis server and we haven't added the Redis image to the CircleCI container.
As we had mentioned above, we should specify the docker image for redis
in the
.circleci/config.yml
like this:
Now, let's commit the changes: