Deploying Projects With Capistrano

August 27, 2008 at 1:25 pm | Posted in General | Comments Off on Deploying Projects With Capistrano

Not too long ago at work, we added a few more servers to our production deployment to speed things up – however, this made deploying code changes a bit more complicated, as you had to remember to update several servers every time.  I decided to look into Capistrano, which I’d heard about before, and am glad to say it’s working very well, once I managed to figure it out.

The only problem with Capistrano is that it’s primarily used by rails devs, so all the examples I could ever find online were using it for rails, and mostly using the included ‘deploy’ script, which is very rails-centric with migrations and such. Since we’re using python and javascript in our projects, those things didn’t apply well.

After some digging I’ve managed to get a nice little deploy script that will update many servers to the latest tag in svn, and can also roll back to the next-latest tag, in case something goes wrong 🙂

role :example, "", "", ""

set :destination, "/home/example/sources"
set :repository, "svn+ssh://"

def ls(path)
    `svn ls #{path}`

def previous_tag(component)
    logger.debug "querying previous #{component} tag..."
    tags_dir = "#{repository}/tags/#{component}/"

def latest_tag(component)
    logger.debug "querying latest #{component} tag..."
    tags_dir = "#{repository}/tags/#{component}/"

desc "Deploy latest example tag"
task :deploy_example, :roles => :example do
    set :latest_rev, latest_tag("example")
    logger.debug "latest tag: #{latest_rev}"
    run "cd #{destination}/example ; svn switch #{repository}/tags/example/#{latest_rev}"
    run "sudo apachectl restart"

desc "Rollback example to previous tag"
task :rollback_example, :roles => :example do
    set :previous_rev, previous_tag("example")
    logger.debug "previous tag: #{previous_rev}"
    run "cd #{destination}/example ; svn switch #{repository}/tags/example/#{previous_rev}"
    run "sudo apachectl restart"

Blog at
Entries and comments feeds.

<span>%d</span> bloggers like this: