CI/CD of Ruby On Rails using Jenkins

Please visit my website and subscribe to my youtube channel for more articles

https://devops4solutions.com/

In this blog, we will setup a CI/CD of Ruby On Rails using Jenkins.

Prerequisite

  1. Java — Install Java from here
  2. Jenkins — Install Jenkins from here
  3. Git — sudo yum install git

Install Plugins in Jenkins

  1. Go to Manage Jenkins -> Manage Plugins -> RubyMetrics, Rake, Rvm
  2. Go To Manage Jenkins -> Configure System -> Search for Rake

Find the RVM Installation and Ruby Installation Path , run the below command:

which rvm
which ruby
RVM and Ruby Path

Please put the below values of these installations

RVM Path -> /usr/local/rvm/bin

Ruby Path -> /usr/local/rvm/rubies/ruby-2.5.1/

Image for post
Image for post
rvm home path

Setup Environment for jenkins user

  1. Change password for jenkins user
sudo passwd jenkins

2. Edit the below file

vi /etc/sudoers

3.Put the below content

jenkins ALL=NOPASSWD: ALL

4. Login as jenkins user

sudo -su jenkins

4. Run the below command

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDBcurl -sSL https://get.rvm.io | bash -s stable --ruby

Next, we need to add rvm to our shell profile — in this case, we’ll add the following to the end of our ~/.bashrc:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

Now re login to jenkins user and run the rvm command , it will work.

Configure Jenkins Job

  1. Create a freestyle Project -> Under SCM -> Select Git ->https://github.com/devops4solutions/CI_Jenkins_RubyonRails.git
Image for post
Image for post

2. Build Environment -> Select Run the build in a RVM-managed environment -> Value is . (dot)

Image for post
Image for post

3. Add build step -> Execute Shell

Make sure your Gemfile has an enrty for RspecJunitformatter like this

gem ‘rspec’, :require => false, :group => :testgem ‘rspec_junit_formatter’, :require => false, :group => :test

bundle install
RAILS_ENV=test bundle exec rake db:migrate
RAILS_ENV=test bundle exec rspec — format RspecJunitFormatter — out results.xml
Image for post
Image for post

4. Add Post Build Action ->Publish Junit Test Result Report

Image for post
Image for post

5. Build Results

Image for post
Image for post
Image for post
Image for post

6. Check Test Results

Image for post
Image for post
Unit Test Report

Finally congratulations!! You have successfully followed each and every step for setting up CI/CD of Ruby On Rails using Jenkins.

References:

  1. https://github.com/sj26/rspec_junit_formatter

Written by

Devops Automation Enginneer

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