Simple Deploy Strategy for Capistrano 3

This is part of a series of articles on rewriting a LAMP deploy stack to use Capistrano 3. The default behaviour of Capistrano is ideal for deploying apps but it is opiniated about app structure and process, this post strips Capistrano to its core to create a simple single version deploy tool.

This is part of a series of articles on rewriting a LAMP deploy stack to use Capistrano 3.

The default behaviour of Capistrano is ideal for deploying apps but it is opiniated about app structure and process, it automatically deploys multiple releases using symlinks to switch between deployed versions.

This behaviour is ideal if you have control over your deploy environment, but if you just want to get started using Capistrano as a drop-in replacement for git / sftp deploys then this approach may help you get started more easily.

Working with a typical deploy strategy

Chances are you currently do something like the following:

  1. Run a git repo on Github (or similar) develop on branches, merge to master and deploy when ready
  2. Deploy normally via an ssh username / password and on the remote server run git pull

If the above sounds similar to your setup then capistrano/simpledeploy is an ideal way to automate your process, and you will be up and running very quickly.

Two files and Deploy

First you need a Gemfile all you need to do is copy the following, the Gemfile goes into the root of your project.

source 'https://rubygems.org'

gem 'capistrano', '~> 3.1'
gem 'capistrano-simpledeploy'

Then you need a Capfile. The Capfile tells Capistrano about your server and project configuration so it can be pushed to the server. It also gets put in the root of your project folder. Use the following as a template, just change the values specific to your server.

# Include the Capistrano Dependencies
require 'rubygems'
require 'bundler/setup'
require 'capistrano/setup'
require 'capistrano/simpledeploy'

# Modify these settings to connect to your server and Git repo
set :application,   "yourapp"
set :deploy_to,     "."
set :repo_url,      "git@github.com:<your repo details>"

### Default stage - this is required
set :stage,         "production"

task :production do
    set :branch,        "master"
    server 'yourserver.com', roles: %w{web},
        ssh_options: {
          user: 'yoursshuser',
          password: 'yoursecretpassword'
        }
end

Note that if you use ssh keys for authentication then you can just delete the line in the yourserver.com configiration part.

One final step, you need to run bundle from the command line in the root of your project. This will take care of installing all the dependencies.

To check everything is working try typing cap -T on the command line, if you see a list of tasks then everything is good.

Deploy

Now that you're all setup deploying is as simple as running cap live deploy from the root of your project.