Automated testing is a commonly used practice. It saves you from boring routine and detects problems while you are developing your product. One of the tools which can help you with this is Jenkins, cross-platform, continuous integration and continuous delivery application with rich plugin ecosystem.

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

 

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

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

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

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

alt text

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.

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 ULR: http://jenkins.example.com:8080/pluginManager/. After plugins are installed Jenkins should be restarted.

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

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

Now we can add out new project to Jenkins. We are going to http://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 on config page for the item. In “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. alt text

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

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

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

alt text

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 for automated testing of our product.

 

Consulting & Advisory Digital Transformation Ruby on Rails Development

Latest Insights in Consulting & Advisory

The Rise of Kotlin – Moving Away from Java for Android Development

Kotlin is a programming language for the Java Virtual Machine that’s able to be used in any scenarios that currently…

Introducing our Sphere Heroes Program – Artem Korenev – Employee of the month

At Sphere, employee recognition is a key component of our corporate culture. We believe in celebrating the successes of our…

Write For Sphere

Are you a writer with tech expertise? Then we want to hear from you! Here are a few guidelines for…

View All Articles arrow

We are here to help:

checkmarkto become a customer checkmarkto become an investor checkmarkto send a media inquiry checkmarkto join our team checkmarkto simply say ‘hi’
Get in Touch