The Ruby Version Manager (RVM) is a great tool if your Ruby and Rails applications are based on different versions of Ruby and/or use different gem sets.
To integrate RVM with the Continuous Integration tool Jenkins, you can either follow this description on the RVM website, or use this Jenkins RVM Plugin. Both solutions have some drawbacks, so we were looking for a new solution.
The description on the RVM website suggests to run the build steps as shell scripts and specify the desired version in every script, which seems to be unnecessary and error prone.
The Jenkins RVM Plugin claims to run the whole build process in the RVM environment specified for the project as a whole. Thats true, but we make heavy use of Jenkins tasks, e. g. for deployment scripts.
Unfortunately, the RVM environment is not available inside the task scripts. To fix this, you can do something like (i captured this code from the console log of builds with the RVM plugin):
bash -c "source ~/.rvm/scripts/rvm && rvm_install_on_use_flag=1 && rvm use --create 1.9.3@projectname && export > rvm.env"
Again, you have to specify the Ruby version in every task.
But we would like to have an easy RVM project workflow, specifying the project specific ruby setting only once to stay DRY.
How it works
The comment section of this blog post led me to a solution which seems to work very well for our purposes without any major drawbacks.
Set up RVM for user jenkins (or whatever user the jenkins process runs), especially add the rvm setup to the user profile
Set up your projects with project specific RVM files (
.ruby-gemset if you use a current version of RVM)
Use shell scripts for all ruby or rake based build steps and add this code to all scripts and tasks:
The trick is to add the
-l option to the shebang, since the RVM setup code only executes if the shell is a login shell.
Now it is sufficient to keep
.ruby-gemset up to date, since the
cd into the workspace directory sets up the correct RVM environment.
A complete example of a rails build script would be
rvm info # print info about rvm (for debugging)
bundle exec rake db:migrate
bundle exec rake test