Install Gems Remotely When Using Git and Gitosis
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:
- Clone the Git repository.
- Change to the new directory.
- Build the gem.
- Install the gem.
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):
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.
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 email@example.com:mygem.git"
run "cd /home/git/mygem && sudo rake manifest && sudo rake build_gemspec && sudo rake install"
Hope it helps. If you have a simpler or more effective way, please post in the comments.
Posted: 06 November 2010