Stop uploaded files getting ‘deleted’ with Capistrano
Here’s how to stop your site’s uploads or other static file directories disappering after doing a ‘cap deploy’ with Capistrano. Capistrano is actually behaving exactly as it should. The problem is that the ‘uploads’ directory is being left behind in release purgatory while the other files happily move on up to the castle in the sky that is the current release.
The solution is to have all uploads stored in a directory outside of the release system. As usual the smart folks who made Capistrano are way ahead of us, if you do a ‘ls’ in your apps path (on your remote machine) you’ll see four directories
‘cd’ into shared/system (cd shared/system) and create an uploads directory (mkdir uploads). Next create a Capistrano task in config/deploy.rb (on your local machine) that symlinks the public uploads directory in the latest release to the shared/system/uploads directory. The example below would be the task for using Capistrano on Media Temple’s Grid Server …
task :after_update_code, :roles => :app do
run "ln -nfs /home//containers/rails//shared/system/uploads /public/uploads"
end
That’s it! Your uploads are now release agnostic.




Like this post? subscribe to the feed.






THANKS MAN!
Comment by marius — December 23, 2007 @ 1:59 pm
[…] Um das zu verhindern (hier gefunden) muss man im meinprojekt/shared Verzeichnis auf dem Server wo man es deployed hat die Verzeichnisse erstellen, die solche wichtigen Sachen enthalten. […]
Pingback by “Capistrano überschreibt meine Dateien immer!” at funkensturm. | Blog — December 23, 2007 @ 2:12 pm
I can’t seem to get this to work… Is there any reason why copying all of my files to the new release directory wouldn’t work (it doesn’t…)? How would I go about making sure the symlink is actually being created there on the server? After deploying, the files are still ending up in the release directory and not the shared/system folder ;(
It’s rather late right now, so advanced apologies if this doesn’t make any sense.
Comment by Greg — January 12, 2008 @ 4:31 am
I can’t seem to get this to work either. My problem is similar to Greg’s. Is there anything else that needs to be done? Permissions? Do I need to svn ignore the local uploads directory?
Comment by David — March 4, 2008 @ 6:30 pm
Hey David, you’re right about setting svn ignore on the local uploads directory. I should have mentioned that for every site I set up right after the initial svn check in I set all ‘dynamic’ directories to be ignored. Hopefully that will fix it for you.
Comment by Alastair — March 4, 2008 @ 7:35 pm
hi David,
thanks for the info - between this and the capistrano source I figured out how to make my assets directory persistent.
for anyone stumbling in, the file callbacks.rb in capistrano 2.4.3 suggests that tasks named ‘after_xxx’ and ‘before_xxx’ aren’t recommended - instead, the before and after methods should be used.
so, here’s my task for symlinking an assets directory;
namespace :deploy do
#desc “Symlink assets directory from latest release (*)”
task :finalise_update_assets, :except => { :no_release => true } do
run “ln -s #{shared_path}/assets #{latest_release}/public/assets”
end
end
after “deploy:finalize_update”, “deploy:finalise_update_assets”
Comment by Greg H — July 6, 2008 @ 6:49 am