How to get Capistrano to ignore upload directories (Carrierwave)

As you probably know, Capistrano creates a fresh directory for your app every time you deploy a new version. So if you want some directories (or files) to be carried through to each version, such as files uploaded by users, then we just need to tell Capistrano that they are shared – and to use the /shared directory for these files instead. Which is done by creating symbolic links. Here’s how.

Add this to your deploy.rb file:

set :shared_children, shared_children + %w{public/uploads}

That will create the symbolic link. So all files uploaded to current/public/uploads actually get stored (and called from) the shared/uploads directory.

Then run deploy:setup to set it up:

cap deploy:setup

You have to make sure these directories are not tracked by Git. Which is easy enough you just add them to the .gitignore file:

# Ignore uploaded files

However, if Git is already tracking (or has previously tracked) the directory, you will need to remove it, with:

git rm -r --cached public/uploads

The –cached option doesn’t touch your working directory (so public/uploads will remain there).

That’s it! 😀