Install Gems Remotely When Using Git and Gitosis

Capistrano, Git, Ruby, Ruby on Rails Add comments

To host your private gems you have several options. The cheapest and most scalable solution will likely be to run your own gitosis server. Then you build your gems as described in this railscast and push them up to your gitosis repo. But how to deploy and install these gems when the gem is updated?

Installing a gem from Git involves four steps:

  1. Clone the Git repository.
  2. Change to the new directory.
  3. Build the gem.
  4. Install the gem.

Git post-receive Hook

One way is to implement a Git post-receive hook. This hook is in the hooks directory of your repo, and it has to be edited on the server. You cannot edit a hook locally and push it to the remote server. So ssh into your remote server and create a hook named post-receive (with 755 permissions) to look as so (Update: because of a “Permission denied – /tmp/stdout.echoe” error rm has to be run with sudo and visudo permissions for the git user has to be extended to ALL):

#! /bin/sh
cd /home/git
unset GIT_DIR
sudo rm -rf mygem
git clone /home/git/repositories/mygem.git
cd mygem && sudo rake manifest && sudo rake build_gemspec && sudo rake install 

Be sure to have the following in your visudo on the remote server:

git ALL=NOPASSWD: ALL

That’s it. Very easy.

Of course you can now setup different remote servers and push to them as needed.

Capistrano

For rails you could use capistrano and run these task only when deploying a Rails project, by putting this into your deploy.rb fild:

namespace :gems do

desc “Install gem”
  task :install_mygem, :roles => :app do
    run “cd /home/git && sudo rm -rf mygem”
    run “git clone git@yourserver.com:mygem.git
    run “cd /home/git/mygem && sudo rake manifest && sudo rake build_gemspec && sudo rake install”
  end

end

Hope it helps. If you have a simpler or more effective way, please post in the comments.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Leave a Reply