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!
Goals:
- Create a rails app on your local machine
- Set up a private shared repository on BlueHost
- Set up SSH keys
- Set up Git on your rails app on your local machine and push code to remote server
- Install Capistrano and Configure your Rails project to use it (on Local Machine only)
Requirements:
- BlueHost account or other shared hosting account which provides you ssh access
- ssh access to your BlueHost. Ask BlueHost support for it
Create a rails app on your local machine:
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:
Set up a private shared repository on BlueHost:
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
Set up SSH keys
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
Set up Git on your rails app on your local machine and push code to remote server:
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
Install Capistrano (on Local Machine only)
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
Configure your Rails Project to use Capistrano
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
7 Comments to “Deploying a Rails App, with Git and Capistrano on BlueHost”
Leave a Reply


Great job! Glad you got it working!
Thanks for reporting your solution.
Finally I figured out.Please chek my deploy.rb file:
This is not for dreamhost thouth. Mine was for hostmonster. I knew from hostmonster customer support that they work in similar manner coz they are sister businesses.
set :application, ‘XXX’
set :user, ‘XXX’
set :domain, “XXX@XXX.com”
set :applicationdir, “/home/#{user}/rails/#{application}”
set :repository, “#{domain}:/home/#{user}/rails/#{application}”
#set :repository, “#{domain}/rails/#{application}”
set :local_repository, “.”
set :scm, :git
#below are two things that didn’t allow me to deploy so I corrected them.
default_run_options[:pty] = true
set :scm_command, “/home/#{user}/git/bin/git” #check this with $ which git command to find out where
ssh_options[:forward_agent] = true
set :branch, “master”
set :scm_verbose, true
set :local_scm_command, :default
# deployment config
set :deploy_to, applicationdir
set :deploy_via, :export # worked
# roles (servers)
role :app, domain
role :web, domain
role :db, domain, :primary => true
# additional config options
ssh_options[:keys] = %w(C:\Users\my_user\.ssh\id_rsa) # I was on windows sorry
set :chmod755, %w(app config db lib public vendor script tmp public/dispatch.cgi public/dispatch.fcgi public/dispatch.rb)
set :use_sudo, false
I can do cap deploy:setup perfectly. cap deploy:check passed successfully. cap deploy fails with “Host Key verification failed”. The failure is at. It asked me my public key(I implemented rsa with paraphase, and setup authorized_keys in the server too), once entered it then initialized empty Git repository and then failed.
And at last it says- failed: “sh -c ‘git clone user@domain.com/home/xxx/xxx/….”
Hope that gives you some insight. waiting for your reply.
Would you please let me know which step you were at before you got the “Host key verification failed”… error?
I followed your steps above but it failed with this error:
“Host key verification failed”.
“The remote end hung up unexpectedly”
I am trying this for the whole day but with no success. I don’t know why server could not verify the key..
I am desperately waiting for some kind of help.
Excellent, thanks for posting this! Last week I finally got my Rails app working on Bluehost (have been using Bluehost for a few years but with PHP only), and with your tutorials I got a private git repository set up there, and I have capistrano working to deploy to bluehost. If you are still having problems with capistrano, let me know.
i’m newbie in rails, git and capistrano, just share about my experience when deployment rails app using capistrano and git…first i think you have to check you publickey on the git, using this command ssh -v git@github.com, see what happens on your machine…
cheers,
hendri