Adam Bishti
Adam Bishti

Reputation: 545

Capistrano and Bitbucket permission denied

I'm trying to set up capistrano with my Rails app and hosting using digitalocean.

I have a Ubuntu server running unicorn and nginx.

My capistrano deploy keeps failing at this stage:

DEBUG [08cab5b3] Command: cd /home/rails/automata && (     GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/automata/git-ssh.sh /usr/bin/env git     clone --mirror [email protected]:automata_tech/staging.git     /home/rails/automata/repo )
DEBUG [08cab5b3]        Cloning into bare repository     '/home/rails/automata/repo'...
DEBUG [08cab5b3]        /home/rails/automata/repo: Permission denied
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as     [email protected]: git exit status: 1
git stdout: Nothing written
git stderr: Cloning into bare repository    '/home/rails/automata/repo'...
/home/rails/automata/repo: Permission denied

SSHKit::Command::Failed: git exit status: 1
git stdout: Nothing written
git stderr: Cloning into bare repository    '/home/rails/automata/repo'...
/home/rails/automata/repo: Permission denied

Tasks: TOP => git:create_release => git:update => git:clone
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing as     [email protected]: git exit status: 1
git stdout: Nothing written
git stderr: Cloning into bare repository    '/home/rails/automata/repo'...
/home/rails/automata/repo: Permission denied

I have generated a ssh key on the server and added it to my bitbucket account.

If I ssh into the server and git clone the repo, that works fine.

Running ssh -T [email protected] on the server returns:

 logged in as company.

 You can use git or hg to connect to Bitbucket. Shell access is disabled.

staging.rb :

set :ssh_options, {
  forward_agent: true,
  auth_methods: %w(publickey)
}

Upvotes: 1

Views: 900

Answers (2)

Matt Brictson
Matt Brictson

Reputation: 11082

Your error message shows a permission denied error when trying to create the directory /home/rails/automata/repo. In other words, your deployer user doesn't have the necessary file permissions. It has nothing to do with your SSH key or Bitbucket.

Just make sure that /home/rails/automata exists and is owned by deployer. You may need to use sudo or log in as root to do this.

mkdir -p /home/rails/automata
chown deployer /home/rails/automata

BTW, I'm not sure placing your app inside /home/rails makes sense. If you are using deployer to deploy (and thus be the owner of) your app, wouldn't it make more sense to put it in /home/deployer? Or in a "neutral" location like /var/www/automata (as Capistrano suggests by default)?

Upvotes: 2

Kyle Balderson
Kyle Balderson

Reputation: 31

In your config/deploy/production.rb (or staging.rb), you need to enable ssh agent forwarding.

You can do this by changing your server file to include forward_agent: true.

Here's an example:

server 'xxx.xxx.xxx.xxx',
  user: 'deploy',
  roles: %w{web app db},
  ssh_options: {
    forward_agent: true,
    auth_methods: %w(publickey)
  }

Upvotes: 0

Related Questions