Thursday, August 13, 2009

Rails dev pattern: collaborate on github, deploy to heroku

Heroku is an awesome no-fuss hosting service for rails applications (I think I've raved about it enough).

It works great for solo development. But what if you want a large team work on the app, while limiting production deployment privileges? Or if you want the application to run as an open source project?

Since git is core infrastructure for heroku, it actually makes setting up distributed source control trivial, like in the diagram:


Here's a simple pattern for setting up this way. It may fall into the special category of "the bleeding obvious" if you are an experienced git user. But many of us aren't;-)

First, I'm assuming you have a rails application in a local git repository to start with. Like this:
$ rails test
$ cd test
$ git init
$ git add .
$ git commit -m "initial check-in"

Next, you want to create a new, empty repository on github. Github will give you a clone URL for the new repo, like
"git@github.com:mygitname/test.git".

Now we can add the github repo as a new remote location, allowing us to push/pull from github. I'm going to name the destination "github":
$ git remote add github git@github.com:mygitname/test.git
$ git push github master
Enter passphrase for key '/home/myhome/Security/ssh/id_rsa':
Counting objects: 3, done.
Writing objects: 100% (3/3), 209 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:mygitname/test.git
* [new branch] master -> master

At this point, you are setup to work locally and also collaborate with other's via github. If you have a paid account on github, you can make this a private/secure collaboration, otherwise it will be open to all.

Next, we want to add the application to heroku. I'm assuming you are already registered on heroku and have the heroku gem setup. Creating the heroku app is a one-liner:
$ heroku create test
Created http://test.heroku.com/ | git@heroku.com:test.git
Git remote heroku added
$

You can see that this has added a new remote called "heroku", to which I can now push my app:
$ git push heroku master
Enter passphrase for key '/home/myhome/Security/ssh/id_rsa':
Counting objects: 29, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (17/17), 2.17 KiB, done.
Total 17 (delta 12), reused 0 (delta 0)

-----> Heroku receiving push
-----> Rails app detected
Compiled slug size is 208K
-----> Launching....... done
http://test.heroku.com deployed to Heroku

To git@heroku.com:test.git
4429990..4975a77 master -> master


So we are done! I can push/pull from the remote "github" to update the master source collection, and I can push/pull to the remote "heroku" to control what is deployed in production.

Sweet!

PS: Once you are comfortable with this, you might want to get a bit more sophisticated with branching between environments. Thomas Balthazar's "Deploying multiple environments on Heroku (while still hosting code on Github)" is a good post to help.

4 comments:

Anonymous said...

Very cool - Heroku rocks!!!! I wonder if they will have a Java version any time soon.

Paul said...

thanks anon.

Java version? I don't see any indication that the heroku guys themselves will go there.

But heroku have a great model for how to provide hosting in the cloud, and I wouldn't mind seeing other java-friendly shops going the same route - it's the kind of innovation they better be working on right now, or else risk eventually getting steam-rolled by Google App Engine.

sameera said...

Hi, Thank you for posting this.. But I'm having an issue with uploading my project. (I created a heroku free account)

I followed your user guide and when I'm give the following command

git push heroku master

it prompts me the following

Enter passphrase for key '/home/sameera/.ssh/id_rsa':

what should i do, coz I have no idea where to find this 'passphrase'

and help would be much appriciated

thanks in advance

cheers
sameera

Paul said...

hi @sameera,
That just sounds like you don;t have ssh setup for heroku properly yet. Checkout the guides at http://help.github.com/msysgit-key-setup/

I'd recommend you try a simple app as per the heroku guides first. Once you have that working, then can go to more complicated things.

Regards,
Paul