Creating a public symlink using Capistrano

How to create a public symlink that’s shared between releases using Capistrano.

So, I have a folder containing static sitemap files for my site. These sitemaps are generated and saved in my public/sitemap folder. However, I want to keep these sitemap files between deployments.

What I need is a symlink from public/sitemap that’s pointing to shared/sitemap.

I use Capistrano for my deployments, so I need the following task (I renamed my sitemap namespace and folder to better reflect other needs you might have):

after “deploy:update_code”, “my_namespace:symlink”

namespace :my_namespace do
desc “Create symlink”
task :symlink do
run “rm -rf #{release_path}/public/my_folder”
run “mkdir -p #{shared_path}/my_folder”
run “ln -nfs #{shared_path}/my_folder #{release_path}/public/my_folder”
This will remove any existing public/my_folder folder (e.g. if it was deployed from your development repository), create the shared/my_folder folder (if it doesn’t exist), and at last create the symlink from public/my_folder to shared/my_folder.

As easy as that 🙂 Hope you can use it.