title: capistrano ssh connection - doesn't work when ran from cron or teamcity
body: <p>I've been researching this all day and can't seem to find an answer so am posting here.  We are using capistrano multistage to deploy our ruby on rails app and all is well, until we get to automated deployments.</p>
  
  <p>Right now whenever this is ran interactively there are no issues, the deploy completes just fine.  We are now looking at using ci (Teamcity) to deploy to our staging environment after each successful build.</p>
  
  <p>On the CI server, running "ssh server1", or "ssh deploy@server1" works without issue.</p>
  
  <p>my ci stage looks like this, and again works fine from command line</p>
  
  <pre><code>set :branch, "development"
  set :rails_env, "staging"
  set :user, "deploy"
  
  $:.unshift(File.expand_path('./lib', ENV['rvm_path']))
  require 'rvm/capistrano'
  set :rvm_ruby_string, 'ruby-1.9.2-p290'
  set :rvm_bin_path, "/usr/local/rvm/bin/"
  
  default_run_options[:pty] = true
  ssh_options[:verbose] = :debug 
  default_run_options[:pty] = true 
  
  role :app, "server1"
  role :web, "server1"
  role :utility, "server2"
  role :db,  "server1", :primary =&gt; true
  </code></pre>
  
  <p>my deploy.rb is very large, but these are the relevant settings</p>
  
  <pre><code># Repo Settings
  set :repository,  "git@github.com:myrepo/myrepo.git"
  set :scm, "git"
  set :checkout, 'export'
  set :copy_exclude, ".git/*"
  set :deploy_via, :remote_cache
  
  # General Settings
  default_run_options[:pty] = true
  set :ssh_options, { :forward_agent =&gt; true }
  set :keep_releases, 20
  set :use_sudo, false
  </code></pre>
  
  <p>Under Team City as a final build step I have added a command line task which is simply "cap ci deploy:setup" - as an easier test than a full deploy</p>
  
  <p>The cap log shows me this:</p>
  
  <pre><code>[03:27:38]: [Step 4/10] D, [2011-11-21T03:27:38.103284 #22035] DEBUG -- net.ssh.authentication.session[70ca88]: allowed methods: publickey,password
  [03:27:38]: [Step 4/10] E, [2011-11-21T03:27:38.103328 #22035] ERROR -- net.ssh.authentication.session[70ca88]: all authorization methods failed (tried publickey)
  </code></pre>
  
  <p>The same thing seems to happen from a cronjob - however don't have the logs there</p>
  
  <p>To me this seems like an environment issue as Teamcity and likely cron arent loading my full environment. I've tried specifying my ssh key directly in the cap file, among other things and it does not seem to have any effect.</p>
  
  <p>The other odd thing is that on the remote server I am trying to deploy to, the auth.log shows no attempted connections, so troubleshooting this from the server side doesnt seem to be an option.</p>
  
  <p>So my questions is, how do I get this working?  Any ideas on tests to determine where the issue is, or environment variables I need to set appreciated.</p>
  
  <p>Any answer that leads me to the solution will be accepted.</p>
  
  <p>Thanks.</p>