title: Capistrano and GitHub Private Repo – Permission denied (publickey)
body: <p>I've inherited a Rails project, hosted on Linode. </p>
  
  <p>The previous developer was using a BitBucket repository, along with Capistrano for deployments. </p>
  
  <p>I've since setup a private repository on GitHub, and I'm trying to get the Capistrano recipe to work. I'm having no luck. I continue to get a publickey error during deployment.</p>
  
  <p>Here are the steps I've taken –</p>
  
  <ol>
  <li>Updated the Git remote (origin) URL on the Linode server to point to my new repository</li>
  <li>Updated the repository reference in the Capfile, to reference my new repository </li>
  <li>Ensured <code>ssh_options[:forward_agent]</code> was set to true in the Capfile</li>
  <li>Generated an SSH key locally (id_rsa.pub) and added it to my user account in GitHub</li>
  <li>Executed the <code>ssh-add</code> command, to ensure the identity was added for auth agent</li>
  <li>Ran <code>ssh -T git@github.com</code> to confirm ssh was properly setup locally</li>
  <li>Logged into my Linode server and ran <code>ssh -T git@github.com</code> to ensure it was working also</li>
  </ol>
  
  <p>Additionally, just in case the forward_agent property wasn't working, I even tried generating an SSH key on the Linode server, and adding it to GitHub as well. No luck.</p>
  
  <p>After all of this, when I run <code>cap deploy</code>, I get the following error:</p>
  
  <pre><code>Permission denied (publickey).
  fatal: The remote end hung up unexpectedly    
  </code></pre>
  
  <p>Below is the recipe I'm using – </p>
  
  <pre><code>require "bundler/capistrano"
  
  server "----SERVER IP----", :web, :app, :db, primary: true
  
  set :application, "blog"
  set :user, "deployer"
  set :deploy_to, "/var/www/blog"
  set :deploy_via, :remote_cache
  set :use_sudo, false
  
  set :scm, "git"
  set :repository, "git@github.com:--MY USERNAME--/blog.git"
  set :branch, "master"
  
  default_run_options[:pty] = true
  ssh_options[:forward_agent] = true
  
  after "deploy", "deploy:cleanup" # keep only the last 5 releases
  
  namespace :deploy do
    task :start do; end
    task :stop do; end
    task :restart, roles: :app, except: {no_release: true} do
      run "touch #{deploy_to}/current/tmp/restart.txt"
    end
  
    task :setup_config, roles: :app do
      sudo "ln -nfs #{current_path}/config/apache.conf /etc/apache2/sites-available/blog"
      run "mkdir -p #{shared_path}/config"
      put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
      puts "Now edit the config files in #{shared_path}."
    end
    after "deploy:setup", "deploy:setup_config"
  
    task :symlink_config, roles: :app do
      run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
      run "ln -nfs #{shared_path}/public/avatars #{release_path}/public/avatars"    
    end
    after "deploy:finalize_update", "deploy:symlink_config"
  
    desc "Make sure local git is in sync with remote."
    task :check_revision, roles: :web do
      unless `git rev-parse HEAD` == `git rev-parse origin/master`
        puts "WARNING: HEAD is not the same as origin/master"
        puts "Run `git push` to sync changes."
        exit
      end
    end
    before "deploy", "deploy:check_revision"
  end
  </code></pre>
  
  <p>I can't seem to figure out where I'm going wrong – any help would be greatly appreciated.</p>
  
  <hr>
  
  <h2>UPDATE</h2>
  
  <p>I've also ensured the following was added to my local ~/.ssh/config file...</p>
  
  <pre><code>Host mydomain.com
    ForwardAgent yes
  </code></pre>