Rails:Capistrano。权限被拒绝(公钥)

6
我一直在寻找使用capistrano部署应用程序的方法。 我目前在Github上托管一个小型的私有repo和一个本地服务器以尝试部署我的测试。但是我遇到了以下问题和错误信息。
我已经完成以下步骤:
1.在服务器上生成ssh密钥,并成功将其添加到库中的部署密钥中并测试(git@github.com);
2.在客户端上生成ssh密钥,并成功将其添加到库中的部署密钥中;
3.设置了一个私有仓库。 并且有一个名为deployer的帐户具有部署权限;
4.配置了deploy.rb和production rb,以模仿其他模板。
我仍然不能确定为什么会出现这样的错误。
    DEBUG [a5554d3d] Command: /usr/bin/env chmod +x /tmp/App/git-ssh.sh
    INFO [a5554d3d] Finished in 0.020 seconds with exit status 0 (successful).
    INFO [b1517df1] Running /usr/bin/env git ls-remote --heads git@github.com:aceofw
    ings/App.git as deploy@192.168.1.84
    DEBUG [b1517df1] Command: ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/App/git-ssh
    .sh /usr/bin/env git ls-remote --heads git@github.com:aceofwings/App.git )

    DEBUG [b1517df1]        Permission denied (publickey).
    DEBUG [b1517df1]        fatal: Could not read from remote repository.
    DEBUG [b1517df1]
    DEBUG [b1517df1]        Please make sure you have the correct access rights
    DEBUG [b1517df1]        and the repository exists.
    (Backtrace restricted to imported tasks)
    cap aborted!
    SSHKit::Runner::ExecuteError: Exception while executing as deploy@192.168.1.84:
    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.

    SSHKit::Command::Failed: git exit status: 128
    git stdout: Nothing written

Deploy.rb文件
   ###############Deploy.rb##################
# config valid only for current version of Capistrano
lock '3.4.0'

set :repo_url, 'git@github.com:aceofwings/App.git'
set :application, 'App'
set :user, 'deploy'
#set :pty, true
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp



# Default value for linked_dirs is []
# set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')


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


Production.rb

server '192.168.1.84', user: 'deploy', roles: %w{app db web}

#set :stage, :production
 role :app, %w{deploy@192.168.1.84}
 role :web, %w{deploy@192.168.1.84}
 role :db,  %w{deploy@192.168.1.84}


  set :ssh_options, {
    forward_agent: false,
    auth_methods: %w(password),
    password: 'Deploy4Real',
    user: 'deploy'
  }

当您登录到“deploy”用户时,您可以执行“ssh -T git@github.com”吗? - smallbutton
是的,我可以。我也可以在我的本地机器上执行相同的命令。成功返回。 - MooCow
在服务器上的deploy用户上运行git ls-remote --heads git@github.com:aceofwings/App.git,这个怎么样? - smallbutton
那也通过了。非常奇怪,这种情况发生了。 - MooCow
这个命令引用了另一个存储库,感觉有点奇怪:INFO [b1517df1] Running /usr/bin/env git ls-remote --heads git@github.com:aceofwings/RotairApp.git。是应用程序从RotairApp重命名为App或者其他什么原因吗? - smallbutton
现在只是一个应用程序,以前是RotairApp。抱歉让您感到困惑。最好的做法是尝试查看服务器,或者重新进行整个过程。 - MooCow
3个回答

15

我遇到了类似的问题。结果发现SSH代理未运行。查看Capistrano文档后我得知这个问题:http://capistranorb.com/documentation/getting-started/authentication-and-authorisation/

运行以下命令:

$ ssh-add -l

显示我的公钥未添加到代理中,所以我需要手动添加:

$ ssh-add

身份验证信息已添加:/Users/me/.ssh/id_rsa (/Users/me/.ssh/id_rsa)

之后我的Capistrano部署就可以工作了。


3

您应该在production.rb中设置:

forward_agent: true

我曾遇到过使用Capistrano 3.4出现的同样问题。但当我使用Capistrano 3.2.1时,它似乎会忽略此参数。

你可以使用capistrano-ssh-doctor来解决可能存在的配置问题。


0

我认为你的Capistrano可能在其Git缓存中有一些旧文件,可能是由于重命名存储库或类似情况引起的。尝试删除服务器上shared文件夹中的cached-copy文件夹,这样Capistrano将再次拉取您的存储库。


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