Capistrano 3 Execute Within a Directory

Capistrano 3 execute within a directory

Smells like a Cap 3 bug.

I suggest just guaranteeing you are where you want to be from the shell perspective:

execute "cd '#{release_path}'; #{fetch(:composer_command)} install"

Running task before deploy:symlink:shared capistrano 3

I had left this as a comment to my question, but at the moment, it's the only answer I can find, so I'm promoting it to an answer;

OK, so I've determined a possible way to do it, but it seems horrible. Even for an absolute beginner like me:

newreleasedir = capture('ls -t /sites/releases | head -1')

then

execute "cd /sites/releases/#{newreleasedir} && composer install"

Someone please tell me that's horrible and how I should be doing it :)

How to execute a command on the server with Capistrano?

You want to use script/runner. It starts an instance of the app to execute the method you want to call. Its slow though as it has to load all of your rails app.

~/user/mysite.com/current/script/runner -e production FeedEntry.update_all 2>&1

You can run that from the capistrano task.

Capistrano doesn't obey within release_path

There's useful information in another answer here but briefly it seems the problem arises when there are spaces in the command you want to run.

I followed bricker's suggestion, e.g.

within release_path do
execute *%w[ ln -nfs /home/blog/config/database.yml ./database.yml ]
end

Checking directory existence in Capistrano task fails

All Ruby methods that you use in your Capistrano tasks run on your local machine.

For example:

  • File.directory?
  • File.symlink?

These are always evaluated on your local filesystem. Capistrano never runs Ruby code on the remote server. These methods always return false for you because they are trying to find e.g. "#{release_path}/public" on your local computer, which of course does not exist.

To run code on the server, the tools available to you are Capistrano's test and execute methods. These take in a command string that is executed remotely via SSH.

If you want to test if a remote path is a directory, you cannot use Ruby; you have to use something that can be run in a remote shell. Here is one way to test if a path is a directory, for example:

is_directory = test("[ -d #{release_path}/public ]")

Likewise, to test if a path is a symlink:

is_symbolic_link = test("[ -h #{release_path}/public ]")


Related Topics



Leave a reply



Submit