25

I'm using rails 3.2 with asset and carrierwave for upload some images, they store in /public/uploads/photo/..... but when I do a cap:deploy (with capistrano) my current directory application doesn't contain the files I uploaded, because capistrano make a new version ....

=== Update ===

After all I use this :

inside :deploy namespace

   task :symlink_uploads do
     run "ln -nfs #{shared_path}/uploads  #{release_path}/public/uploads"
   end

and after:

after 'deploy:update_code', 'deploy:symlink_uploads'

=== Re Update ===

The solution of @tristanm is the best way to solve this.

eveevans
  • 4,392
  • 2
  • 31
  • 38

5 Answers5

65

How about this:

# config/deploy.rb
set :shared_children, shared_children + %w{public/uploads}

:shared_children defaults to %w(public/system log tmp/pids) so we're just expanding this list.

EDIT:

Don't forget to run cap deploy:setup after changing :shared_children so that the new targets are created under shared.

EDIT Capistrano 3:

Capistrano 3 uses the linked_dirs setting and doesn't specify public/system as a default anymore.

set :linked_dirs, fetch(:linked_dirs) + %w{public/system public/uploads}

tristanm
  • 3,337
  • 2
  • 27
  • 40
  • It did created the uploads folder inside shared, but the files aren't save there. am I missing a step? – leonel Sep 12 '12 at 15:42
  • 1
    Could be that the symlinks haven't been created. Run `cap deploy` again and check that `releases/current/public/uploads` is a symlink to `shared/uploads`. Also, make sure you're not using a really ancient version of Capistrano. – tristanm Sep 12 '12 at 22:01
  • I think that your solution looks great but really I didn't try that way, so for the amount of UP votes, I will give you the "accept". Thanks – eveevans Sep 13 '12 at 21:30
  • Thanks! Worked, though I had to redeploy whole application from scratch. Simple cap deploy:setup did not work. – kubbing Aug 26 '13 at 23:26
  • 3
    I am using capistrano 3. I only added this line to deploy.rb set :linked_dirs, %w{public/uploads}. Got an error when using the fetch(:linked_dirs) part. – Jepzen Aug 18 '14 at 13:46
5

With Capistrano 3 and without needing to redeploy.

Like @tristanm mentioned add this to your config/deploy.rb

# config/deploy.rb
set :linked_dirs, fetch(:linked_dirs) + %w{public/uploads}

To have capistrano create shared/public/uploads

cap deploy:check:linked_dirs

Now cap can create the symlink

cap deploy:symlink:shared

Finally, if you have backups of the uploads you can put them in shared/public/uploads/ and they should work without needing to redeploy.

Jason R
  • 2,085
  • 20
  • 12
4

Capistrano creates new directory for every deploy.

There are some exceptions to it. For example, the log files are shared between the deployment directories because they are just symlinks. You have to create a symlink for public/uploads as well.

Here is the command:

run <<-CMD
  rm -rf #{latest_release}/public/uploads &&
  ln -s #{shared_path}/uploads #{latest_release}/public/uploads
CMD
Arun Kumar Arjunan
  • 6,827
  • 32
  • 35
0

Go to your app server shared folder and create an uploads directory.

mkdir uploads

In your deploy.rb file insert these codes under deploy namespace

task :symlink_uploads do

run "rm -rf #{latest_release}/public/uploads && ln -nfs #{shared_path}/uploads #{latest_release}/public/uploads"

end

after 'deploy:update_code', 'deploy:symlink_uploads'

Now delete the old files present already as they won't work. Upload a new file and cap deploy your app again. It should work now.

sumitsv21
  • 302
  • 3
  • 7
0

Using Capistrano 3, I just added this line to my config/deploy.rb

set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}

Then, run:

$ cap production deploy
Bruno Paulino
  • 5,611
  • 1
  • 41
  • 40