2015年3月8日日曜日

Capistrano3でSinatraをデプロイ

Railsでは使っていたがSinatraは初めてなのでメモ。

capistrano3インストール



Gemfileに以下のように書いてbundle install。


group :development do
gem 'capistrano'
gem 'capistrano-bundler'
gem 'capistrano-rails', '~> 1.1.3'
end



その後

$ bin/cap install

これでCapfileなどが作られる。

capistrano task設定

capistranoのタスクは http://qiita.com/satococoa/items/9b0cc416ffc042680b9b を参考に以下のようにして、
lib/capistrano/tasks/unicorn.rake に保存。

namespace :deploy do
task :environment do
set :unicorn_pid, "#{shared_path}/tmp/pids/unicorn.pid"
set :unicorn_config, "#{current_path}/config/unicorn/unicorn_#{fetch(:rails_env)}.rb"
set :unicorn_path, "#{current_path}/bin/unicorn"
end

def start_unicorn
within current_path do
execute "#{fetch(:unicorn_path)} -c #{fetch(:unicorn_config)} -E #{fetch(:rails_env)} -D"
end
end

def stop_unicorn
execute :kill, "-s QUIT $(< #{fetch(:unicorn_pid)})" end def reload_unicorn execute :kill, "-s USR2 $(< #{fetch(:unicorn_pid)})" end def force_stop_unicorn execute :kill, "$(< #{fetch(:unicorn_pid)})" end desc "Start unicorn server" task :start => :environment do
on roles(:app) do
start_unicorn
end
end

desc "Stop unicorn server gracefully"
task :stop => :environment do
on roles(:app) do
stop_unicorn
end
end

desc "Restart unicorn server gracefully"
task :restart => :environment do
on roles(:app) do
if test("[ -f #{fetch(:unicorn_pid)} ]")
reload_unicorn
else
start_unicorn
end
end
end

desc "Stop unicorn server immediately"
task :force_stop => :environment do
on roles(:app) do
force_stop_unicorn
end
end
end
end


Capfile

Capfileはとりあえずbundle installしてくれるうにcapistrano/bundlerはONにした。
assets_compileするにはcapistrano/rails/assetsもONに。
あと先ほどのtaskファイルを読み込むように。

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

# Include default deployment tasks
require 'capistrano/deploy'

# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
# https://github.com/capistrano/rvm
# https://github.com/capistrano/rbenv
# https://github.com/capistrano/chruby
# https://github.com/capistrano/bundler
# https://github.com/capistrano/rails
# https://github.com/capistrano/passenger
#
# require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
# require 'capistrano/rails/migrations'
# require 'capistrano/passenger'

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


config/deploy.rb


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

set :application, '#{app_name}'
set :repo_url, '#{repo_url}'

# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp

# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, '#{deploy_path}'
set :bundle_flags, "--binstubs" # bundleにbinstubsオプションをつけている。

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

# Default value for :format is :pretty
# set :format, :pretty

# Default value for :log_level is :debug
# set :log_level, :debug

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

# Default value for :linked_files is []
# set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')

# 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, { path: "/opt/ruby/bin:$PATH" }

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

namespace :deploy do

after :restart, :clear_cache do
on roles(:app), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
after 'deploy:publishing', 'deploy:restart' # これ入れないとdeployした後にunicornに更新が読みこまれない。

end


config/deploy/production

こちらは

set :rails_env, "production"

を入れるのと、assets_compileはroleがwebのときにしか実行されないので、
webサーバをかねる場合はwebのroleをつける必要がある。

またその場合、Railsでアセットのルーティングを処理しなければいけないので、
config/environments/production.rbでconfig.serve_static_assetsをtrueに設定する。
これをやらないと画像、css,jsなどがNot Foundになってしまう。




これで

$ bin/cap production deploy

で本番リリースできるようになります。


更新履歴

2015/06/20 assets_compileについての記載追加。

0 件のコメント:

コメントを投稿