Capistrano - How to put files in the shared folder?

CapistranoConfigWeb Deployment

Capistrano Problem Overview


I am new to Capistranoand I saw there is shared folder and also option :linked_files. I think shared folder is used to keep files between releases. But my question is, how do files end up being in the shared folder?

Also, if I want to symlink another directory to the current directory e.g. static folder at some path, how do I put it at the linked_dirs ?

Lastly how to set chmod 755 to linked_files and linked_dirs.

Thank you.

Capistrano Solutions


Solution 1 - Capistrano

Folders inside your app are symlinks to folders in the shared directory. If your app writes to log/production.log, it will actually write to ../shared/log/production.log. That's how the files end up being in the shared folder.

You can see how this works by looking at the feature specs or tests in Capistrano.

If you want to chmod these shared files, you can just do it once directly over ssh since they won't ever be modified by Capistrano after they've been created.

To add a linked directory, in your deploy.rb:

set :linked_dirs, %w{bin log tmp/backup tmp/pids tmp/cache tmp/sockets vendor/bundle}

or

set :linked_dirs, fetch(:linked_dirs) + %w{public/system}

Solution 2 - Capistrano

Capistrano 3.5+

Capistrano 3.5 introduced append for array fields. From the official docs, you should use these:

For Shared Files:

append :linked_files, %w{config/database.yml}

For Shared Directories:

append :linked_dirs, %w{bin log public/uploads vendor/bundle}

Solution 3 - Capistrano

I've written a task for Capistrano 3 to upload your config files to the shared folder of each of your servers, it'll check these directories in order:

  1. config/deploy/config/:stage/*.yml
  2. config/deploy/config/*.yml

And upload all config files found. It'll only upload the files if they've changed. Note also that if you have the same file on both directories then the second one will be ignored.

Here's the code: https://gist.github.com/Jesus/448d618c83fb0445ebbf

One last thing, this task is just uploading the config. files to your remote shared folder, you still need to set linked_files in config/deploy.rb, eg:

set :linked_files, %w{config/database.yml config/aws.yml}

UPDATE:

If you're using Git, you'll probably want to ignore these files:

echo "config/deploy/config/*" >> .gitignore

Solution 4 - Capistrano

There are 3 simple steps you can follow to put a file that you don't want to change in consecutive releases; add your file to linked_files list.

set :linked_files, fetch(:linked_files, []).push('config.php')

Select all the files that you want to share. Put this file from your local to remote server through scp

scp config.php deployer@amazon:~/capistrano/shared/config.php

Now, deploy through the command given below:

bundle exec cap staging deploy

of course, staging can be changed as per requirements may be production,sandbox etc.

One more thing, because you don't want your team members to commit such files. So, put this file to your .gitignore file. And push it to git remote repo.

Solution 5 - Capistrano

For Capistrano 3.5+, as specified in official doc :

append :linked_dirs, ".bundle", "tmp"

Solution 6 - Capistrano

For me non of the above worked so I ended up adding two functions to the end of the deployment process:

namespace :your_company do
    desc "remove index.php"
    task :rm_files do
        on roles(:all) do
                execute "rm -rf #{release_path}/index.php"
        end
    end
end

namespace :your_company do
    desc "add symlink to index.php"
    task :add_files do
        on roles(:all) do
                execute "ln -sf #{shared_path }/index.php #{release_path}/index.php"
        end
    end
end

after "deploy:finished", "your_company:rm_files"
after "deploy:finished", "your_company:add_files"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFajarmfView Question on Stackoverflow
Solution 1 - CapistranoMichaelView Answer on Stackoverflow
Solution 2 - CapistranoSheharyarView Answer on Stackoverflow
Solution 3 - CapistranoxuusoView Answer on Stackoverflow
Solution 4 - CapistranoAnkit VishwakarmaView Answer on Stackoverflow
Solution 5 - CapistranoElodieView Answer on Stackoverflow
Solution 6 - CapistranoShay ZalmanView Answer on Stackoverflow