部署 Rails 应用程序时,Capistrano 3 的 SSH 代理转发不起作用

mas*_*llz 2 ssh capistrano ruby-on-rails ruby-on-rails-4 capistrano3

我的deploy.rb中有以下设置

set :application, 'sample_app'
set :repo_url, 'user@123.45.67.100:/home/user/railsapps/sample_app'
set :deploy_to, '/var/www/sample_app'
set :user, "user"
set :ssh_options, { :forward_agent => true }
Run Code Online (Sandbox Code Playgroud)

和我的部署/生产.rb 文件:

set :stage, :production
server '123.45.67.200', user: 'user', roles: %w{app db web}
Run Code Online (Sandbox Code Playgroud)

当我运行 cap生产部署时出现以下错误:检查

DEBUG [] ssh: connect to host 123.45.67.100 port 22: Connection timed out
DEBUG [] fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as user@123.45.67.200: git exit status: 128
git stdout: Nothing written
git stderr: ssh: connect to host 123.45.67.200 port 22: Connection timed out
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
Run Code Online (Sandbox Code Playgroud)

在其中一行中,我看到它尝试以 user@123.45.67.200 身份访问存储库,这是生产服务器的部署用户:

INFO [] Running /usr/bin/env git ls-remote --heads user@123.45.67.100:/home/user/railsapps/sample_app as user@123.45.67.200
Run Code Online (Sandbox Code Playgroud)

难道不应该说它是以本地用户身份使用本地 ssh 密钥进行连接吗?Capistrano 是否登录到生产服务器,然后从存储库中提取代码?如果是,有没有办法让它将代码从存储库推送到生产服务器?

Kar*_*bur 5

您的 Git URL 似乎无效。您可以通过连接到远程系统 ( user@123.45.67.200) 来测试这一点,并尝试使用简单的命令访问远程 Git 存储库git ls-remote --heads,这将证明连接性。

git ls-remote --heads user@123.45.67.100:/home/user/railsapps/sample_app
Run Code Online (Sandbox Code Playgroud)

我怀疑您可能需要.git附加到您的 URL ( user@123.45.67.100:/home/user/railsapps/sample_app.git),但这实际上取决于您如何设置远程存储库。

Git 确实使用 SSH 进行连接,但它没有在 Capistrano 输出中明确显示这一点。您将看到的只是显式git命令。

或者,如果您希望使用代理转发,那么您的 ssh 转发配置(本地或远程)可能会遇到问题。您可以通过检查本地计算机然后连接到远程计算机并查看您的身份是否被转发来进行测试。你会这样做:

local-host$ ssh-add -l
local-host$ ssh user@remote-host
remote-host$ ssh-add -l
Run Code Online (Sandbox Code Playgroud)

如果您看到如下输出:

git ls-remote --heads user@123.45.67.100:/home/user/railsapps/sample_app
Run Code Online (Sandbox Code Playgroud)

或者:

local-host$ ssh-add -l
local-host$ ssh user@remote-host
remote-host$ ssh-add -l
Run Code Online (Sandbox Code Playgroud)

或者:

Error connecting to agent: No such file or directory
Run Code Online (Sandbox Code Playgroud)

然后您需要先解决该问题,然后 Capistrano 才能按预期工作。

您可以查看这篇文章“ Using ssh-agent with ssh ”来帮助进行 SSH 配置。