Setup – Jenkins and RVM
Overview
In this article I’m going to go through the process of using Ruby Version Manager (RVM) with my Build Server (Jenkins). This article will show how to set RVM and Jenkins up so I can run my Continuous Integration environment under the correct Rubies for my projects and with the correct Gems I need for the project and my tests to run successfully. I’m not going to go into too much specifics where it concerns the setup of Jenkins and what RVM is. For more information around these you can got to the “Setup – Apache and Hudson” article and the “Setup – Ruby and RVM” articles and for some more detailed views on how to use RVM you can go to my article called “Using – Ruby Version Manager”. In this article I will assume that you have Jenkins set up on an Ubuntu machine. I will be covering the following:
- Pre-requisites
- Installing Ruby Version Manager (RVM)
- Install Ruby
- Generate SSH Key
- Configuring Git
- Cloning your first GitHub Project
- Creating a Jenkins build with GitHub
- Conclusion
Pre-requisites
For the re-requisites, please refer to my “Setup – Requirements” article.
Installing Ruby Version Manager (RVM)
Before you try and install RVM you should ensure that your user “jenkins” has the correct privileges. In my case I simply added my user to the “admin” group by running the following command:
sudo adduser jenkins admin
When you install RVM you’ll want to install it under the Jenkins user account on your Build Server. You can do this by running the Switch User command as follows:
sudo su – jenkins
If you don’t know what your Jenkins user’s password is you can run the following command:
sudo passwd jenkins
When it prompts you for the password you should type one that you can remember. Once you’re running under the jenkins user account you can install RVM by running the following command:
bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
Once you’ve installed RVM you should do some editing of some files on your system. I found that if you run the default install, then the “~/.bashrc” and the “~/.profile” files don’t exist. If they exist on your machine you can follow the steps as outlined in my “Setup – Ruby & RVM” article to configure these two files. If it doesn’t exist then you can follow the following steps. I’ve uploaded the files to a GitHub repository and in theory you should only have to download them and copy them into the Jenkins home folder. I would suggest creating a “Downloads” folder in the Jenkins home folder and switching to it by running the following commands:
mkdir ~/Downloads
cd ~/Downloads
Now that you have a space where you could download the files you can run the following to create a Read-Only git clone of my online Repository:
git clone git://github.com/Yakiloo/UbuntuFiles.git
This should create a folder in your “~/Downloads” folder called “UbuntuFiles”. Once you have them here you can view them to ensure that I don’t have some funky stuff in there that you don’t want and once you’re ready you can copy both the “bashrc” and “profile” files into your Jenkins user’s home folder. To do this you can run the following commands which will copy them whiles renaming them to the correct name:
cp bashrc ~/.bashrc
cp profile ~/.profile
Once saved you should run the following command to refresh your environment with the settings you in the “~/.bashrc” file:
source ~/.bashrc
You should also run the following command to refresh your environment with the settings you added to the “~/.profile” file:
source ~/.profile
Install Ruby
In my case I’m going to install Ruby version 1.9.2 patch 0. You can do this easily by running the following command:
rvm install ruby-1.9.2-p0
You may follow the instruction found on my “Setup – Ruby and RVM” page if you have any additional things you’d like to install. As long as you install it under the “jenkins” user account and you use RVM to install them you should be fine. Ons last thing to mention here is that it’s always a good idea to setup a default RVM environment. You can and should do this by running the following command:
rvm use 1.9.2-p0 –default
Having this simple setting will save you hours of searching and not understanding why things don’t work and why Jenkins refuses to read your Ruby environment.
Generate SSH Key
To have your Jenkins server authenticate itself against your online Git repository, you’ll need to generate an ssh key. To do this you should go to the “~/.ssh” folder on your machine. Once there you can create a ssh key by running the following command:
ssh-keygen -t rsa -C “username@email.com”
If you follow the steps that follow you should have a new file called “id_rsa” and another file called “id_rsa.pub” in the “.ssh” folder. Now you can register this with your online Git provider for the specified User Name. To see how to do this when using GitHub you can follow the instructions I have on my “Getting Started – GitHub” article
Configuring Git
If you’re using GitHub like I am, then you’ll have to configure Git on your Build Server. To do this you can run the following command to configure your global Git user name:
git config –global user.name “YOURUSERNAME”
Once that is done you should you should also configure your global user’s email by running the following command:
git config –global user.email “YOUREMAILADDRESS”
Cloning your first GitHub Project
This is very simple to do. All you have to do is run the following command for your repository:
git clone git@github.com:GITHUBUSERNAME/YOURREPOSITORYNAME.git
You should be able to just add the URI above from your GitHub Repository by going to GitHub, logging in and browsing to the specific Repository that you would like to clone. You should see something like the following when you’re on the Repository’s dashboard:
You only have to add the “git clone” command to the front of it and that should do it. You should now be able to do a manual test to see if your stuff works. In the next section I’ll take you through getting Jenkins up and running to pull your GitHub repository and run some tests.
Creating a Jenkins build with GitHub
So, now that you have RVM installed under you Jenkins user account and you’ve made sure that Ruby works under the RVM, you’ll want to create your first Jenkins Job, which will connect to your online GitHub Repository, download your source code and run your tests. Before you do this I would suggest that you install the Bundler gem as follows:
gem install bundler
For this example I’ve created a “HelloWorld-GitHub” sample project that has the following files and folders in it:
bin/app.rb
lib/server/services/say.rb
spec/say_spec.rb
spec/ spec_helper.rb
config.ru
rakefile
README
Gemfile
Gemfile.lock
I would recommend that you reboot your Build Server at this stage. I’m not sure whether it’s necessary, but I would rather do that than to try and figure out why something is broken 🙂 Call me cautious if you want… You can do this very easily by just running the following command:
sudo reboot
Aren’t Ubuntu and Linux just great for the amount of Terminal support it gives you? Anyway, to move onto the discussion, once your machine is rebooted you should go to your Jenkins UI. When you’re on the Dashboard you should see your options as below:
You should select the “Manage Jenkins” option. When you should see something like:
On this page you should select the Manage Plugins option. This should take you to a page like this one:
On this page you should select the “Available” tab and do a search for the following plugins:
Git Plugin
GitHub Plugin
Select these two plugins and select install at the bottom of the page. If you go to the “Installed” tab you should see them in the list. Now that you have the necessary plugins for Jenkins – Git Integration you should select the “Back to Dashboard” option. On this page you should select the “New Job” option which should take you to a page that looks like this one:
You should now enter your Job’s name and select the “Build a free-style software project” option. When you click on OK, you should be taken to the “Configure” page. On this page you’ll be setting up your project to do the pull from GitHub and run the necessary tests. To start you should choose the Git option under Source Code Management and enter your online Repository’s URL and specify that it should pull all the branches as follows:
Once you’re done here you should configure your Build Triggers. In my this example I’ll be using the “Build Periodically” option with a Schedule to run every 10 minutes as follows:
And the final step is to configure your Build script to run. In my case I’m selecting the “Execute shell” option and configuring it to run a Bundle Install and a rake as follows:
Once this is done you can save your configuration and run it. Hopefully it will work for you as well 🙂
Conclusion
This concludes this walk through the world of getting RVM and Jenkins to play nice with each other. I hope it will help someone out there, because when I was struggling to get this up and running I found very scattered information which eventually led to finding the ultimate solution.
If you are testing more than one project on your Jenkins instance, you want to be able to use rvm in your build processes to specify separate rubies/gemsets for your different projects.
I found these links helpful to get rvm working *inside* the build process:
https://rvm.beginrescueend.com/integration/hudson/
http://blog.8thlight.com/articles/2011/3/2/jenkins-rvm-and-selenium
(basically, you just need to add something like this at the beginning of your build step:
#!/bin/bash -x
source ~/.bashrc
rvm [ruby]@[gemset]
)
this didn’t work for me, i had to use something like this
rvm use 1.9.2@rentini && rvm-shell -c “bundle install && RAILS_ENV=test && rake –trace db:drop db:migrate && cucumber features”
#!/bin/bash -e
source “$HOME/.rvm/scripts/rvm”
[[ -s “.rvmrc” ]] && source .rvmrc
export RAILS_ENV=test
bundle install
rake db:migrate default
It worked flawlessly. Tks very much.
For me, it’s
#!/bin/bash -l — make bash use a login shell so the RVM function can function 😉
source ~/.bashrc #to init the rvm path
rvm use 1.9.3
rake db:migrate
rake
For me, it’s
#!/bin/bash -l — need a login shell for rvm function to function 😉
source ~/.bashrc — load rvm
rvm use 1.9.3
rake db:migrate
rake
I appreciate, lead to I discovered just what I used to be having a look for.
You’ve ended my four day lengthy hunt! God Bless you man. Have a nice day. Bye