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.
wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add - sudo sh -c 'echo deb http://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 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.
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.
After we have our keys we need to add our public key as deploy key to the github repo.
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 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
Then we need to add Jenkins(GitHub Plugin) service to our repo and set our url for webhooks there.
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.
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:
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 for automated testing of our product.