LoginSignup
33
29

More than 5 years have passed since last update.

capistrano3でのデプロイを7分弱から30秒に短縮

Last updated at Posted at 2015-07-11

1. はじめに

Railsでデプロイといえばcapistranoを使っている人が多いかと思いますが,
悩まされるのがデプロイ時間(主にassets:precompile).
自身のさくらVPSではデプロイに7分ほどかかり,その内6分がassets:precompileでした.

現状のRails4.2.3 && capistrano 3.4.0で,
どういう設定をしたら無駄なprecompileとmigrationをスキップできるかをまとめます.

2. DBのmigration skip

まずはdbのmigration skipです.
こちらは公式にも方法が記されています.

config/deploy.rb
# Defaults to false. If true, it's skip migration if files in db/migrate not modified
set :conditionally_migrate, true

このオプションを付け足すと,db/migrateに差分がなければmigrationをスキップします.

3. assets:precompile skip

次に本題のassets:precompileのスキップです.
capistrano-faster-assetsというgemを利用します.

これは,

  • app/assets
  • lib/assets
  • vendor/assets
  • Gemfile.lock

の4種類に対してdiffを取り,変更がなければassets:precompileをスキップしてくれます.

Gemfileにgem 'capistrano-faster-assets'を追加し,
Capfileを以下のように設定します.

require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/faster_assets' # 必ずrails/assetsの下に記述
require 'capistrano/rails/migrations'

1度目のデプロイで,キャッシュを作成し,
2回目以降は差分がなければassets:precompileをスキップしてくれます.

また,警告にも書かれていますが,

text = <%= helper.get_text %>

このようなerb形式の記述をしていた場合,helperの内容に変更があったとしても,
assets:precompileをされない場合があるため,心に留めておいたほうが良さそうです.

4. その他お勧めオプション

remote_cache

set :deploy_via, :remote_cache

詳細はこちらに記述されています.

33
29
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33
29