2008年7月17日星期四

Deploy Ruby On Rails App on Heroku - A Simple Guide for Windows User

Heroku is an amazing online deploy/development environment for ruby on rails.

Ruby on rails is popular due to its practical methodology, framework and tools, rather than an extensive acceptance by web hostings(like php). If you've tried to deploy a ror application on one of most of popular web hostings (those promised to provide 1500+G space, 3000+G/mo file transfer), you may agree with me, because they always limit you so much. Of course you can buy a dedicated server, but how about just publish a toy project before pay anything? Heroku is a good choice.

As an online service, heroku did some wonderful things, for example, an irb, an ror editor, an rails console, a gem manager, and so on. The point is, they are all run in browser, even without any rich client support (I believe it will be practically used if is implemented in a rich client). You can check these features at http://heroku.com/features.

An important feature is about "work locally". For linux user, all the necessary tools are native. For Windows user, a few additional resource are required. Here's a simple guide for Windows user.

Heroku supports two ways to transfer source code between local and server,

* A simple way is "import/export", "import" means upload a compressed source code package in .tar.gz, it's easy on linux. For Windows user, a convenient approach is to use 7z. For example, your app folder is "myapp", compress it into "myapp.tar" using 7z, then compress "myapp.tar" into "myapp.tar.gz" using 7z. Upload "myapp.tar.7z" to an existed app on heroku, done! "export" means download a .tar.gz package from heroku, uncompress it, work locally.

* The other approach is more practical. using Git and a "heroku" gem for source control.

** Git is yet another source control tool, a substitute for SVN in ror world.

** GitHub is a Git repository, Rails itself and many other ror source are currently hosted there.

** "heroku" gem is a tool which helps to manage source code hosted in github, including source control and publish source code on heroku.

** Git is native in linux, for Windows user, there are two options: Cygwin and msysGit, the former is a comphensive linux simulator on Windows. The latter is an unofficial Git implementation on Windows.

** Download either of them (they are similar if you just need Git), and install. For Cygwin, remember to select git manually.

** msysGit provide a GUI interface for Windows user, Actually, I don't think it is better than its bash shell interface. So, even as a Windows user, you'd better to know a little about bash shell.

** Open msysGit bash shell from start menu, like this:

msysGit Bash
** Register an account at github.com, the "Free" one is ok, remember the email you used (e.g. name@example.com).

** Generate a ssh-key. In the opened msysGit bash shell, input following command:

ssh-keygen -C name@example.com -t rsa

In the following steps, it will ask to provide path and passphrase, use default by pressing enter (or change them if you know what you are doing).

** Get your SSH Public Key. Input following command in shell:

cat .ssh/id_rsa.pub

The SSH Public Key (lines of strange charactors) will display in the shell, copy them (how? just like what you did in a Windows cmd shell), and paste into Github account (go to https://github.com/account, login if necessary, there's a textbox titled with "SSH Public Key"), then click "Update Keys".

** The last thing is to get "heroku" gem: "gem install heroku"

** Everything is prepared, go to see heroku's demo, or follow the next simple steps to start to develop and deploy a ror app on heroku:

- Open msysGit shell, input "heroku list". which lists all your existed rails apps on heroku;

- Input "heroku create myapp", it helps to create skeleton of a ror app named "myapp" (I guess it run "rails myapp" for you on heroku server), you can access http://myapp.heroku.com directly (of course you should make sure "myapp" is not an existed app name on heroku), a ror welcome page is waiting for you.

- input "heroku clone myapp", make a clone of "myapp" for local development. First time, it will ask your email and password which you've used to register in Github. (BTW, in msysGit shell, change directory to the location you want to put your local app. It looks like "cd /d/apps/", which equals to "cd d:/apps" in windows cmd shell. Interesting to both linux or windows user:) )

- if everything ok, you could edit your rails app with your favorite editor or IDE. (I tried Netbeans and e-texteditor, both ok.)

- Make some changes to your local app, (for example, generate a scoffold, or modify routes mapping in config/routes.rb, these will take effect after you push them to heroku), run server and test it locally. If everything ok, check in (and publish) your local source code to heroku (Github) by following commands:

- 'git add .', adds the current content of new or modified files to the index, note you should in your rails app folder;

- 'git commit -m "comments about this checkin" ' , stores the current contents of the index in a new commit along with a log message from the user describing the changes;

- 'git push', updates remote refs using local refs, while sending objects necessary to complete the given refs;

- ok, now try to access your rails app on heroku by accessing http://myapp.heroku.com,all the local changes work. 'git push' does some great things: check in source code, publish updated source code on heroku, rake db:migrate (if there's migration), restart rails server (let the changes to routes.rb takes effect), maybe include other necessary work. Great!

- Git is similar to SVN,except above options, you can do checkout, diff, merge, mv, rm and so on. Check helps as you need.

- In fact, all the above command can be used in Windows cmd shell if you setup msysGit as need. And all these commands can be used in Cygwin either.

As a ruby on rails fan, you really should take a tour through heroku, there are so many interesting features except "work locally". After deployment environment getting done as above, you are ready to publish your toy project to the world.

没有评论: