Nerd For Tech
Published in

Nerd For Tech

Apply Continuous Integration for Ruby project by using CircleCI

Most software developers might have experienced this kind of situation: The more members involved in the software project, the more chance that errors appear when new code integrates into the project. To avoid this kind of problem, The concept of Continuous Integration(CI) comes out and it becomes popular to be applied to software project nowaday. What is Continuous Integration(CI)? According to the definition of Continuous Integration(CI) from Atlassian website, we could know it is a good practice for software projects to keep the new code’s correctness before integration into the project.

Continuous integration (CI) is the practice of automating the integration of code changes from multiple contributors into a single software project. It’s a primary DevOps best practice, allowing developers to frequently merge code changes into a central repository where builds and tests then run. Automated tools are used to assert the new code’s correctness before integration.

There are many tools to fulfill Continuous integration (CI) in software projects, for example, Jenkins, TeamCity, Bamboo, Buddy, and CircleCI... Every tool has its pros and cons. This article chooses CircleCI to be an example to show how to apply Continuous Integration to a Ruby project. Below is the outline of this article. There are 5 sessions to introduce the usage of CircleCI.

Getting started with CircleCI on your Github repo

To start using CircleCI, First of all, you should sign up with GitHub or BitBucket. In this article, we are using Github to signup. After signing up and logging in, you could see on the project page all your GitHub projects will list.

Before clicking the Set Up Project Button from the project you want to apply CircleCI, you could create a the folder at the root of your project, and create a file inside the folder. With this , after setting up the project with CircleCI, every time when there is a new change pushed to the Github repo, CircleCI will read the and execute the jobs defined in the

The content of you may use the example config share in CircleCI Configuration Introduction testing first. The example config is as below. We could see there is one job in the config, and which docker image used in the job environment was also defined, so when the project is set up with CicleCI, the environment for the job will be prepared every time the job runs. In the config, the command which is executed when the job runs is also defined. It will execute two commands: print and then print

After committing the and push it to Github, you could click on the Set Up Project Button, and there will be a pop-up window to let you choose the way to use your config file. We could just choose the fastest one and click set up the project.

After setting up the project, there is a pipeline shown in your dashboard. All the jobs defined in the will show. If there is a green tick shown beside the jobs, which means all the commands in the job are executed successfully.

If there is a red exclamation mark, it means the job fails.

We could click on the jobs to see more information. In our example, if we click on , there will show all the detailed results of this job. We could click on steps and there will be a step call which we define in the

Click on the you could see the two commands in this step execute. and print on the console, so if we want to implement another command, just update it in the steps of , commit it and push it to the Github repo.

If we need multi jobs run and some jobs should run after some jobs are done, the could be implemented in . Below is the example config in CircleCI document. In the example, there are three jobs defined: Hello-World, Fetch-Code, and Using-Node. In the part, the could let CircleCI know the job needs to execute only when which job is done. We could see Fetch-Code could execute only the first job Hello-World is done and Using-Node could execute only Fetch-Code is done.

After committing the and push it to Github, we could see in the CircleCI dashboard there is a pipeline, and there are three jobs which are the jobs we define in

Click on the success button, there is the detail of the workflow. It shows the jobs in the order just as we define in

In the job Fetch-Code, there are two steps defined. The first one is . It fetches the code from your Github repo when it executes, so that’s why step two could command to list all the contents of the Github repo.

Click on the Fetch-Code to see detail, we could see the two steps: Checkout code and Getting the Code.

In the Checkout code part, the code has been fetching from your git repo and it is on the branch of

In the Getting the code part, CircleCI execute and list all the files and folders in the first layer of your repo.

Every time we do a code change in our project and push it to the Github repo, CircleCI will run the jobs and execute all the commands in the steps which were defined in automatically. There is clear instruction on how to write to implement Continuous Integration in your software project in CircleCI document. People who are interested in learning more could see the CircleCI config introduction and take the example to get familiar with how CircleCI works with the Github repo.

Implement CircleCI in Ruby project

To implement CircleCI in Ruby, our aim is to run all the RSpec tests automatically every time when there is a code change and pushed to the Github repo, so CircleCI needs to execute run RSpec. The example Rails project used in this article could be found here. The ruby version is 2.7.1 and the database is using SQLite. Before running RSpec in CircleCI, the test environment should be prepared, so there will be two jobs defined in the . The first is for building the environment, the second one is for running the testing. We will use to define the order and requirements of the jobs. The example config is as below.

Because we will install ruby in two jobs, we implement to reuse ruby install steps which already defined in CircleCI, so that we could use to call all the commands in every job to install ruby in CircleCI.

More information about could be found in CircleCI document Orbs Introduction. Below is the definition of in CircleCI page.

Orbs are reusable snippets of code that help automate repeated processes, accelerate project setup, and make it easy to integrate with third-party tools.

Except prepare the , the also need to update, because CircleCI needs to execute . need to be added in of like below.

After the and , we could see the pipeline with two jobs in CircleCI dashboard after committing the change and pushing it to the Github repo.

Clicking on the job, we could see after Checkout code there is three-step: Restoring cache, Bundle install, Saving cache, and clicking on job, also could find these three jobs after Checkout code. These three steps are the steps that will run when executing

In the job detail page, we could see the RSpec is executed after Saving cache.

Clicking on the step and we could see all the RSpec tests have been executed. Because there are only two test cases in this project, only two test results show, but the result would be just like execute in your local environment. And every time there is now code change and push to the Github repo, will be triggered to check if any test case fails.

More config settings for Ruby project could be found in CircleCI document Language Guide: Ruby. There is more example to show how to implement CircleCI in Ruby project.

Setup Database in CircleCI

If the database in your project is not SQLite, it also could set up the database environment in CircleCI. Just need to define the docker image of the database and environment parameters in the . Below is the example config of using the PostgreSQL database. In the steps, after running , will continue to run the database setup command.

In the job running result of the example config, we could see the docker image of PostgreSQL database is using and the two steps to set up the database, Wait for DB and Database setup, have been executed.

Clicking on these two steps, we could see the log of table creation in the PostgreSQL database.

More database config examples could be found in CircleCI document Database Configuration Examples.

Apply reusable command with parameters in CircleCI config

If there is a need to run a command in different jobs or steps but have to pass different parameters, CircleCI supports us to define custom commands with parameters. We could print greeting words before running all the jobs, and part of the greeting words depend on what we pass to the parameter in the different jobs. The example config is as below. There is a greeting command defined with a parameter which has a default value , and in the job, the greeting command is in the steps with the value : . The greeting command is also shown in the job and the value is .

In the result of the example config, both in and jobs, the greeting command are all executed and they print out the greeting words with what we pass to the parameter

There are more details and examples about the reusable config in CircleCI document. That information could be found in CircleCI Reusable Config Reference Guide.

Debugging CircleCI error

If the jobs fail, we could click the jobs and see more information. Most of the time, the info would be clear enough for us to debug. Like the bundle install error below, it clearly shows the failure is caused by the wrong version of the gem.

But sometimes the error message might not be clear enough for us to know the root cause of failure, another way to debug is by using .

When clicking on the , CircleCI will prepare a remote virtual machine that is just the same as the environment of your failed job executed. We could see in the step Wait for SSH session that there is a command provided for us to access the machine.

By typing the command in your local console, you could successfully login to the machine.

Go to the project folder, and we could see the content is just the same as your Github project, then we could run the command in our failure job in CircleCI to see more information.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
icelandcheng

Programming Skill learner and Sharer | Ruby on Rails | Golang | Vue.js | Web Map API