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
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
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
ruby -v: Displays the ruby version used by the application
gem install bundler --no-ri --no-doc: Installs
bundlerfor packages without any documentation (as we don’t need docs while running CI)
bundle install --quiet: Installs the
gemsrequired 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.
Gemfile located at the root of the application
Add these lines to the
gem 'rubocop', require: false
gem 'brakeman', require: false
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
Your gitlab CI file should look like this after adding the flow for these gems.
The additional scripts will,
RSpectests each time you push to a specific branch
Brakemanscanner for the application to detect security vulnerabilities
Rubocopto 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
Build starts running…
Your build will pass or fail based on the latest commit.
Adding status badge
Add this line in your
README.md file to know the status of the build based on the latest commit.
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 !!!