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]/git-ssh.sh /usr/bin/env git clone --mirror git@github.com:[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 'github.com,###.###.###.###' (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

Solution:

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

  ssh-add
  Could not open a connection to your authentication agent.

Google led me to this page https://coderwall.com/p/rdi_wq which provided the solution.

On the host (not the target) run

eval $(ssh-agent)
ssh-add

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