Search code examples
ruby-on-railscapistranodigital-ocean

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)
}

Solution

  • 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)?