Capistrano と git と公開鍵認証

Pocket

Capistrano はデプロイツールの1つとして知られていますが、今回はデプロイターゲットサーバ、ならびに gitosis(github でもいいです)リポジトリコンテナに対して公開鍵認証を使用してデプロイをやってみることにします。ここに辿り着くまで長かった・・・。まだマイグレーションとかリスタートの処理は試していませんが、とりあえずデプロイできたのでメモです。

言葉で説明するよりもコードを読んでいただいたほうが速いと思うので deploy.rb をさらします。もちろん企業秘密は秘密にしてあります。

set :application, "rails_sample"
set :repository,  "git@git.example.com:unicast/sample/rails_sample.git"

set :scm, :git
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
set :scm_passphrase, "foo_bar_passphrase"
default_run_options[:pty] = true

role :web, "example.com"                          # Your HTTP server, Apache/etc
role :app, "example.com"                          # This may be the same as your `Web` server
role :db,  "example.com", :primary => true        # This is where Rails migrations will run
#role :db,  "your slave db-server here"

# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts

set :deploy_to, "/home/#{application}/#{application}"
set :user, "rails_sample"

# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
   task :start do
     run "#{try_sudo} service httpd restart"
   end
#   task :stop do ; end
   task :restart, :roles => :app, :except => { :no_release => true } do
     run "touch #{File.join(current_path,'tmp','restart.txt')}"
   end
end

ポイントは

  1. set :repository, "git@git.example.com:unicast/sample/rails_sample.git"
  2. set :scm, :git
  3. set :scm_passphrase, "foo_bar_passphrase"
  4. default_run_options[:pty] = true
  5. Capistrano のインストールしてあるマシンにデプロイターゲットサーバと git リポジトリコンテナアカウントの秘密鍵が両方指定されていること
  6. デプロイターゲットサーバのアカウントに git リポジトリコンテナアカウントの秘密鍵が指定されていること(.ssh/id_rsa)

です。特に3~6の設定がセンシティブなので注意して設定します。3については git リポジトリコンテナアカウントのパスフレーズを指定します。5についてはクライアントが Linux なら .ssh/config に、 Windows であれば Putty の Pageant を使います。詳しい設定方法についてはぐぐってください。

以上を踏まえて以下のコマンドをタイプすれば OK です。

$ cap deploy:update

参考

  • Help.GitHub - Deploy with Capistrano
  • Why is the host key failing? - Capistrano | Google Groups
  • 投稿者紹介

    株式会社ユニキャスト
    私たちは、テクノロジに魅せられた個性あふれるメンバーによって構成された茨城県日立市に本社を構えるベンチャー企業です。
    ”テクノロジを通して「驚き」と「感動」を創造し、人々の「夢」と「笑顔」を支えます。” の経営理念をモットーに明るい未来を描き、ワクワクする企画提案を続けて参ります。

    コメントを残す

    メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

    このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください