Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 + Capistrano 3 : fatal: Could not read from remote repository while deploying

I am getting following error while deploying Rails 4 application using Capistrano 3

INFO [87512eb8] Running /usr/bin/env chmod +x /tmp/magnificent/git-ssh.sh as [email protected]
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 [email protected]:BoTreeConsultingTeam/magnificent.git as [email protected]
DEBUG [1ec94dd1] Command: ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/magnificent/git-ssh.sh /usr/bin/env git ls-remote --heads [email protected]: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.

Here is capistrano configuration.

config/deploy.rb

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

set :application, 'magnificent'
set :repo_url, '[email protected]: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'

I also copied /home/deploy/.ssh/id_rsa.pub of remote server to github deploy keys.

UPDATE I confirm that I am able to access remote repo and also GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/magnificent/git-ssh.sh /usr/bin/env git ls-remote --heads [email protected]:BoTreeConsultingTeam/magnificent.git command works on remote server.

like image 393
Amit Patel Avatar asked Aug 06 '15 12:08

Amit Patel


3 Answers

Current Solution

Lately I use different solution. Before cap production deploy I run following commands.

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

Previous Solution

I am able to fix this issue by replacing

set :repo_url, '[email protected]:BoTreeConsultingTeam/magnificent.git'

with

set :repo_url, 'https://my_github_username:[email protected]/BoTreeConsultingTeam/magnificent'

Note if your password contains special characters then thet should be url encoded. You can quickly encode using URI::encode in irb.

With other deployments using Capistrano 2, I never need to supply github credentials.

Can anybody please tell why should I have to specify git username/password in repo_url?

There is one more solution in the upcase forum post which also worked.

like image 51
Amit Patel Avatar answered Nov 13 '22 13:11

Amit Patel


This could also happen if you are deploying to the server for the first time, and the git server is not in the known host list of your deployment server.

so, logging into the remote server and then doing a git request to the repository will cause the git server to be added to the known host list.

like so:

git ls-remote [email protected]: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

you should confirm to connect.

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

Now try deploying using the capistrano task.

cap production deploy
like image 3
thanikkal Avatar answered Nov 13 '22 12:11

thanikkal


I had this problem too. Turns out I had loaded the wrong SSH key and it interfered with the deployment. Solved it by removing the wrong loaded ssh key like so:

ssh-add -d ~/.ssh/wrong-ssh-key

and then running the Capistrano deployment again.

like image 2
racl101 Avatar answered Nov 13 '22 12:11

racl101