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

$ ls
current  releases  revisions.log  shared

‘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/#{site}/containers/rails/#{application}/shared/system/uploads #{release_path}/public/uploads"
end

That’s it! Your uploads are now release agnostic.

This entry was posted in Capistrano, Media Temple, Ruby on Rails. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

5 Comments

  1. marius
    Posted December 23, 2007 at 1:59 pm | Permalink

    THANKS MAN!

  2. Greg
    Posted January 12, 2008 at 4:31 am | Permalink

    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.

  3. David
    Posted March 4, 2008 at 6:30 pm | Permalink

    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?

  4. Posted March 4, 2008 at 7:35 pm | Permalink

    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.

  5. Greg H
    Posted July 6, 2008 at 6:49 am | Permalink

    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”

2 Trackbacks

  1. [...] 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. [...]

  2. By Diigo’ the week (weekly) » The Bipeds’ Monitor on February 28, 2009 at 4:54 pm

    [...] Vixiom Axioms » Stop uploaded files getting ‘deleted’ with Capistrano [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word