Table of Contents

Middleware

Capistrano

Web Sites

Legacy


Installation

# gem search capistrano
# gem install capistrano

# cap --version
# mkdir capistrano
# cap install
mkdir -p config/deploy
create config/deploy.rb
create config/deploy/staging.rb
create config/deploy/production.rb
mkdir -p lib/capistrano/tasks
create Capfile
Capified
(abbr)

list all available tasks

# cap -T
cap deploy                         # Deploy a new release
cap deploy:check                   # Check required files and directories exist
cap deploy:check:directories       # Check shared and release directories exist
cap deploy:check:linked_dirs       # Check directories to be linked exist in shared
cap deploy:check:linked_files      # Check files to be linked exist in shared
cap deploy:check:make_linked_dirs  # Check directories of files to be linked exis...
cap deploy:cleanup                 # Clean up old releases
cap deploy:cleanup_rollback        # Remove and archive rolled-back release
cap deploy:finished                # Finished
(abbr)


Commands

help

# cap -h
Install capistrano in a project:
    bundle exec cap install [STAGES=qa,staging,production,...]

Show available tasks:
    bundle exec cap -T

Invoke (or simulate invoking) a task:
    bundle exec cap [--dry-run] STAGE TASK

Advanced options:
    -h, -H, --help                   Display this help message.
    -f, --rakefile [FILENAME]        Use FILENAME as the rakefile to search for.
    -T, --tasks [PATTERN]            Display the tasks (matching optional PATTERN) with descriptions, then exit.
    -t, --trace=[OUT]                Turn on invoke/execute tracing, enable full backtrace. OUT can be stderr (default) or stdout.

    -r, --roles ROLES                Run SSH commands only on hosts matching these roles
    -z, --hosts HOSTS                Run SSH commands only on matching hosts

console

cap production console ROLES=web
cap staging console

task

cap -t production deploy BRANCH=test
hosts
cap --hosts=server1,server2 production deploy
HOSTS=server1,server2 cap production deploy
roles
cap --roles=app,web production deploy
ROLES=app,web cap production deploy


Configuration

console

Capfile

require 'capistrano/console'


STAGE(Hosts)

config/deploy/STAGE.rb
config/deploy/production.rb
config/deploy/staging.rb

server-based syntax

server 'localhost'
server 'example.com', roles: [:web, :app]
server 'db.example.com', user: 'deploy', roles: %w{db}
server 'localhost', user: 'vagrant', roles: %w{web}

role-based syntax

role :web, %w{host1 host2}


TASK

capistrano/config/deproy.rb

capistrano/lib/capistrano/tasks/***.rake

#cap test:show_hostname             # Show hostname

namespace :test do

  desc 'Show hostname'
  task :show_hostname do
    on roles :all do
      execute 'hostname'
    end
  end

end
require "capistrano_colors"

desc "test"
namespace :deploy do
  task :test1 do
    run "pwd"
    run "echo \"Hello Servers!\""
  end
end
within /tmp/test do
    ls -l /tmp/test
end
if test "[ ! -e /tmp/test ]"
    touch /tmp/test
end
desc 'upload file'
task :upload_file do
    on roles(:app) do |host|
        if test "[ ! -e #{shared_path}/.env ]"
            upload!('../.env.example', "#{shared_path}/.env") # .env
            execute 'echo ".env uploaded!"'
        end
    end
end
desc 'uptime test'
task :uptime do
  #local
  run_locally do
    output = capture "uptime"
  end
  #remote
  on roles(:web) do
    output = capture "uptime"
  end
end
task :deploy => :archive do
  archive_path = fetch :archive_absolute_path
  archive_name = fetch :archive_name
  release_path = File.join(fetch(:deploy_to), fetch(:application))

  on roles(:web) do
    unless test "[ -d #{release_path} ]"
      execute "mkdir -p #{release_path}"
    end

    execute "cd #{release_path}; tar -zxvf #{archive_name}"

    project_dir = File.join(release_path, capture("cd #{release_path}; ls -d */").chomp)

    launch = capture("cd #{project_dir}; ls bin/*").chomp
  end
end


TIP

Delete default TASKS

Sequence

deploy.rb

#sequence
module SSHKit
  class Coordinator
    private
      def default_options
        { in: :sequence, wait: 1 }
      end
  end
end 
module SSHKit
  class Coordinator
    private
      def default_options
        { in: :groups, limit: 5 }
      end
  end
end