tkuchikiの日記

新ブログ https://blog.tkuchiki.net

capistrano の deploy を git (ssh) で行うときの設定

capistrano で、repository を以下のように設定した場合の諸々の設定。
※この作業は、~/.ssh/id_rsa 以外を使う場合に必要となる。

# config/deploy.rb

set :scm, "git"
set :repository, "git@github.com:tkuchiki/example.git"

このまま deploy しようとしても、以下の様なエラーがでる。

Permission denied (publickey).
fatal: Could not read from remote repository.

repository につなげていないからっぽいので、
~/.ssh/config に設定を書く。

cat >> ~/.ssh/config
Host example.github.com
  User git
  Port 22
  Hostname github.com
  IdentityFile ~/.ssh/example_rsa.pem

config/deploy.rb の :repository に書く repository を .ssh/config で設定した Host に合わせる。
上記例では、Host = example.github.com

# config/deploy.rb

set :scm, "git"
set :repository, "git@example.github.com:tkuchiki/example.git"

set :deploy_via, :copy の場合は capistrano を実行するマシンに設定するだけで良いが、
set :deploy_via, :remote_cache にした場合は、deploy 先のサーバにも同じ設定をする必要がある。
上記2つ以外使用したことがないので不明だが、他にも deploy 先のサーバから git コマンドを使うようなオプションがある場合は、同様に設定する必要がある。

これで、github に登録した deploy keys で deploy できる。