LoginSignup
9
8

More than 5 years have passed since last update.

[Capistrano] assets:precompile をローカルでして, リモートに上げる方法

Posted at

[Capistrano] assets:precompile をローカルでして, リモートに上げる方法

環境

  1. Rails 4.2.4
  2. Ruby 2.3.0
  3. capistrano 3.5.0

最近デプロイ時に異常にassets:precompileで時間がかかるようになったので,
デプロイの高速化を図ろうと思い実装してみました。

今回はassets:precompileの部分を切り出して紹介します。

Capfile編集

Capfile
# 他省略

#require 'capistrano/rails'を以下のように修正

require 'capistrano/bundler'
require 'capistrano/rails/migrations'

# 他省略

require 'capistrano/rails' では, 以下の3つをrequireしてしまうため, precompileを抜いた状態にします。
1. require 'capistrano/bundler'
2. require 'capistrano/rails/assets'
3. require 'capistrano/rails/migrations'

deploy.rb編集 --ポート22の場合

config/deploy.rb

# 他省略
set :user, 'ユーザ名'

namespace :deploy do
  # 他省略
  after :updated, "assets:precompile" # 追加
  # 他省略
end


namespace :assets do
  desc "Precompile assets locally and then rsync to web servers"
  task :precompile do
    on roles(:web) do
      rsync_host = host.to_s
      run_locally do
        with rails_env: fetch(:stage) do
          execute "rake assets:precompile"
        end

        execute 'rsync -av --delete public/assets/ #{fetch(:user)}@#{rsync_host}:#{shared_path}/public/assets/'
        execute "rm -rf public/assets"
      end

    end
  end
end

deploy.rb編集 --別のポートに変更している場合

上記の以下のコードだとデフォルトのポート番号が22なので,他のポート番号で設定している方は,
次のように変更してください

-execute 'rsync -av --delete public/assets/ #{fetch(:user)}@#{rsync_host}:#{shared_path}/public/assets/'

+execute 'rsync --rsh="ssh -p ポート番号" -av --delete public/assets/ #{fetch(:user)}@#{rsync_host}:#{shared_path}/public/assets/' 

※このように設定すると,rsyncで同期する際に,いちいちパスワードを聞かれます。
もし,パスワードを聞かれたくない方は次のように鍵を設定してあげると聞かれないようになります。

deploy.rb編集 --別のポートに変更している場合 + 公開鍵設定

  1. 鍵作成
ssh-keygen -t rsa

### 対話形式
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/?????/.ssh/id_rsa):hogehoge <= (ファイル名(デフォルト: id_rsa))
Enter passphrase (empty for no passphrase):      <= パスワード(指定しないのであればEnter)
Enter same passphrase again:      <= パスワード確認(パスワードを指定しないのであればEnter)
Your identification has been saved in hogehoge.
Your public key has been saved in hogehoge.pub.
The key fingerprint is:

2.サーバーでの設定

# 先ほど作成した公開鍵をサーバーにアップする
scp  ~/.ssh/hogehoge.pub ユーザ名@ホスト名:/home/ユーザ名/.ssh

# サーバーにログイン
ssh ホスト名

# authorized_keysに公開鍵を設定
cat /home/ユーザ名/.ssh/hogehoge.pub >> /home/ユーザ名/.ssh/authorized_keys

3.deploy.rbの編集

config/deploy.rb

-execute 'rsync --rsh="ssh -p ポート番号" -av --delete public/assets/ #{fetch(:user)}@#{rsync_host}:#{shared_path}/public/assets/' 

+execute 'rsync --rsh="ssh -i ~/.ssh/hogehoge -p ポート番号" -av --delete public/assets/ #{fetch(:user)}@#{rsync_host}:#{shared_path}/public/assets/' 

注意: execute ' ...-rsh="" ' の '' と "" を逆にすると,
なぜか相対パスが聞かなくなりました。

以上でパスワードが聞かれることがなくなります。

ご清聴ありがとうございました。
もっといい方法とか簡単にできる方法とかあったら
教えていただけるといただけると嬉しいです。

9
8
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
9
8