2

Trying to use Capistrano 3 to deploy a project and have run into an issue. In one of my tasks I'm trying to change directories and then run composer install.

For the life of me I can't get it to cd into the proper folder.

Right now if I do

set :theme_path, "#{release_path}/web/app/themes/sage"
append :linked_dirs, "web/app/uploads"
append :linked_files, ".env"
set :keep_releases, 3

namespace :deploy do

desc "Build"
after :updated, :build do
    on roles(:app) do
        within release_path do
            execute :composer, "install --no-dev --quiet"
            execute :cd, "#{fetch(:theme_path)}; composer install --no-dev --quiet"
        end
    end
  end

end

It's for some reason running it in the previous release. Doing a pwd instead of the composer install shows that it is inside /current/ and not in the new /release/ directory. So it runs my composer install in current, and then does all the symlinking, so I end up with it always running composer in the previous deploy, so my current deploy never works.

So if release_path is /current/ and not the release currently being deployed... how do I get that path? Or how should I go about running composer install in two places?

Samuel Lelièvre
  • 3,212
  • 1
  • 14
  • 27
Octoxan
  • 1,949
  • 2
  • 17
  • 34

1 Answers1

2

The within syntax is how you tell Capistrano where to execute a command. I am not sure why this isn't working for you. I've copied and pasted your example into one of my apps at it works fine:

namespace :deploy do
  after :updated, :build do
    on roles(:app) do
      within release_path do
        puts capture("pwd")
      end
    end
  end
end

When I run cap production deploy I see the pwd I expect in the output:

/home/deployer/apps/[REDACTED]/releases/20180405162938

You definitely should not be using execute :cd. Use within instead. So for example, if you need to do two executions, one in release_path and another in :theme_path, then I would do the following:

within release_path do
  execute :composer, "install --no-dev --quiet"
end

within fetch(:theme_path) do
  execute :composer, "install --no-dev --quiet"
end
Matt Brictson
  • 10,904
  • 1
  • 38
  • 43