My Recipe For Capistrano/Git Rails Deployment on Webfaction
The joys of Capistrano have long eluded me. I was well aware of the easy life of instant deploys that it promised, but getting it up and running on my WebFaction account proved an insurmountable task that always saw me giving up in frustration, I didn’t even try getting things going with Vlad. Every time I created a WebFaction account I bravely attempted to get Capistrano running again, and every time I failed and gave up. Maybe the problem had to do with me not being willing to fork out the cash for a Github account – I was trying to deploy instead from my local Github repo. Or maybe it had to do with Capistrano’s vague/non-existent error messages; I had a feeling that the software was vague due to the fact that it was created and intended for an elite group of intelligent professionals, not troglodytic hackers like myself.
The first time I created a Hosting Rails account I found getting things going with Git and Capistrano were incredibly easy. And the benefits of Capistrano were immediately discernible. Pushing changes were suddenly a few words and a return key away instead of an exercise in annoyance. The writeup I followed to get Capistrano working on Hosting Rails was based on the idea of deploying from a Git repo on the WebFaction account itself.
It occurred to me that this could be the key to finally getting Capistrano and Git working for me on WebFaction…. 24 hours later, and after endless curses, hair pulling, fists banged on tables and whatnot, I finally got the darned thing playing nice, pretty much.
This is the solution that finally worked for me, based on a combination of resources:
- Ryan Kanno’s article on Deploying With Capistrano To Webfaction
- The Hosting Rails article Deploying With Git and Capistrano
- More Hosting Rails Info On Capistrano And Git
- The Webfaction Article On Doing Your Own Ruby Install
- This PDF on a personal Git Install on Webfaction
Preliminaries
- Create your WebFaction account, Rails app and database, add your WebFaction production database to config/database.yml.
- Create a personal Webfaction Ruby install.
At the point where you install the mysql gem using the instructions in this article, you will encounter an error. Run this instead: gem install mysql -- --with-mysql-config=/usr/bin/mysql_config
Step 1 - Get Git Going
- Go to the Git site and copy a link to the latest release, then log in to your WebFaction account using SSH and get it (adjust your actions according to the release):
wget http://kernel.org/pub/software/scm/git/git-1.6.5.6.tar.bz2
- Untar Git:
tar -xvjf git-1.6.5.6.tar.bz2
- Change into the Git directory:
cd git-1.6.5.6
- Configure it:
./configure --prefix=$HOME
- Make it:
make && make install
- Now test to make sure everything worked by calling the Git version:
git version
You’ll need to edit .bashprofile and .bashhrc in your Webfaction home folder now to make sure that the Git command line works:
.bash_profile
Get the aliases and functions
if [-f ~/.bashrc]; then . ~/.bashrc fi
User specific environment and startup programs
PATH=$PATH:$HOME/bin GIT=/home/me/git-1.6.5.6/
export PATH export PATH=$GIT:/home/me/ruby1.8/bin/:/home/me/ruby1.8/lib/ruby/gems/1.8/ bin/:$PATH
Create a zip archive of your app on your local machine.
Go to your home folder, then create a place for your repository on the server:
cd mkdir git
Back on your local machine, copy up your app:
scp /path/to/myapp.zip username@domain:~/git/
Back on the server, unzip the application, then initialize a Git repository:
cd git unzip myapp.zip cd myapp git init git add . git commit -a -m “Initial commit.”
Back on your local machine, clone the Git repo, then Capify it:
git clone me@domain:/home/me/git/myapp myapp cd /path/to/myapp capify .
Everything worked this far? Great! On to the next step: the Capistrano recipe…
Step 2. The Capistrano Recipe (myapp/config/deploy.rb)
Step 3. Prepare Your App For Capistrano
Log into your Webfaction account and create a directory called webapps-releases, then empty your webapp folder, save for the autostart, and relink :
cd ~/webapps/myapp
mv autostart.cgi ~/
rm -rf *
mv ~/autostart.cgi .
ln -s ~/webapps-releases/myapp/shared/log ~/webapps/myapp/log
Now you want to edit the last line of your autostart: os.system('/home/me/ruby1.8/lib/ruby/gems/1.8/bin/mongrel_rails start -c /home/me/webapps-releases/myapp/current -d -e production -P /home/me/webapps/myapp/log/mongrel.pid -p MY PORT NUMBER')
Step 4. Final Steps
Save the recipe and add it to your Git repo.
git add . git commit -a -m “Capified.” git push
Now your are ready to begin your deployment:
cap deploy:setup cap deploy:update
Future Deployments
If you’ve got this far without any problems, future deployments might be as easy as comitting your changes and deploying:
git add .
git commit -m 'my latest commit'
git push origin master
cap deploy
Notes
I have had problems with a few gems, namely Hpricot and RedCloth, perhaps because they include non-Ruby files in C and Java etc). I found that the only way to get around these problems was to NOT INCLUDE THEM in the config.rb gem dependencies in my application. Just install them on your WebFaction via gem install hpricot, gem install RedCloth. I think these gems are also causing problems with the standard deploy procedure…. If your app is not working after running cap deploy, you can try running
cap:deploy:restart
or
cap deploy:stop
cap:deploy:start
Solving Restart Problems After Cap Deploy
I have been having problems and found that the procedure that works for me is to modify the capistrano restart procedure to this:
Capistrano’s error messages are vague/non-existent as far as I can tell. I finally troubleshot my problems by modifying the capistrano gem itself to print out various parts of its process - eg the revision, the results, the command it was issuing, etc…. you might try doing this, if you’re having problems.
Capistrano Migrations
I have had trouble with Capistrano’s cap deploy:migrate feature, and solved it like this:
javan-whenever
I also had problem getting javan-whenever to update via Capistrano, and after much wrangling I finally started reading through Capistrano’s endless logging and discovered that sh was simply not finding whenever, so this worked for me:
I am available for Ruby on Rails consulting work – get in touch to learn more.