Home

Awesome

Capistrano Unicorn

Capistrano plugin that integrates Unicorn tasks into capistrano deployment script.

Developers: Please consider contributing your forked changes, or opening an issue if there is no existing relevant one. There are a lot of forks--we'd love to reabsorb some of the issues/solutions the community has encountered.

Build Status Gem Version Code Climate

Usage

If you are upgrading from a previous version, please see the NEWS file.

Setup

Add the library to your Gemfile:

group :development do
  gem 'capistrano-unicorn', :require => false
end

And load it into your deployment script config/deploy.rb:

require 'capistrano-unicorn'

Add unicorn restart task hook:

after 'deploy:restart', 'unicorn:reload'    # app IS NOT preloaded
after 'deploy:restart', 'unicorn:restart'   # app preloaded
after 'deploy:restart', 'unicorn:duplicate' # before_fork hook implemented (zero downtime deployments)

Create a new configuration file config/unicorn.rb or config/unicorn/STAGE.rb, where stage is your deployment environment.

Example config - examples/rails3.rb. Please refer to Unicorn documentation for more examples and configuration options.

Deploy

First, make sure you're running the latest release:

cap deploy

Then you can test each individual task:

cap unicorn:start
cap unicorn:stop
cap unicorn:reload

Configuration

You can modify any of the following Capistrano variables in your deploy.rb config. You can use the unicorn:show_vars task to check that the values you have specified are set correctly.

Environment parameters

Execution parameters

Relative path parameters

Absolute path parameters

Zero Downtime Deployment Options

Available Tasks

To get a list of all capistrano tasks, run cap -T:

cap unicorn:add_worker                # Add a new worker
cap unicorn:remove_worker             # Remove amount of workers
cap unicorn:reload                    # Reload Unicorn
cap unicorn:restart                   # Restart Unicorn
cap unicorn:show_vars                 # Debug Unicorn variables
cap unicorn:shutdown                  # Immediately shutdown Unicorn
cap unicorn:start                     # Start Unicorn master process
cap unicorn:stop                      # Stop Unicorn

Tests

To execute test suite run:

bundle exec rake test

Multistage

The issue here is that capistrano loads default configuration and then executes your staging task and overrides previously defined variables. The default environment before executing your stage task is set to :production, so it will use a wrong environment unless you take steps to ensure that :rails_env and :unicorn_env are set correctly.

Let's say you have a scenario involving two deployment stages: staging and production. You’ll need to add config/deploy/staging.rb and config/deploy/production.rb files. However, it makes sense to adhere to DRY and avoid duplicating lines between the two files. So it would be nicer to keep common settings in config/deploy.rb, and only put stuff in each staging definition file which is really specific to that staging environment. Fortunately this can be done using the lazy evaluation form of set.

So config/deploy.rb file would contain something like:

set :stages, %w(production staging)
set :default_stage, "staging"
require 'capistrano/ext/multistage'

set(:unicorn_env) { rails_env }

role(:web) { domain }
role(:app) { domain }
role(:db, :primary => true) { domain }

set(:deploy_to)    { "/home/#{user}/#{application}/#{fetch :rails_env}" }
set(:current_path) { File.join(deploy_to, current_dir) }

Then config/deploy/production.rb would contain something like:

set :domain,      "app.mydomain.com"
set :rails_env,   "production"

and config/deploy/staging.rb would only need to contain something like:

set :domain,      "app.staging.mydomain.com"
set :rails_env,   "staging"

Nice and clean!

License

See LICENSE file for details.