Rails+Capistrano環境でデプロイ

Rails+Capistranoでデプロイした時のメモ

Gitで管理

tsyknsr.hatenablog.com

Capistrano

GitHub - capistrano/capistrano: Remote multi-server automation tool

動作確認用に最小限の設定です。設定等はお好みで適宜変更してください。(記事に関係ない部分は一部省略している箇所もあります)

  • Gemfile
group :development do
  gem 'capistrano', '~> 3.10'
  gem 'capistrano-rails'
  gem 'capistrano-rbenv'
  gem 'capistrano-bundler'
  gem 'capistrano3-unicorn'
end
$ bundle install
$ bundle exec 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
  • config/deploy/production.rb
set :branch, 'master'
server '192.0.2.0', user: 'username', roles: %w{web app db}

role :app, %w{username@192.0.2.0}
role :web, %w{username@192.0.2.0}
role :db,  %w{username@192.0.2.0}

# ポートフォワーディングを利用する場合
set :ssh_options, {
  keys: %w(~/.ssh/id_rsa),
  forward_agent: true,
  auth_methods: %w(publickey)
}
  • Capfile
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/rbenv'
require 'capistrano/bundler'
require 'capistrano3/unicorn'
require 'capistrano/sitemap_generator'
require 'whenever/capistrano'

require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git

Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
  • config/deploy.rb
# config valid only for current version of Capistrano
lock "3.8.0"

set :application, "my_app_name"
set :repo_url, "git@example.com:me/my_repo.git"
set :deploy_to, "/var/www/nginx/example.com"

set :pty, true

set :linked_files, "config/database.yml", "config/secrets.yml"
set :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system"

# rbenv
set :rbenv_type, :user
set :rbenv_ruby, '2.5.0'
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, %w{rake gem bundle ruby rails}
set :rbenv_roles, :all

# unicorn
set :unicorn_pid, "#{shared_path}/tmp/pids/unicorn.pid"
set :unicorn_config_path, "config/unicorn.rb"

# whenever
set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }

before 'deploy:publishing', 'db:seed_fu'
after 'deploy:publishing', 'deploy:restart'
namespace :deploy do
  task :restart do
    invoke 'unicorn:restart'
  end
end
after 'deploy:restart', 'deploy:sitemap:create'
app_path = "/var/www/nginx/example.com"
working_directory "#{app_path}/current"
pid "#{app_path}/shared/tmp/pids/unicorn.pid"

listen "#{app_path}/shared/tmp/sockets/unicorn.sock"

stderr_path "#{app_path}/shared/log/unicorn.stderr.log"
stdout_path "#{app_path}/shared/log/unicorn.stdout.log"

worker_processes 3

timeout 15

preload_app true

before_exec do |server|
  Dotenv.overload
  ENV['BUNDLE_GEMFILE'] = "#{app_path}/current/Gemfile"
end

before_fork do |server, worker|
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection.disconnect!
  end

  old_pid = "#{server.config[:pid]}.oldbin"
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
    end
  end
end

after_fork do |server, worker|
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
  end
end

動作確認

$ bundle exec cap production deploy:check

参考リンク