Rails 4 + Capistrano 3:部署时出现致命错误:无法从远程存储库读取

18

使用 Capistrano 3 部署 Rails 4 应用程序时,我遇到了以下错误:

INFO [87512eb8] Running /usr/bin/env chmod +x /tmp/magnificent/git-ssh.sh as deploy@104.236.6.180
DEBUG [87512eb8] Command: /usr/bin/env chmod +x /tmp/magnificent/git-ssh.sh
INFO [87512eb8] Finished in 0.444 seconds with exit status 0 (successful).
INFO [1ec94dd1] Running /usr/bin/env git ls-remote --heads git@github.com:BoTreeConsultingTeam/magnificent.git as deploy@104.236.6.180
DEBUG [1ec94dd1] Command: ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/magnificent/git-ssh.sh /usr/bin/env git ls-remote --heads git@github.com:BoTreeConsultingTeam/magnificent.git )
DEBUG [1ec94dd1]    ERROR: Repository not found.
DEBUG [1ec94dd1]    fatal: Could not read from remote repository.
DEBUG [1ec94dd1]    
DEBUG [1ec94dd1]    Please make sure you have the correct access rights
DEBUG [1ec94dd1]    and the repository exists.

这是 Capistrano 的配置文件。

config/deploy.rb

# config valid only for current version of Capistrano
lock '3.4.0'

set :application, 'magnificent'
set :repo_url, 'git@github.com:BoTreeConsultingTeam/magnificent.git'
set :deploy_to, '/home/deploy/magnificent'

set :linked_files, %w{config/database.yml config/secrets.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :branch, 'develop' #set/ :branch,`git rev-parse --abbrev-ref HEAD`.chomp
set :ssh_options, { forward_agent: true }

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      execute :touch, release_path.join('tmp/restart.txt')
    end
  end

  after :publishing, 'deploy:restart'
  after :finishing, 'deploy:cleanup'
end

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

set :stage, :production
server 'xx.xx.xx.xx', user: 'deploy', roles: %w{web app}

Capfile

require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
set :rvm_type, :user
set :rvm_ruby_version, '2.2.2'

我还将远程服务器的/home/deploy/.ssh/id_rsa.pub复制到了Github部署密钥中。

更新:确认我能够访问远程仓库,并且GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/magnificent/git-ssh.sh /usr/bin/env git ls-remote --heads git@github.com:BoTreeConsultingTeam/magnificent.git命令在远程服务器上也可以工作。

7个回答

63

当前解决方案

最近我使用了不同的解决方案。在运行cap production deploy之前,我运行以下命令。

  1. eval "$(ssh-agent -s)"
  2. ssh-add ~/.ssh/id_rsa

以前的解决方案

我曾经通过替换

来解决这个问题。
set :repo_url, 'git@github.com:BoTreeConsultingTeam/magnificent.git'

使用
set :repo_url, 'https://my_github_username:my_github_password@github.com/BoTreeConsultingTeam/magnificent'

注意:如果您的密码包含特殊字符,则应进行URL编码。您可以在irb中快速使用URI :: encode进行编码。

使用Capistrano 2进行其他部署时,我从未需要提供github凭据。

请问为什么我必须在repo_url中指定git用户名/密码?

upcase论坛帖子中还有另一种解决方案也行得通。


我本来不会实现原始解决方案,但你的更新确实帮助我解决了问题。 - MTarantini
1
它能工作,但这很糟糕,将其提交到Git是非常糟糕的安全做法。 - James Tan

3

如果您第一次部署到服务器,且git服务器不在部署服务器的已知主机列表中,则可能会出现此问题。

因此,登录到远程服务器,然后对存储库执行git请求将导致将git服务器添加到已知主机列表中。

操作如下:

git ls-remote git@bitbucket.org:your_gitbucket_user_id/your_repo.git master

The authenticity of host 'bitbucket.org (104.192.143.2)' can't be established.
RSA key fingerprint is SHA256:zzXQOXSRBEiUtuE8AikJYKwbHaxvSc0ojabwzha.
Are you sure you want to continue connecting (yes/no)? yes

您需要确认连接。

Warning: Permanently added 'bitbucket.org,104.192.143.2' (RSA) to the list of known hosts.

现在尝试使用Capistrano任务进行部署。

cap production deploy

3
我也遇到了这个问题。原来是我加载了错误的SSH密钥,导致部署受到干扰。通过以下方式移除错误的SSH密钥解决了这个问题: ssh-add -d ~/.ssh/wrong-ssh-key 然后重新运行Capistrano部署。

1

我曾经遇到过类似的问题(在Ubuntu系统中)。ssh-agent应该是开启的。你可以将其添加到启动应用程序中。


是的,那也可以正常工作。最近我开始在部署之前使用 ssh-agent。已更新我的答案。 - Amit Patel

0

前往服务器框获取id_rsa.pub并将其添加到Github的密钥中


0

我曾经因为用户/仓库路径中的一个错别字(破折号而不是下划线)而遇到了同样的问题。三次检查路径!

git ls-remote git@bitbucket.org:your_gitbucket_user_id/your_repo.git master
# ---------------------------------------------------------^
# => fatal: Could not read from remote repository.

git ls-remote git@bitbucket.org:your_gitbucket_user_id/your-repo.git master
# ---------------------------------------------------------^
# => Success!

0
一个常见的错误是您可能需要将服务器的公钥添加到仓库的访问密钥列表中。我总是忘记这一点,而是添加了本地环境的公钥,但从未奏效。

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