title: Capistrano Deploy Failing on git:check - Permission denied (publickey)
body: <p>Local OS: Windows 10 Pro (using Git Bash as my terminal)</p>
  
  <p>Staging Server OS: Ubuntu 16.04 LTS</p>
  
  <p>I've been struggling to get a basic Capistrano deploy working on my staging server. I followed <a href="https://www.phusionpassenger.com/library/deploy/nginx/automating_app_updates/ruby" rel="noreferrer">this guide</a> to set up Capistrano.</p>
  
  <p>The deployment process always fails on the <code>git:check</code> stage due to allegedly not having permission to access my repo on GitLab. I am sure my SSH Agent forwarding is working because I am able to SSH into my server and access GitLab with my SSH key. The SSH key is not stored anywhere on my server:</p>
  
  <pre><code>$ ssh deploy@myserver.com
  
  deploy@MyServer:~$ ssh -T git@gitlab.com
  debug1: client_input_channel_open: ctype auth-agent@openssh.com rchan 2 win 65536 max 16384
  debug1: channel 1: new [authentication agent connection]
  debug1: confirm auth-agent@openssh.com
  Welcome to GitLab, Alexander!debug1: channel 1: FORCE input drain
  </code></pre>
  
  <p>None of the questions about this issue have been working for me so far.</p>
  
  <p>Here are my deploy files:</p>
  
  <p><strong>deploy.rb</strong></p>
  
  <pre><code>set :application, "myapp"
  set :branch, "master"
  set :repo_url, "git@gitlab.com:MyApp/myapp.git"
  
  # Defaults to false
  # Skip migration if files in db/migrate were not modified
  set :conditionally_migrate, true
  
  set :ssh_options, { forward_agent: true }
  
  set :rvm_ruby_version, '2.2.6'
  
  # Default deploy_to directory is /var/www/my_app_name
  set :deploy_to, "/var/www/#{fetch(:application)}/"
  set :deploy_user, "deploy"
  
  # Tells Capistrano to store config/database.yml file inside a directory called /shared, which is meant for any files
  # we want to persist between deploys
  set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')
  
  # Directories that are meant to persist between deploys, and they will also be stored inside /shared
  set :linked_dirs, fetch(:linked_dirs, []).push('bin', 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/uploads')
  
  # The specs that should be run before deployment is allowed to continue
  set :tests, []
  
  # Delayed Job Config: https://github.com/AgileConsultingLLC/capistrano3-delayed-job
  set :delayed_job_workers, 3
  
  # Keep the last 5 deploys for rollback purposes
  set :keep_releases, 5
  
  namespace :deploy do
    after :restart, :clear_cache do
      on roles(:web), in: :groups, limit: 3, wait: 10 do
        # Here we can do anything such as:
        # within release_path do
        #   execute :rake, 'cache:clear'
        # end
      end
    end
  end
  </code></pre>
  
  <p><strong>staging.rb</strong></p>
  
  <pre><code>set :stage, :staging
  set :rails_env, :staging
  
  role :app, %w{deploy@myserver.com}
  role :web, %w{deploy@myserver.com}
  role :db,  %w{deploy@myserver.com}
  </code></pre>
  
  <p>And here is the Capistrano debug log when I deploy:</p>
  
  <pre><code>$ bundle exec cap staging deploy --trace
  ** Invoke staging (first_time)
  ** Execute staging
  ** Invoke load:defaults (first_time)
  ** Execute load:defaults
  ** Invoke rvm:hook (first_time)
  ** Invoke passenger:rvm:hook (first_time)
  ** Invoke passenger:test_which_passenger (first_time)
  ** Execute passenger:test_which_passenger
  ** Execute passenger:rvm:hook
  ** Execute rvm:hook
  ** Invoke rvm:check (first_time)
  ** Execute rvm:check
  rvm 1.29.3 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
  ruby-2.2.6
  ruby 2.2.6p396 (2016-11-15 revision 56800) [x86_64-linux]
  ** Invoke bundler:map_bins (first_time)
  ** Invoke passenger:bundler:hook (first_time)
  ** Execute passenger:bundler:hook
  ** Execute bundler:map_bins
  ** Invoke deploy:set_rails_env (first_time)
  ** Execute deploy:set_rails_env
  ** Invoke deploy:set_linked_dirs (first_time)
  ** Execute deploy:set_linked_dirs
  ** Invoke deploy:set_rails_env
  ** Invoke deploy (first_time)
  ** Execute deploy
  ** Invoke deploy:starting (first_time)
  ** Execute deploy:starting
  ** Invoke deploy:check (first_time)
  ** Invoke git:check (first_time)
  ** Invoke git:wrapper (first_time)
  ** Execute git:wrapper
  00:00 git:wrapper
        01 mkdir -p /tmp
      ✔ 01 deploy@myserver.com 0.286s
        Uploading /tmp/git-ssh-myapp-staging-localuser.sh 100.0%
        02 chmod 700 /tmp/git-ssh-myapp-staging-localuser.sh
      ✔ 02 deploy@myserver.com 0.277s
  ** Execute git:check
  00:01 git:check
        01 git ls-remote git@gitlab.com:MyApp/myapp.git HEAD
        01 Permission denied (publickey).
        01 fatal: Could not read from remote repository.
        01
        01 Please make sure you have the correct access rights
        01 and the repository exists.
  cap aborted!
  SSHKit::Runner::ExecuteError: Exception while executing as deploy@myserver.com: git exit status: 128
  git stdout: Nothing written
  git stderr: Permission denied (publickey).
  fatal: Could not read from remote repository.
  
  Please make sure you have the correct access rights
  and the repository exists.
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute'
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/runners/parallel.rb:11:in `block (2 levels) in execute'
  
  Caused by:
  SSHKit::Command::Failed: git exit status: 128
  git stdout: Nothing written
  git stderr: Permission denied (publickey).
  fatal: Could not read from remote repository.
  
  Please make sure you have the correct access rights
  and the repository exists.
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/command.rb:99:in `exit_status='
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/netssh.rb:169:in `execute_command'
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:141:in `block in create_command_and_execute'
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:141:in `tap'
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:141:in `create_command_and_execute'
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:74:in `execute'
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/capistrano-3.10.1/lib/capistrano/scm/git.rb:77:in `git'
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/capistrano-3.10.1/lib/capistrano/scm/git.rb:38:in `check_repo_is_reachable'
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/capistrano-3.10.1/lib/capistrano/scm/tasks/git.rake:19:in `block (4 levels) in eval_rakefile'
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:93:in `with'
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/capistrano-3.10.1/lib/capistrano/scm/tasks/git.rake:18:in `block (3 levels) in eval_rakefile'
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:29:in `instance_exec'
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:29:in `run'
  C:/Ruby22/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
  Tasks: TOP =&gt; deploy:check =&gt; git:check
  The deploy has failed with an error: Exception while executing as deploy@myserver.com: git exit status: 128
  git stdout: Nothing written
  git stderr: Permission denied (publickey).
  fatal: Could not read from remote repository.
  
  Please make sure you have the correct access rights
  and the repository exists.
  ** Invoke deploy:failed (first_time)
  ** Execute deploy:failed
  
  
  ** DEPLOY FAILED
  ** Refer to log/capistrano.log for details. Here are the last 20 lines:
  
  
   DEBUG [6b6ba2d0] Finished in 0.471 seconds with exit status 0 (successful).
  
   DEBUG [c6e2d7dc] Running ~/.rvm/bin/rvm 2.2.6 do ruby --version as deploy@myserver.com
  
   DEBUG [c6e2d7dc] Command: ~/.rvm/bin/rvm 2.2.6 do ruby --version
  
   DEBUG [c6e2d7dc]       ruby 2.2.6p396 (2016-11-15 revision 56800) [x86_64-linux]
  
   DEBUG [c6e2d7dc] Finished in 0.608 seconds with exit status 0 (successful).
  
    INFO [fd5500a8] Running /usr/bin/env mkdir -p /tmp as deploy@myserver.com
  
   DEBUG [fd5500a8] Command: /usr/bin/env mkdir -p /tmp
  
    INFO [fd5500a8] Finished in 0.286 seconds with exit status 0 (successful).
  
   DEBUG Uploading /tmp/git-ssh-myapp-staging-localuser.sh 0.0%
  
    INFO Uploading /tmp/git-ssh-myapp-staging-localuser.sh 100.0%
  
    INFO [f33d4873] Running /usr/bin/env chmod 700 /tmp/git-ssh-myapp-staging-localuser.sh as deploy@myserver.com
  
   DEBUG [f33d4873] Command: /usr/bin/env chmod 700 /tmp/git-ssh-myapp-staging-localuser.sh
  
    INFO [f33d4873] Finished in 0.277 seconds with exit status 0 (successful).
  
    INFO [86d3cd5a] Running /usr/bin/env git ls-remote git@gitlab.com:MyApp/myapp.git HEAD as deploy@myserver.com
  
   DEBUG [86d3cd5a] Command: ( export GIT_ASKPASS="/bin/echo" GIT_SSH="/tmp/git-ssh-myapp-staging-localuser.sh" ; /usr/bin/env git ls-remote git@gitlab.com:MyApp/myapp.git HEAD )
  
   DEBUG [86d3cd5a]       Permission denied (publickey).
  
   DEBUG [86d3cd5a]       fatal: Could not read from remote repository.
  
  
  
  Please make sure you have the correct access rights
  
  and the repository exists.
  </code></pre>
  
  <p>I've tried manually running the commands that Capistrano attempts both locally and on my remote server, and they all succeed. I'm really stuck, and any help would be hugely appreciated!</p>