Aston J

How to get Capistrano to ignore upload directories (Carrierwave)

Posted on: June 23rd, 2012 by AstonJ 5 Comments

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
/public/uploads

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! :D

Tags: , ,

5 Responses

  1. Amazing ! You are the chosen one ! Works as a charm.

  2. rusllonrails says:

    Simple and Nice solution. It works.
    Thanks you.

  3. Alexander says:

    Thanks you! Very useful solution.

  4. marvin says:

    Thanks for posting a comment on Simone’s blog referencing this entry. This is more current and way easier.

  5. xofred says:

    Thank you! Nice solve!

Leave a Reply