Like many others I've seen in the Googleverse, I fell victim to the File.exists?
trap, which of course checks your local file system, not the server you are deploying to.
I found one result that used a shell hack like:
if [[ -d #{shared_path}/images ]]; then ...
but that doesn't sit well with me, unless it were wrapped nicely in a Ruby method.
Has anybody solved this elegantly?
I have done that before using the run command in capistrano (which execute a shell command on the remote server)
For example here is one capistrano task which will check if a database.yml exists in the shared/configs directory and link it if it exists.
@knocte is correct that
capture
is problematic because normally everyone targets deployments to more than one host (and capture only gets the output from the first one). In order to check across all hosts, you'll need to useinvoke_command
instead (which is whatcapture
uses internally). Here is an example where I check to ensure a file exists across all matched servers:Note that
invoke_command
usesrun
by default -- check out the options you can pass for more control.In capistrano 3, you can do:
This is nice because it returns the result of the remote test back to your local ruby program and you can work in simpler shell commands.
Inspired by @bhups response, with tests:
May be you want to do is: