Deploying a Rails App, with Git and Capistrano on BlueHost

Here I’ll make an attempt to describe how to create a rails app from scratch on your local machine, configure a private Git repository on BlueHost and use Capistrano to deploy the latest code from the private shared Git repository.

Almost all of the steps below work, except I’m still unable to deploy the code via Capistrano but you can see where I am now. If you have a solution to my problem, I’d appreciate your comments!

Thanks!

  1. Create a rails app on your local machine
  2. Set up a private shared repository on BlueHost
  3. Set up SSH keys
  4. Set up Git on your rails app on your local machine and push code to remote server
  5. Install Capistrano and Configure your Rails project to use it (on Local Machine only)
  • BlueHost account or other shared hosting account which provides you ssh access
  • ssh access to your BlueHost. Ask BlueHost support for it

Do the following your local machine. In this case, I use OS X 10.5

cd /Users/[your_username]/Development
rails sample_app
cd sample_app
script/generate scaffold Contact name:string email:string age:integer
rake db:migrate
script/server

Open http://localhost:3000/contacts/new in your browser and you should see the following:

New Contact Form

New Contact Form

Initially, Git is not installed on your BlueHost account. You have to install it. The following assumes that you already have Git installed on your BlueHost account:

  • ssh username@yourdomain.com
  • Edit your ~/.bashrc file and add the export line after the line with # User specific aliases and functions:
# User specific aliases and functions
export PATH=$PATH:$HOME/bin
cd ~/rails/
mkdir sample_app
git --bare init

Do the following so you don’t have to type in your password each time you want to push/pull code from the repository. Also, it will allow you to deploy your code, via Capistrano, to the repository without having to type in a password.

On your local machine,

if ~/.ssh/id_rsa.pub doesn’t already exists, type to generate your public key:

ssh-keygen -t rsa

Once your public key ~/.ssh/id_rsa.pub is generated, do the following to append your public key to your authorized_keys of your BlueHost account:

cat ~/.ssh/id_rsa.pub | ssh username@yourdomain.com "cat >> .ssh/authorized_keys"

To verify that the keys were set up properly, the following should not ask you for your password:

ssh username@yourdomain.com

The following assumes you already have Git on your local machine:

cd /Users/[your_username]/Development/sample_app
git init
git remote add origin ssh://username@yourdomain.com/~/rails/sample_app
touch .gitignore

Add the following contents to your ~/rails/sample_app/.gitignore file:
---------{snippet} --------
*.log
db/schema.rb
db/development.sqlite3
public/.DS_Store
public/images/.DS_Store
---------{end snippet}-----

git add .
git commit -m "Initial Commit"
git push origin master

Append the following to your .git/config file within the yourproject directory you just created

[branch "master"]
        remote = origin 
        merge = refs/heads/master

You only need to install Capistrano on your local machine only. First check to see if you already have Capistrano installed by typing:

git list capistrano

If you don’t already have it installed, type:

sudo gem install capistrano

If you already have it installed but want to update to the latest, type:

sudo gem update capistrano

To check which version of Capistrano you have installed, type:

cap --version

To install the Capistrano configuration files (on Local Machine only):

cd /Users/[your_username]/Development/sample_app
capify .
cap -T (to view Capistrano out-of-box tasks)

The above command will generate a Capfile and config/deploy.rb

Edit the config/deploy.rb file and make it look similar to this:

set :application, "sample_app"
set :user, "your_user_name"
set :domain, "#{user}@yourdomain.com"

set :repository,  "#{domain}:/home/#{user}/rails/#{application}"
set :local_repository, "."

default_run_options[:pty] = true
set :scm_command, "/home/#{user}/bin/git"
set :use_sudo, false

# If you have previously been relying upon the code to start, stop
# and restart your mongrel application, or if you rely on the database
# migration code, please uncomment the lines you require below

# If you aren't deploying to /u/apps/#{application} on the target
# servers (which is the default), you can specify the actual location
# via the :deploy_to variable:
set :deploy_to, "/home/#{user}/rails/#{application}"

