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, "www.example.com", "www2.example.com", "www3.example.com"

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

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

def previous_tag(component)
    logger.debug "querying previous #{component} tag..."
    tags_dir = "#{repository}/tags/#{component}/"
    ls(tags_dir).split("\n")[-2].chomp('/')
end

def latest_tag(component)
    logger.debug "querying latest #{component} tag..."
    tags_dir = "#{repository}/tags/#{component}/"
    ls(tags_dir).split("\n")[-1].chomp('/')
end

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"
end

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"
end
Advertisements

Blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: