使用capistrano部署rails项目

时间:2013-10-08 05:16:24

标签: ruby-on-rails deployment capistrano

我想用capistrano部署我的项目。这是我的设置文件:

deploy.rb

require "capistrano/ext/multistage"
require "capistrano_colors"
require "bundler/capistrano"

require "rvm/capistrano"                       # Load RVM"s capistrano plugin.

set :application, "project"
set :copy_exclude, %w(.git .gitignore doc features log spec test tmp Capfile)
#set :shared_children, shared_children + %w(public/uploads)

set :use_sudo, false
set :user, "app"

set :stages, %w(staging production)

namespace :deploy do
  task :start, roles: :app, except: { no_release: true } do
    run "cd #{current_path} && bundle exec unicorn_rails -c config/unicorn.rb -E #{rails_env} -D"
  end

  task :stop, roles: :app, except: { no_release: true } do
    run "kill -KILL -s QUIT `cat #{shared_path}/pids/unicorn.pid`"
  end

  task :restart, roles: :app, except: { no_release: true } do
    stop
    start
  end
end

def confirm
  puts "\n\e[0;36m#{stage}\e[0m\e[0;31m Do you really deploy? (yes/no) \e[0m\n"
  proceed = STDIN.gets rescue nil
  exit unless proceed.chomp! == "yes"
end

对于多级,我创建了两个文件(一个是环境): 部署/ production.rb

server "myserver.net", :app, :web, :db, primary: true
set :rails_env, "production"

set :rvm_type, :user
set :rvm_ruby_string, "ruby-2.0.0-p0"

set :scm, :git
set :repository, 'ssh://xxxx@11.111.111.111:54333/~/git-workspace/myproject.git'
set :deploy_via, :remote_cache

confirm

和暂存的几乎相同。

(正如您所猜测的那样,出于安全考虑,我已更改了项目,服务器名称和端口号)

我第一次执行:

  

捆绑exe cap生产部署:检查

然后:

  

捆绑exe cap生产部署:设置

没有任何问题,在调用deploy命令(bundle exe cap production deploy)时,我收到以下消息:

xxxx-no-MacBook-Air:myproject xxxx$ bundle exe cap deploy
    triggering load callbacks
    triggering start callbacks for `deploy'
  * 2013-10-08 13:43:12 13:43:12 == Currently executing `multistage:ensure'
No stage specified. Please specify one of: staging, production (e.g. `cap staging deploy')
xxxx-no-MacBook-Air: xxxx$ bundle exe cap production deploy
    triggering load callbacks
  * 2013-10-08 13:43:18 13:43:18 == Currently executing `production'

production Do you really want to deploy? (yes/no) 
yes
    triggering start callbacks for `deploy'
  * 2013-10-08 13:43:20 13:43:20 == Currently executing `multistage:ensure'
  * 2013-10-08 13:43:20 13:43:20 == Currently executing `deploy'
  * 2013-10-08 13:43:20 13:43:20 == Currently executing `deploy:update'
 ** transaction: start
  * 2013-10-08 13:43:20 13:43:20 == Currently executing `deploy:update_code'
    updating the cached checkout on all servers
    executing locally: "git ls-remote ssh://xxxx@11.111.111.111:54333/~/git-workspace/myproject.git HEAD"
 Bonjour xxxx                                
xxxx@11.111.111.111's password: 
    command finished in 6010ms
  * executing "if [ -d /u/apps/myproject/shared/cached-copy ]; then cd /u/apps/myproject/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard e000681dc88244f04ac2e82dd2cf8d94bfa9d930 && git clean -q -d -x -f; else git clone -q ssh://xxxx@11.111.111.111:54333/~/git-workspace/myproject.git /u/apps/myproject/shared/cached-copy && cd /u/apps/myproject/shared/cached-copy && git checkout -q -b deploy e000681dc88244f04ac2e82dd2cf8d94bfa9d930; fi"
    servers: ["myserver.net"]
Enter passphrase for /Users/myname/.ssh/id_rsa: 
    [myserver.net] executing command
 ** [myserver.net :: out]  Bonjour xxxx
 ** [myserver.net :: out] xxxx@11.111.111.111's password:
Password: 
 ** [myserver.net :: out]
 ** [myserver.net :: out] Permission denied, please try again.
 ** xxxx@11.111.111.111's password:
Password: 
 ** [myserver.net :: out]
 ** [myserver.net :: out] Permission denied, please try again.
 ** xxxx@11.111.11.111's password:
Password: 
 ** [myserver.net :: out]
 ** [myserver.net :: out] Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
 ** [myserver.net :: out] fatal: The remote end hung up unexpectedly
    command finished in 36598ms
*** [deploy:update_code] rolling back
  * executing "rm -rf /u/apps/myproject/releases/20131008044412; true"
    servers: ["myserver.net"]
    [myserver.net] executing command
    command finished in 1182ms
failed: "rvm_path=$HOME/.rvm $HOME/.rvm/bin/rvm-shell 'ruby-2.0.0-p0' -c 'if [ -d /u/apps/myserver/shared/cached-copy ]; then cd /u/apps/myserver/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard e000681dc88244f04ac2e82dd2cf8d94bfa9d930 && git clean -q -d -x -f; else git clone -q ssh://xxxx@11.111.111.111:54333/~/git-workspace/myproject.git /u/apps/myproject/shared/cached-copy && cd /u/apps/myproject/shared/cached-copy && git checkout -q -b deploy e000681dc88244f04ac2e82dd2cf8d94bfa9d930; fi'" on myserver.net

xxx-no-MacBook-Air:myprojectxxxxx$ 

我已生成密钥(在我的本地环境中)并将公共密钥放在authorized_keys文件(服务器端)中。

1 个答案:

答案 0 :(得分:1)

您似乎采取了许多正确的步骤,所以现在出现这种错误,您应该直接测试权限:

  1. 确保您可以从开发计算机(运行capistrano的位置)ssh到该用户的部署服务器。 (例如,这可能与ssh deploy@myserver.com
  2. 类似
  3. 一旦确定可行,然后在登录服务器时,作为部署用户,请尝试连接到存储库服务器。例如,ssh -T git@github.com as described here
  4. 99%的时间,如果你能成功完成这两件事,就没有权限问题。