Capistrano for Ruby/Sinatra deployments onto server without source access

I put off using Capistrano as I thought it would take as much effort to learn as msbuild or ant, but after trying it I regret not doing it sooner. It’s really simple to use, has great source integration and offers a very reliable process for deployments using ssh onto unix boxes. It works well with other languages/frameworks, so you can also deploy your Java/Scala/php projects. The focus of this post is deploying a simple Ruby/Sinatra app from git onto a server which doesn’t have direct access to source (e.g. private git host).

To use you will need SSH access to your servers

First install Capistrano:

gem install capistrano

Prepare your project (in project dir, will generate default deployment files):

capify .

Ignore the Capfile created, it’s pretty standard and you shouldn’t have to touch it. Edit the config/deploy.rb file and clear down, this contains some standard Rails deployment stuff which you won’t need.

set :application, “My Sinatra Application”

set :scm, :git

set :repository,  “git@github.com:mygit/myproject.git”

set :deploy_via, :copy # makes capistrano clone and copy source locally rather than on server

set :deploy_to, “/var/www/mysite.com” # current release will be symlinked in ‘/var/www/mysite.com/current’

set :user, “deploy” # user used to ssh onto server and perform tasks

server “mysite.com”, :app, :web, :db, :primary => true # server to deploy to, must be able to “ssh deploy@mysite.com”

set :use_sudo, false # whether to use sudo on deploy actions

namespace :deploy do

  task :restart, :roles => :web do

    run “echo this is where the actions necessary to restart the application should go”

  end

  task :finalize_update, :roles => :web do

    # Overwrite default rails action and perform any steps before symlink

    run “echo this is the path to the release folder #{ current_release }”

  end

end

Run setup before deployment to create deployment folders:

cap deploy:setup

Check:

cap deploy:check

Deploy!

cap deploy

Now you should have your application copied over to the server under the :deploy_to ‘current’ folder. Old releases (default 5) will be stored in the ‘release’ folder. You can use the restart task to call any commands necessary to restart the application. You can do a lot more complex things, like setup dbs, stage multiple servers simultaneously, tag release in source etc.

Useful links:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s