Capistrano bitbucket - 权限被拒绝 (publickey)

10

我正在尝试使用Capistrano将我的应用程序部署到DigitalOcean服务器。

这不是我第一次使用Capistrano在DigitalOcean上配置RoR服务器进行部署,所以我很困惑;我没有改变我的工作流程。

这是我的Capistrano配置文件:

require 'bundler/capistrano'
require 'rvm/capistrano'

set :application, "foobar"
set :repository,  "git@bitbucket.org:sergiotapia/foobar.git"
set :ping_url, "http://192.168.1.1/"
set :scm, :git
set :scm_verbose, true
default_run_options[:pty] = true

set :user, "sergiotapia" # The user on the VPS server.
set :password, "hunter2"
set :use_sudo, false
set :deploy_to, "/home/sergiotapia/www/#{application}"
set :deploy_via, :remote_cache
set :keep_releases, 1
set :rails_env, "production"
set :migrate_target, :latest

role :web, "192.168.1.1"
role :app, "192.168.1.1"

namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, roles: :app, except: { no_release: true } do
    run "sudo touch #{File.join(current_path,'tmp','restart.txt')}"
  end
end

# Add this to add the `deploy:ping` task:
namespace :deploy do
  task :ping do
    system "curl --silent #{fetch(:ping_url)}"
  end
end

namespace :gems do
  task :bundle, :roles => :app do
    run "cd #{release_path} && bundle install --without development && rake db:migrate RAILS_ENV=production"
  end
end

after "deploy:update_code", "gems:bundle"

# Add this to automatically ping the server after a restart:
after "deploy:restart", "deploy:ping"

当运行cap deploy:setupcap deploy:check时,一切都顺利(工作正常)。

但在实际的cap deploy命令中失败了。

** [192.168.1.1 :: out] Enter passphrase for key '/home/sergiotapia/.ssh/id_rsa':
Password: 
** [192.168.1.1 :: out]
** [192.168.1.1 :: out] Permission denied (publickey).
** [192.168.1.1 :: out]
** [192.168.1.1 :: out] fatal: Could not read from remote repository.
** [192.168.1.1 :: out]
** [192.168.1.1 :: out]
** [192.168.1.1 :: out] Please make sure you have the correct access rights
** [192.168.1.1 :: out]
** [192.168.1.1 :: out] and the repository exists.
** [192.168.1.1 :: out]

我已经将我的id_rsa.pub文件添加到BitBucket,并确保使用ssh-add -l命令将其添加到我的SSH代理中。甚至从远程服务器测试SSH也可以正常工作:
sergiotapia@tappia:~/www$ ssh -T git@bitbucket.org
logged in as sergiotapia.

You can use git or hg to connect to Bitbucket. Shell access is disabled.

为什么我无法访问BitBucket上的代码库?

Capistrano是否以除sergiotapia之外的用户身份运行?这可能是原因吗?


1
我也有这个问题!不过,我发现如果我的ssh密钥没有密码,我可以根据需要部署。 (对于某些机器来说,这是一种可容忍的解决方案。) - JellicleCat
在 Windows 上部署 Laravel 应用程序遇到了与在 OSX 上不同的问题。在 OSX 上运行正常。 - hamishtaplin
2个回答

9

请确保将您的ssh密钥添加到身份验证代理中:

ssh-add ~/.ssh/id_rsa

并且在 deploy.rb 文件中确保

ssh_options[:forward_agent] = true

编辑: 如果您在重新启动时丢失ssh-add配置,则应执行以下操作:
从macOS Sierra 10.12.2开始,苹果添加了一个名为UseKeychain的ssh_config选项,可解决此问题。将以下内容添加到您的~/.ssh/config文件中:
Host *
   AddKeysToAgent yes
   UseKeychain yes 

在当前版本中,我们应该在 deploy.rb 文件中添加 set :ssh_options, { forward_agent: true } - MaciekR

3
  1. 您可以在:app服务器上设置SSH代理。
  2. 设置无需密码的密钥以在:app服务器和Bitbucket之间进行通信。
  3. 将deploy_via更改为::deploy_via,:copy(不需要部署服务器检出文件,但可能较慢。)

@electrawn,我们遇到了同样的问题。所有这些步骤都需要实施来解决问题吗?还是只需要步骤1或步骤2就足够了? - user938363

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接