読者です 読者をやめる 読者になる 読者になる

Capistrano3 でRailsをデプロイするとき、current_path, shared_path などを正しく取得する

rails ruby capistrano3

$ config/deploy.rb

# config valid only for current version of Capistrano
lock '3.4.0'

set :application, 'hoge'
set :repo_url, 'git@bitbucket.org:hoge/hoge.git'

# Default value for :scm is :git
set :scm, :git

#
set :rbenv_ruby, '2.2.1'

# Default value for :pty is false
set :pty, true

# Default value for linked_dirs is []
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')

# Default value for default_env is {}
set :default_env, { rbenv_root: "$HOME/.rbenv/bin/rbenv", path: "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH" }

# Default value for keep_releases is 5
set :keep_releases, 4

# ここ!lambda を使う!
set :unicorn_config_path, -> {"#{current_path}/config/unicorn.rb"}

# ここ!lambda を使う!
set :unicorn_pid, -> {"#{shared_path}/tmp/pids/unicorn.pid"}

$ config/deploy/production.rb

set :stage,            :production
set :rails_env,        :production
set :unicorn_rack_env, :production

# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, '/var/www/html/hoge'

server 'localhost', user: 'app', roles: %w{web app db

$ Capfile

# Load DSL and set up stages
require 'capistrano/setup'

# Include default deployment tasks
require 'capistrano/deploy'

#
require 'capistrano/rbenv'
require 'capistrano/bundler'
#require 'capistrano/rails/assets' # asset_sync を使うため外している
require 'capistrano/rails/migrations'
require 'capistrano3/unicorn'

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }