deploymentteamcitycapistranomultistage

capistrano ssh connection - doesn't work when ran from cron or teamcity


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.

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.

On the CI server, running "ssh server1", or "ssh deploy@server1" works without issue.

my ci stage looks like this, and again works fine from command line

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 => true

my deploy.rb is very large, but these are the relevant settings

# 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 => true }
set :keep_releases, 20
set :use_sudo, false

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

The cap log shows me this:

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

The same thing seems to happen from a cronjob - however don't have the logs there

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.

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.

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.

Any answer that leads me to the solution will be accepted.

Thanks.


Solution

  • I fixed this by modifying the ssh connection in ssh/config, running ssh-agent with a specific pid, adding environment variables and adding a build step to add the key to the ssh agent in the running build.

    http://petey5king.github.com/2011/12/09/deploying-with-capistrano-from-teamcity.html