The Complete Guide to Jenkins Installation for Ruby on Rails Using Ubuntu

04 Feb 2016

Automated testing is a commonly used practice. It saves you from boring routines and detects problems while you are developing your product. The Jenkins automation testing tool can help you with this is Jenkins, cross-platform, continuous integration, and continuous delivery applications with a rich plugin ecosystem.

To show you how to use the Jenkins for automation testing tool for Ruby on Rails, I’ve created a test rails app and put it on GitHub and created a server on Ubuntu, which I will refer to by the URL https://jenkins.example.com. The first thing we need to do is to install Jenkins and Git packages on our server.

wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins git

To check that Jenkins is working, we can go to our server using 8080 port https://jenkins.example.com:8080/ where we should find Jenkins’ Web UI.

Now we will prepare our server to work with our Rails application. We need to switch to Jenkins user and install rvm and ruby.

sudo su - jenkins
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
\curl -sSL https://get.rvm.io | bash -s stable --ruby

We also need to create ssh keys to add them as our deploy key to the repo, so Jenkins could access the application.

ssh-keygen

After we have our keys we need to add our public key as a deploy key to the Github repo.

cat ~/.ssh/id_rsa.pub

After that we can switch back and prepare .rvmrc and .bashrc files to load rvm automatically when Jenkins logs in, so it had access to ruby and gems.

echo "export rvm_trust_rvmrcs_flag=1" >> /tmp/.rvmrc
sudo mv /tmp/.rvmrc  /var/lib/jenkins/
chmod 755 /var/lib/jenkins/.rvmrc

echo '[ -s "/var/lib/jenkins/.rvm/scripts/rvm" ] && source /var/lib/jenkins/.rvm/scripts/rvm' >> /tmp/.bashrc
sudo mv /tmp/.bashrc /var/lib/jenkins/
chmod 755 /var/lib/jenkins/.bashrc

When it’s done we can go to Jenkins Web UI and configure our project there.

To work with Github we need to install Git and GitHub Plugins for our Jenkins server, we can do that via Jenkins Plugin Manager, which is available via URL: https://jenkins.example.com:8080/pluginManager/. After plugins are installed Jenkins should be restarted.

Scheduling Builds with Ruby and Jenkins

There are many ways to schedule builds in Jenkins, I will show how to launch a build after changes were pushed to our GitHub repo. Jenkins GitHub plugin provides a URL for GitHub hooks, by default it’s https://jenkins.example.com:8080/github-webhook/, but you can change it by going to https://jenkins.example.com:8080/configure and set custom URL in the GitHub Plugin Webhook section

Then we need to add Jenkins(GitHub Plugin) service to our repo and set our URL for webhooks there.

Now we can add our new project to Jenkins. We are going to https://jenkins.example.com:8080/ and add New Item, we need to specify its name and chose “Freestyle project”

After Jenkins’ item was created we will be redirected to the config page for the item. In the “Source Code Management section” we need to choose “Git”, set the correct URL to our git repository and set the name of the branch we want to monitor, you can leave the field empty if you want it to work for all branches.

Then we need to check “Build when a change is pushed to GitHub” in the “Build Triggers” section.

Now we need to specify the build step, things that will happen when we push changes to our repo. In the “Build” section we need to click on the “Add build step” and choose the “Execute shell” option.

This is our script which will prepare the enviroment and launch tests:

source ~/.bashrc
cd .

bundle install

RAILS_ENV=test bundle exec rake db:drop db:create db:migrate db:schema:load
bundle exec rake test

To check that it works we need to push any changes to your git repository and check Jenkins with its Web UI.

And it’s done! We installed and configured Jenkins CI, which is for automated testing of our product.