Setup Gitlab-CI for a Rails application

Though Rails development appears to be a piece of cake, there are some guidelines which we need to follow to make the code look the rails way. Many developers have their own way of writing code. Some may have fat models and skinny controllers, Some may have everything in the lib/ and make it independent from the application.

One way among the other to maintain code setup and quality is the CI Part. Continuous Integration comes handy in maintaining the code quality and also to verify the test written for your code in a proper way. It becomes more productive when we work with other developers in the same project and satisfies the need of every developer maintaining the same set of rules to complete the CI flow.

There are various continuous integration services available. Some of them are,

Let’s start with integrating Gitlab CI into a Rails Application.

Create a sample rails application

$ rails new sample_ci_app
$ cd sample_ci_app
$ bundle install

Make your application up and running

$ rails server

Now the application is available at http://localhost:3000

Setup version control

After creating the application, we should setup git in-order to maintain the code changes across various levels. Let’s start it.

$ git init
$ git status
$ git add -A
$ git commit -m "Initial Commit"
$ git remote add <your remote gitlab url>
$ git push origin master

Adding .gitlab-ci.yml

Now create a .gitlab-ci.yml file in the root of the application

By default the file should contain the above contents. Let’s look into each commands in the .yml file

  • ruby -v : Displays the ruby version used by the application
  • gem install bundler --no-ri --no-doc : Installs bundler for packages without any documentation (as we don’t need docs while running CI)
  • bundle install --quiet : Installs the gems required by the application without verbose
  • rake db:migrate : Migrates the database
  • rake db:test:prepare : Prepares the test DB for running test cases

So, the above file is the default file that you would have for an initial rails application.

Add code-quality gems

The best part of rails is that it has a huge variety of gems for each and every tasks to be performed. We can find the list of awesome gems here

In our application we will adding,

  • Rubocop (Code analyzer and style guide)
  • Brakeman (To scan application for vulnerabilities)
  • RSpec (For RSpec test cases)

Apart from these there are lot more integrations that can be added to the application as the complexity of the application grows by.

Open you Gemfile located at the root of the application

Add these lines to the Gemfile

gem 'rubocop', require: false
gem 'brakeman', require: false
gem 'rspec-rails'

After adding these lines, install the gems for the application

$ bundle install

Now the required gems have been configured for the application. The next step would be to integrate it with the CI.

Adding pipeline scripts

After adding the required gems, we will be adding their flow in the .gitlab-ci.yml file.

Your gitlab CI file should look like this after adding the flow for these gems.

The additional scripts will,

  • Run RSpec tests each time you push to a specific branch
  • Run Brakeman scanner for the application to detect security vulnerabilities
  • Run Rubocop to check code style and quality.

Now each time you push to a branch the build starts running and produce the results based on the work-flow provided in the .gitlab.yml file.

Build starts running…

Build Running in Gitlab

Your build will pass or fail based on the latest commit.

Build Status in Gitlab

Adding status badge

Add this line in your file to know the status of the build based on the latest commit.

![Build Status](<project_url>/badges/<branch_name>/build.svg)
Status Badge in

That’s it. We have created a sample rails application and integrated a minimal gitlab CI configuration and it works :-)

Start coding the Rails way !!!




DBL is a part of MobME Wireless Solution ltd which provides end-to-end support to banking and financial institutions with products and services to enhance customer experience with path-breaking inovations.

Recommended from Medium

Portal Solves International Payroll and Compliance for Distributed Teams

Guide for

2D Mobile: Designing Enemies Using Abstract Classes

Comparing and contrasting distributed and dedicated methods of doing research ops.

Getting Started With Java

Application Software Stocks In The Downturn

Committing changes in Unity project to Github

How to use Angel Broking Smart API Web Socket in Flutter || How to Decode Angel Broking Smart API…

What can you do with Azure Functions? Here’s one example.

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


Developer | Blogger | FOSS Activist

More from Medium

From A Developer perspective, lambda cold and warm start

From A Developer perspective, lambda cold and warm start

Rails User Session Handling using Redis

Accessing a PostgreSQL database from a Ruby on Rails project with TablePlus

Creating Rails Api Using Existing MySql Database