rubber capistrano deploy to ec2 from bitbucket getting permission denied

I’ve done this a few times in the past for other projects and eventually get it working, but each time I can’t seem to remember exactly what to do.

I have an ec2 ubuntu server and am deploying with capistrano and have used rubber to set evertyhing up.

  • Startup script on EC2
  • error: waitpid for C:\Program Files (x86)\PuTTY\plink.exe failed: No child processes
  • Remotely Installing Websites Using Git with Nginx
  • Setting up Git on EC2 to pull from GitHub repo
  • setup AWSDevTools-RepositorySetup.sh in git repository on ubuntu
  • Deploy a GitHub branch automatically to AWS Elastic Beanstalk
  • On my ec2 server, I’ve generated ssh keys, added to bitbucket (under my accounts, ssh key. at first i tried under the project and deployment keys, but that didn’t work either). On my ec2 server, I can successfully manually ssh in and clone my repo into my home directly. I’ve added the key to ssh agent.

    I can deploy just fine if I do not use scm and set deploy_via to :copy. I believe this grabs my local project and deploys it.

    I’ve read these: https://confluence.atlassian.com/display/BITBUCKET/Using+Deployment+Keys
    https://confluence.atlassian.com/display/BITBUCKET/Troubleshooting+SSH+Issues
    https://confluence.atlassian.com/pages/viewpage.action?pageId=270827678

    This works fine:

    ubuntu@production:~$ ssh -T hg@bitbucket.org
    conq: logged in as myBitBucketUser.
    
    You can use git or hg to connect to Bitbucket. Shell access is disabled.
    

    Here’s the error message I get during deploy:

    * executing "if [ -d /mnt/myProject-production/shared/cached-copy ]; then cd /mnt/myProject-production/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard f2b5b8b6cd1c7835d020d66fdc09b42b2870561e && git clean -q -d -x -f; else git clone -q git@bitbucket.org:myRepo.git /mnt/myProject-production/shared/cached-copy && cd /mnt/myProject-production/shared/cached-copy && git checkout -q -b deploy f2b5b8b6cd1c7835d020d66fdc09b42b2870561e; fi"
        servers: ["production.myserver.com"]
        [production.myserver.com] executing command
     ** [production.myserver.com :: out] Permission denied (publickey).
     ** fatal: The remote end hung up unexpectedly
    

    Here’s my deploy.rb:

    set :rails_env, Rubber.env
    
    on :load do
      set :application, rubber_env.app_name
      set :runner,      rubber_env.app_user
      set :deploy_to,   "/mnt/#{application}-#{Rubber.env}"
      set :copy_exclude, [".git/*", ".bundle/*", "log/*", ".rvmrc"]
    end
    
    # Use a simple directory tree copy here to make demo easier.
    # You probably want to use your own repository for a real app
    #set :scm, :none
    #set :repository, "."
    #set :deploy_via, :copy
    
    ssh_options[:forward_agent] = true # also tried false
    set :repository, "git@bitbucket.org:myProject.git"
    set :scm, "git"
    set :deploy_via, :remote_cache
    set :branch, 'master'
    
    # Easier to do system level config as root - probably should do it through
    # sudo in the future.  We use ssh keys for access, so no passwd needed
    set :user, 'root'
    set :password, nil
    
    # Use sudo with user rails for cap deploy:[stop|start|restart]
    # This way exposed services (mongrel) aren't running as a privileged user
    set :use_sudo, false # also tried true
    

  • Git push force issue - Unable to force local changes on top of remote
  • What is the best approach for deploying a sails.js application?
  • How to preserve history on bitbucket while moving files within a repository
  • GIT rebase requires to recommit changes
  • Bitbucket API bad request while trying to merge
  • SSH multiple BitBucket accounts + 2 step verification recovery
  • 2 Solutions collect form web for “rubber capistrano deploy to ec2 from bitbucket getting permission denied”

    I ran into this problem using rubber as well.

    If you have multiple instances you’ll need to get SSH keys for all instances as rubber will deploy the code to all. Haven’t found a way to prevent this.

    The ssh keys to use are under ~/.ssh or /root/.ssh if you the keys you generated are in a different directory or under a different user they are probably not being recognized as rubber deploys using the user ‘root’ and that’s the user fetching from the repo.

    Hope this helps.

    I used the ssh keys generated in my local machine both for the server and for bitbucket repo and deployed using capistrano. So, using that would solve your problem.

    The way i did

    I used public key(id_rsa.pub) of my local machine both for

    • ec2 instance – guide and
    • bitbucket – guide .

    deploy.rb

    ssh_options[:forward_agent] = true
    

    That forwards the authentication from your local machine through when the deployment server tries to connect to your git server.

    Possible solution to the way you tried

    Unlike ssh git@github.com, ssh git@bitbucket does not add itself to the list of known hosts.
    So the way to do in bitbucket must be https://confluence.atlassian.com/display/BITBUCKET/Using+the+SSH+protocol+with+Bitbucket

    Git Baby is a git and github fan, let's start git clone.