Deploying w/ Bitbucket repo

I am using BitBucket to host my (private) repository. I am getting the following error when I run $ bundle exec cap staging deploy (I’ve replaced my username and server address with ‘username@example.com’):

(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as username@example.com: git exit status: 128
git stdout: Nothing written
git stderr: Permission denied (publickey).
fatal: The remote end hung up unexpectedly

SSHKit::Command::Failed: git exit status: 128
git stdout: Nothing written
git stderr: Permission denied (publickey).
fatal: The remote end hung up unexpectedly

Tasks: TOP => git:check
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing as username@example.com: git exit status: 128
git stdout: Nothing written
git stderr: Permission denied (publickey).
fatal: The remote end hung up unexpectedly

Before coming here I did a lot of research into what could be going wrong, however there are a few uncertainties I have about the way Capistrano is handling things that I’d love to clear up.

  • I’ve given deploy.rb my repo’s SSH URL: set :repo_url, 'git@bitbucket.org:[my-repo]'
  • I am unsure whether Capistrano is trying to access the repo from my local server or my remote server; BitBucket suggests using $ ssh -Tv git@bitbucket.org to troubleshoot SSH keys, and I’m not sure whether to run that from my local root directory in Terminal, or after $ ssh'ing into my remote server? I have tried both and have had successful connections to both, this just isn’t reflected when attempting to connect when running a Capistrano deploy.

Is it an issue with the repository being private? Perhaps a deploy.rb or staging.rb config issue? I have provided a couple of gists if it helps to look at (again, I changed the server name and username to placeholders):

Thanks in advance!

You are able to ssh into your remote server, and run a git pull from your bitbucket repo using the deploy user, correct?

I haven’t tested this, I’ll do it now. I haven’t cloned my repository onto my remote server yet (i.e. srv/www/dev.example.com), is this something I need to do first before pulling?

Sorry if this is extending outside the support of this forum.

To answer your questions:

Capistrano runs everything on the remote server unless you specify some local action. So any git command is on the remote server which means that server needs to be able to access your private repository.

Basically: you need ssh -Tv git@bitbucket.org to work on the remote server run under your deploy user.

If that doesn’t work, then deploying definitely won’t.

I’m going to close this topic since it is outside the scope of our support. There are lots of resources online about getting a remote server to connect to a private Git repo.