Remote file dependencies in Capistrano

Rake's File Tasks allow us to define steps to create a file and to add the File Task as a prerequisite. Capistrano extends this idea using Remote File Tasks, allowing the existence of a remote
file to be set as a dependency. This is especially useful when deploying an application that depends on configuration files that are not checked in to the repo.

First define the Remote File Task, passing the name of the file, File Task and the roles to check against.

remote_file 'config/newrelic.yml' => '/tmp/newrelic.yml', roles: :app

file '/tmp/newrelic.yml' do |t|
  sh "curl -o #{t.name} https://rpm.newrelic.com/accounts/xx/newrelic.yml"
end

Capistrano assumes that the name of the file is relative to shared_path, so in this example /var/www/cap-test/shared/config/newrelic.yml

When the Remote File Task is executed, it checks for the existence of the file on the roles specified. If the file is missing it will execute the File Task '/tmp/newrelic.yml' and upload the resulting file.

When using the standard deploy tasks, check:linked_files verifies that any files specified in the :linked_files variable exist, exiting the deploy if files are missing. By adding the 'config/newrelic.yml' task as a prerequisite of check:linked_files, if the file is missing it will be created and uploaded.

namespace :deploy do
  namespace :check do
    task :linked_files => 'config/newrelic.yml'
  end
end

If the file exists on the hosts specified, then the task will not run.