# If you aren't using Subversion to manage your source code, specify
# your SCM below:
set :scm, :git
# see a full list by running "gem contents capistrano | grep 'scm/'"

role :web, domain
role :app, domain
role :db, domain, :primary => true

To Create Capistrano Files on BlueHost server which keep track of different versions, type the following commands on your Local Machine:

cap deploy:setup

However, I get a RuntimeError when trying to deploy via

my-macbook-2:sample_app user$ cap deploy
  * executing `deploy'
  * executing `deploy:update'
 ** transaction: start
  * executing `deploy:update_code'
    executing locally: "/home/user/bin/git ls-remote . HEAD"
/opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:31: command not found: /home/user/bin/git ls-remote . HEAD
*** [deploy:update_code] rolling back
  * executing "rm -rf /home/user/rails/sample_app/releases/20090818025122; true"
    servers: ["hostname.com"]
    [user@hostname.com] executing command
    command finished
/opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/recipes/deploy/scm/git.rb:231:in `query_revision': Unable to resolve revision for 'HEAD' on repository '.'. (RuntimeError)
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/recipes/deploy/scm/base.rb:35:in `send'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/recipes/deploy/scm/base.rb:35:in `method_missing'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/recipes/deploy/scm/base.rb:63:in `local'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/recipes/deploy/scm/base.rb:35:in `method_missing'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/recipes/deploy.rb:37:in `load'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/variables.rb:87:in `call'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/variables.rb:87:in `fetch'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/variables.rb:110:in `protect'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/variables.rb:110:in `synchronize'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/variables.rb:110:in `protect'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/variables.rb:78:in `fetch'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/variables.rb:95:in `[]'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/recipes/deploy/strategy/base.rb:73:in `revision'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/recipes/deploy/strategy/checkout.rb:14:in `command'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/recipes/deploy/strategy/remote.rb:15:in `deploy!'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/recipes/deploy.rb:204:in `load'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/execution.rb:139:in `instance_eval'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/execution.rb:139:in `invoke_task_directly_without_callbacks'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/callbacks.rb:27:in `invoke_task_directly'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/execution.rb:89:in `execute_task'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/namespaces.rb:186:in `send'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/namespaces.rb:186:in `method_missing'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/namespaces.rb:104:in `update_code'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/recipes/deploy.rb:185:in `load'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/execution.rb:56:in `transaction'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/namespaces.rb:186:in `send'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/namespaces.rb:186:in `method_missing'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/recipes/deploy.rb:184:in `load'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/execution.rb:139:in `instance_eval'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/execution.rb:139:in `invoke_task_directly_without_callbacks'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/callbacks.rb:27:in `invoke_task_directly'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/execution.rb:89:in `execute_task'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/namespaces.rb:186:in `send'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/namespaces.rb:186:in `method_missing'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/namespaces.rb:104:in `update'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/recipes/deploy.rb:153:in `load'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/execution.rb:139:in `instance_eval'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/execution.rb:139:in `invoke_task_directly_without_callbacks'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/callbacks.rb:27:in `invoke_task_directly'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/execution.rb:89:in `execute_task'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/configuration/execution.rb:101:in `find_and_execute_task'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/cli/execute.rb:45:in `execute_requested_actions_without_help'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/cli/execute.rb:44:in `each'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/cli/execute.rb:44:in `execute_requested_actions_without_help'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/cli/help.rb:19:in `execute_requested_actions'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/cli/execute.rb:33:in `execute!'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/lib/capistrano/cli/execute.rb:14:in `execute'
	from /opt/local/lib/ruby/gems/1.8/gems/capistrano-2.5.8/bin/cap:4
	from /opt/local/bin/cap:19:in `load'
	from /opt/local/bin/cap:19
VN:F [1.9.22_1171]
Rating: 4.0/5 (3 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)
Deploying a Rails App, with Git and Capistrano on BlueHost, 4.0 out of 5 based on 3 ratings
Facebook Twitter Email

7 Comments to “Deploying a Rails App, with Git and Capistrano on BlueHost”