Sunday, February 10, 2008

Capistrano for Web Site Deployment -- Non Rails

Capistrano is an amazing tool for deploying Rails applications - and like they say when you have a hammer everything looks like a nail...so when I was tasked with developing a way to deploy a simple marketing web site for my company I looked first to Capistrano. I wanted to make it so that non-developers could get set up correctly and then do a "cap deploy" to deploy the current state of the web site. The Capistrano documentation is not great, so I googled around for how I might be able to deploy a non Rails application and found this. That link will lead you to a great blog entry by Pat Nakijima that explains how to deploy a non Rails application with Capistrano. Pat's version of this even sets up the Apache conf file and restarts the apache server using apachectl. I pared down his version because our conf file was going to remain fairly static so I wasn't worried about updating it or restarting Apache for changes to take effect. Also, with the web server hosting many different virtual host I wouldn't want to get stuck if someone had changed any of the conf files and Apache didn't restart correctly...

So here is my minimal version of deploying a simple web site with Capistrano (basically Pat's code with Apache related stuff removed and a micro task that removes the Capfile and config/ directories from the release):

# modifications from standard capistrano deploy as taken from:
# http://devthatweb.com/view/deploy-any-project-using-capistrano-2
set :application, "name removed"
set :repository, "url to trunk of repo"
# 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, "/var/www/#{application}"
# If you aren't using Subversion to manage your source code, specify
# your SCM below:
# set :scm, :subversion
set :domain, 'our domain removed'
role :app, domain
role :web, domain
role :db, domain, :primary => true
namespace :deploy do
task :setup, :except => { :no_release => true } do
dirs = [deploy_to, releases_path, shared_path]
dirs += %w(system).map { |d| File.join(shared_path, d) }
run "umask 02 && mkdir -p #{dirs.join(' ')}"
end
# Also overwritten to remove Rails-specific code.
task :finalize_update, :except => { :no_release => true } do
run "chmod -R g+w #{release_path}" if fetch(:group_writable, true)
end
task :migrate do
end
task :migrations do
end
task :cold do
end
task :start do
end
task :stop do
end
task :restart do
end
end
desc "remove the Capfile and config"
task :after_update_code do
run "rm #{release_path}/Capfile"
run "rm -rf #{release_path}/config"
end
view raw gistfile1.rb hosted with ❤ by GitHub