Capistrano Deployment Using Rsync
Jul 15th, 2006 by phil
Capistrano is a great tool for deploying Rails applications. However, by default Capistrano requires that the servers being deployed to have access to the version control repository to check out the code. While this may not seem like a big deal at first, there are situations where it is just not feasible.
I have run into two scenarios this year where I could not use the default Capistrano deployment model. In the first scenario there was a firewall between the production servers and the Subversion server. Corporate policy prevents servers in the DMZ from making connections to any machine on the internal network. In the second scenario, I have the Typo source code hosted in a local Darcs repository and I want to deploy to my web host.
It turns out that the default Capistrano behavior is easy to override. What I have done is to use Rsync to copy the code to the production server. Capistrano checks out the source code in the update_code task, so I overrode that task to call Rsync instead:
desc <<-DESC
Update all servers with the latest release of the source code.
Since production servers do not have access to SCM, we use
rsync to push to code to the servers.
DESC
task :update_code, :roles => [:app, :db, :web] do
on_rollback { delete release_path, :recursive => true }
username = user || ENV['USER']
current_task.servers.each do |server|
`rsync -avz -e ssh "./" "#{username}@#{server}:#{release_path}"
--exclude "_darcs" --exclude ".svn" --exclude "log"`
end
run <<-CMD
rm -rf #{release_path}/log &&
rm -rf #{release_path}/public/system &&
rm -rf #{release_path}/public/files &&
ln -nfs #{shared_path}/log #{release_path}/log &&
ln -nfs #{shared_path}/files #{release_path}/public/files &&
ln -nfs #{shared_path}/system #{release_path}/public/system
CMD
end
There are a few things to watch out for if you use this method. First, the contents of your working directory get copied to the server, not what is in the SCM. You need to make sure that your working copy is clean and up to date. Also, you should make sure that you don’t have uncommitted changes in your working copy as they will also be deployed to the server.




