Capistrano deploy fails after I changed the repository URL

RubyDeploymentCapistrano

Ruby Problem Overview


I have a simple deployment via capistrano from a Git repository. At first I was deploying form GitHub, everything worked just fine. But then I moved my repository to BitBucket and now I'm getting

fatal: Could not parse object '9cfb...'.

The problem goes away once I change

set :deploy_via, :remote_cache

to

set :deploy_via, :copy

but that doesn't fix the problem, it only bypasses it. Is there any way I can tell capistrano to just drop the old cache?

Ruby Solutions


Solution 1 - Ruby

Capistrano 2.X

Delete and re-clone the repo using the new address:

cd $deploy_to/shared
rm -rf cached-copy
git clone ssh://[email protected]/new/repo.git cached-copy

Modify your config/deploy.rb to use the new repo:

set :repository, "ssh://[email protected]/new/repo.git"
set :scm, :git
set :deploy_via, :remote_cache

Deploy again:

cap deploy

Capistrano 3.X

  1. Remove the $deploy_to/repo directory
  2. Modify your config/deploy.rb (same as 2.X)
  3. cap deploy

Solution 2 - Ruby

I gotta say I’m not sure, since I haven’t been able to test this but this should work:

cap deploy:cleanup -s keep_releases=0

Since it wipes every release (cache) from the server.

Apparently you will also need to remove shared/cached-copy, because this doesn’t seem to be cleaned by the Capistrano call above according to the comment below.

Solution 3 - Ruby

Capistrano 2 and below

SSH to your server and update the repo in ./shared/cached-copy/.git/config of the deployment folder, or just remove the ./shared/cached-copy

Capistrano 3 and above

SSH to your server and update the repo in ./repo/config of the deployment folder.

Check Fixing Capistrano 3 deployments after a repository change

Solution 4 - Ruby

I solved this with the following in deploy.rb:

namespace :deploy do
  task :cope_with_git_repo_relocation do
    run "if [ -d #{shared_path}/cached-copy ]; then cd #{shared_path}/cached-copy && git remote set-url origin #{repository}; else true; fi"
  end
end
before "deploy:update_code", "deploy:cope_with_git_repo_relocation"

It makes deploys a little slower, so it's worth removing once you're comfortable that all your deploy targets have caught up.

Solution 5 - Ruby

You need to change git origin in your /shared/cached-copy folder

cd /var/www/your-project/production/shared/cached-copy
git remote remove origin
git remote add origin [email protected]:/origin.git

try cap production deploy

Solution 6 - Ruby

The most simple way is just changing the repo url to the new one in .git/config in the shared/cached-copy directory on the webserver. Then you can do a normal deploy as usual.

Solution 7 - Ruby

Depends on your version Capistrano 3 is different from it's older ancestors:

Read my original answer here and how to fix similar issues https://stackoverflow.com/questions/16764750/capistrano-error-when-change-repository-using-git/23863673#23863673

Solution 8 - Ruby

If you need to do a lot of repo's you might want to add a task for it.

For capistrano 3 you add this task in your deploy.rb

desc "remove remote git cache repository"
  task :remove_git_cache_repo do
      on roles(:all) do
    execute "cd #{fetch(:deploy_to)} && rm -Rf repo"
  end
end

And then run it once for every stage:

cap testing remove_git_cache_repo

Solution 9 - Ruby

Here's the Capistrano 3 version of what this answer talks about. It might be tedious to do what the answer suggests on each server.

So drop this in deploy.rb and then run cap <environment> deploy:fix_repo_origin

namespace :deploy do
  desc 'Fix repo origin, for use when changing git repo URLs'
  task :fix_repo_origin do
    on roles(:web) do
      within repo_path do
        execute(:git, "remote set-url origin #{repo_url}")
      end
    end
  end
end

Solution 10 - Ruby

For Capistrano 3.0+

  1. Change the repository URL in your config/deploy.rb

  2. Change the repository URL in the your_project/repo/config file on the server.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJakub ArnoldView Question on Stackoverflow
Solution 1 - RubyJustin TannerView Answer on Stackoverflow
Solution 2 - RubyrobustusView Answer on Stackoverflow
Solution 3 - RubyGuilherme ViebigView Answer on Stackoverflow
Solution 4 - RubysheldonhView Answer on Stackoverflow
Solution 5 - RubyTim KozakView Answer on Stackoverflow
Solution 6 - RubySævarView Answer on Stackoverflow
Solution 7 - Rubyuser1553777View Answer on Stackoverflow
Solution 8 - RubymipmipView Answer on Stackoverflow
Solution 9 - RubyErJabView Answer on Stackoverflow
Solution 10 - RubyNataliya LogunkovaView Answer on Stackoverflow