CapistranoでPermission denied (publickey)が出た時の対処

Capistranoでdeploy:check時にPermission denied (publickey)のエラーが出た時の対処法メモ。

原因

デプロイ先のサーバーで秘密鍵・公開鍵の作成管理が行われていないため、デプロイ先のサーバーからgithubに接続できない。

対応

ssh agent forwardingを利用。

参考:capistrano3でssh agent forwarding

エラー内容の確認

$ bundle exec cap staging deploy:check
(略)
git:check
01 git ls-remote --heads git@github.com:hoge/huga.git
01 Permission denied (publickey).
01 fatal: Could not read from remote repository.
01
01 Please make sure you have the correct access rights
01 and the repository exists.

ssh-agentに登録されている鍵の確認

$ ssh-add -l
The agent has no identities.

ssh-agentに鍵を登録

$ ssh-add ~/.ssh/huga.pem
$ ssh-add -l
2048 (略) /Users/hoge/.ssh/huga.pem (RSA)

参考