75

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?

Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327

10 Answers10

106

Capistrano 2.X

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

cd $deploy_to/shared
rm -rf cached-copy
git clone ssh://git@example.org/new/repo.git cached-copy

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

set :repository, "ssh://git@example.org/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
Justin Tanner
  • 14,062
  • 17
  • 82
  • 103
  • 5
    I'd say this is the better answer. If you delete all releases you are essentially in bad shape if your new deploy fails and you cannot roll back. I also think it is worth noting that cloning the repo manually is only needed to add the host to the known hosts. If it already is added then the deploy will work (so essentially the same thing as when setting up capistrano for the first time, checking out the repo anywhere works just to get it added) – Kenny Lövrin Jul 17 '13 at 09:10
  • @KennyLövrin yes cloning the repo anywhere will get your host into the known hosts, but aif you have `:remove_cache` set you need something in the cached-copy directory or capistrano won't deploy. – Justin Tanner Jul 18 '13 at 13:48
  • 7
    Keep in mind that capistrano 3.1 uses `shared/repo` instead of `shared/cached-copy` so this answer, while almost correct, should be updated. – Fred Oliveira Feb 04 '14 at 15:54
  • 4
    In capistrano 3.2.1 I solved removing the `$deploy_to/repo` folder since I cannot found any `repo` folder in `shared`. – Roxas Shadow Oct 03 '14 at 10:30
  • where are all these directories? I'm having a similar problem in a Rails 3.2 app and have no idea what's going on. I develop on OS X and have no trouble deploying but my coworker, who is using a nitrous.io linux box, is getting errors on deploy that the repo can't be found despite the git remote being set and the master branch being up to date – sixty4bit Dec 11 '14 at 22:12
  • 1
    This is the better answer – Brettski Sep 17 '15 at 02:47
  • It's not necessary to delete the entire `repo` folder, just edit `repo/config` and change the repo URL to the new one. – ppires Oct 08 '15 at 18:19
  • I was able to get by without, git clone ssh://git@example.org/new/repo.git cached-copy, working solution, though thanks – a2f0 Sep 19 '17 at 04:47
45

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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
robustus
  • 3,626
  • 1
  • 26
  • 24
  • 65
    This didn't solve the problem entirely, but once I removed `shared/cached-copy`, it deploys just fine now. – Jakub Arnold Dec 03 '11 at 10:56
  • It did not work for me, either, but Jakub's comment here did. – JD. Oct 10 '12 at 20:22
  • 5
    you can just change the url of origin remote to your new repo address in the `shared/cached-copy` directory. – nil Dec 06 '12 at 08:57
  • 2
    the answer only delete all releases on server (including current). The best answer (if you have remote cache active) is that of Jakub Arnold. – eveevans Aug 08 '13 at 22:25
  • [This link](https://coderwall.com/p/4k1lja/fixing-capistrano-3-deployments-after-a-repository-change) Worked, shared/cached-copy and deploy:cleanup doesn't work with capistrano 3 – hemc4 Jan 08 '15 at 10:41
  • add jakub comment to the answer, it solves the issue – Rodrigo Zurek Feb 12 '15 at 17:07
  • 7
    Since Capistrano 3 you have to delete `/repo` instead of `shared/cached-copy`. Edit: See [Justin Tanner's answer](http://stackoverflow.com/a/17618176/727341) – csch Aug 03 '15 at 09:39
  • Justin Tanner's answer doesn't require removing old releases and is simpler. – ObjectNameDisplay Oct 19 '16 at 17:21
  • This is quite dangerous if your deploy fails, since no rollback will be possible and you'll be crying at a blank screen where your website was. (empirically tested) – gl03 Feb 08 '17 at 17:58
15

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

Yule
  • 9,668
  • 3
  • 51
  • 72
Guilherme Viebig
  • 6,901
  • 3
  • 28
  • 30
  • 1
    nice solution. let's say that `./repo/config` -> `path_to_your_repo/repo/config` (it's not clear for beginning) – mmike Sep 20 '17 at 13:18
6

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.

sheldonh
  • 2,684
  • 24
  • 31
3

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 git@bitbucket.org:/origin.git

try cap production deploy

Tim Kozak
  • 4,026
  • 39
  • 44
0

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.

Sævar
  • 1,602
  • 11
  • 18
0

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

Read my original answer here and how to fix similar issues Capistrano error when change repository using git

Community
  • 1
  • 1
user1553777
  • 241
  • 3
  • 7
0

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
mipmip
  • 1,072
  • 1
  • 10
  • 24
0

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
ErJab
  • 6,056
  • 10
  • 42
  • 54
-1

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.