Deploy your Ruby project with out the need to install gems.
I have been using the gem bundler lately with my ruby projects to deploy to team city build agents. My project needed to run on multiple OS X and Cent OS systems without having to manage ruby gems manually. The bundler gem repository is located on github at http://github.com/carlhuda/bundler. There are a few different ways to use it so I decided to write about how use it. The short answer is that I lock and pack the gems I need, and fall back to sudo installed gems when needed. I am using bundler 0.9.6. The prerequisite is that you gem install bundler.
Use a begin / rescue to try to require the environment file.
If the file is not found then execute the bundle install command
begin # Try to require the preresolved locked set of gems. require File.dirname(__FILE__) + "/../.bundle/environment" rescue Exception=>e # Fall back on doing an unlocked resolve at runtime. if (!system("bundle install")) puts $? end end require File.dirname(__FILE__) + "/../.bundle/environment"
On the first run the bundle install command will execute
This operation will create a .bundle directory in the root of your project that will contain an environment.rb file that will contain all of your gem configuration information.
All your files need to do, is include this environment file and they will have access to your bundled gems. If you run the bundle install command twice in a row it will not install again if your gems have already been installed.
Three step process to setup your bundler.
Create a Gemfile that will list what gems to install. Lock your gem file and then pack your gems.
Check out the information on github to see the format of the gemfile. You can use the bundle init command to create the Gemfile for you in the base of your project. Now run the bundle lock command. A Gemfile.lock file will be created you will check this in to your version control system. Next you need to pack the gems. Run the command bundle pack. You will see that a vendor/cache directory will be created. You will need to check in the vendor cache directory into your VCS. Now when your deploy your code will self execute the bundle install command making all of the gems you need available too your project.
Working with compiled gems
I really like using the Nokogri gem but I foud out that when I execute my project with in the Team City execution environment with a Cent OS build agent, that Nokogiri cannot build correctly because it cannot access the system files it needs. The work around is to list nokogiri in your Gemfile, but don’t check in the gem into the gem cache. Install it as the user that will be executing your scripts so it will be available to your execution environment. The bundler will resolve the dependency to your system installed gem.
That was a quick rundown of what you can get using the bundler and how to set it up. I will continue to post updates as I come across more challenging situations. For now I still have to touch every machine that needs odbc ruby configuration and nokogiri.