Deploying with Capistrano 3 failing – Permission denied (publickey)

The problem:

Running cap production deploy was failing since it was not able to connect to Github.
The primary error generated was Permission denied (publickey), followed by fatal: The remote end hung up unexpectedly.

Command: cd /home/[user]/apps/[appname] && ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/[appname]/ /usr/bin/env git clone --mirror[user]/[appname].git /home/[user]/apps/[appname]/repo )
 Cloning into bare repository '/home/[user]/apps/[appname]/repo'...
 Cloning into bare repository '/home/[user]/apps/[appname]/repo'...
 Warning: Permanently added ',###.###.###.###' (RSA) to the list of known hosts.
 Cloning into bare repository '/home/[user]/apps/[appname]/repo'...
 Permission denied (publickey).
 Cloning into bare repository '/home/[user]/apps/[appname]/repo'...
 fatal: The remote end hung up unexpectedly


There are many reasons which could cause this issue. In this case while troubleshooting the issue, running ssh-add generated a new error.

  Could not open a connection to your authentication agent.

Google led me to this page which provided the solution.

On the host (not the target) run

eval $(ssh-agent)

You should see the following output
Identity added: /Users/[user]/.ssh/id_rsa (/Users/[user]/.ssh/id_rsa)

I noticed that this fix is not permanent. Have to run ssh-add for new deployments.

Leave a Reply