August 30th, 2009

Howto Auto Deploy Rails Apps After Git Push

For my most recent project, I had to share a git repository with another Rails developer. He also had to make deploys on my server. As my server has many projects, I though about having another way than giving him access via SSH for cap deploy.

I remembered Heroku automatically deploys Rails apps after commits. So here’s how you can have the same functionality with pure Git.

Configuration of SSH And Users

I’m assuming the following configuration:

# Git repository:
/home/git/awesome-rails-app.git
# Deploy path:
/var/www/awesome-rails-app
# Git user: 
git
# Deploy user:
deploy
# Authentication with SSH keys

First, create a SSH key for the user git (unless you already did so) and add it for for the deploy user and vice versa:

# log in as git:
ssh-keygen -t rsa
# log in as deploy:
cat /home/git/.ssh/id_rsa.pub >> /home/deploy/authorized_keys
ssh-keygen -t rsa
# log in as git:
cat /home/deploy/.ssh/id_rsa.pub >> /home/git/authorized_keys

Installing the Git Hook

Git offers some hooks, where you can add your own code. The most interestion hook for having a deploy is “post-receive”. Just add the deploy command:

# File: /home/git/awesome-rails-app.git/hooks/post-receive
#!/bin/sh
 
cd /var/www/awesome-rails-app/current && cap deploy:migrations
 
## If Git isn’t on the same server as the application:
# ssh deploy@deploy-server.com \
#   'cd /var/www/awesome-rails-app/current && cap deploy:migrations'
## Don’t forget to add the SSH keys.

Make this file executable:

chmod +x /home/git/awesome-rails-app.git/hooks/post-receive

Finish

Now it should work:

# on your local mashine:
git push origin master

You’ll see all commands of the cap deploy:migrations process in your console.

This post was inspired by this post. After you finished the automatical deploy, there is some other interesting homework: auto test before commits, integration your bug tracker, …

4 Responses to “Howto Auto Deploy Rails Apps After Git Push”

  1. […] Howto Auto Deploy Rails Apps After Git Push – Nico Hagenburger (tags: git rails deployment) […]

  2. man, you rule
    short, simple and makes heroku cry and beg you to remove this post to keep its lazy silly users

  3. Hi there,
    looks like you will be able to get me out of my deployment problem.
    I have setup a git repo in my account at hostmonster hosting. I want to run cap deploy to deploy changes to the same hostmonster server. But it is giving me some public keys errors when in fact I had generated ssh key in my local machine and copied the id_rsa.pub to hostmonster .ssh directory and renamed it to authorized_keys. But when I run deploy, it gives me error like this: Error reading response length from authentication socket. Permission denied, please try again. Permission denied (publickey,password). The remote end hung up unexpectedly.
    I am not sure what is going on here.
    Help me please if you have any knowledge on this issue.

    Thanks

  4. @rup: are you using two different users for this? Did you also copy the id_rsa (without “.pub”) into the git user’s .ssh directory?

    Maybe you should try to login as your git user to your server and try to login as deploy user.

Leave a Reply

  1. (required)
  2. (required)
  3. XHTML: You can use these tags: <pre lang="" line="">