Jenkins

CI/CD Setup Ruby On Rails using Jenkins

In this blog, we will  do the CI/CD Setup 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/

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 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -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

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

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

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

5. Build Results

6. Check Test Results

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
Please follow and like us: