5

I am using Capistrano to deploy to a server running Nginx. I'm running into some issues with APC and I need to reload PHP-FPM after Capistrano has completed the deployment. The issue itself is outlined here but like that author I don't want to have to SSH in and reload PHP-FPM remotely from the command line, I'd like Capistrano to do it as a post deployment hook.

The essence of the deploy.rb being used is below;

    set :application, "deploytest"

    set :repository,  "git@bitbucket.org:gitaccount/git-repo.git"
    set :scm,         :git
    set :deploy_via,  :remote_cache
    set :app_webroot, "/public"

    default_run_options[:pty] = true

    desc "Execute Capistrano tasks against Production server."
    task :prod do
        role :web, "123.45.67.89"
        role :app, "123.45.67.89"
        set :env,         "prod"
        set :domain,      "deploy-domain.com"
        set :deploy_to,   "/var/www/vhosts/#{domain}/site"
        set :branch,      "master"
    end

And I can push using the command;

    bundle exec cap prod deploy        

Works great. Boy have I struggled trying to get that command to automatically trigger another command once the deploy is complete.

What I have tried;

Here's a summary of the main approaches;

  1. Creating a new namespace for my task

    namespace :mcnab do
      desc "Running hook post deploy"
      task :fpmreload do
        execute "service php-fpm reload"
      end
    end
    
    after "deploy:create_symlink", "mcnab:fpmreload"
    
  2. Wrapping both tasks in a 'deploy' namespace and using the following command to trigger the hook

    after "deploy:create_symlink", "deploy:fpmreload"
    
  3. Explicitly setting the roles again within the new task

    task :fpmreload do
        role :web, "178.62.13.10"
        role :app, "178.62.13.10"
        on roles(:all) do 
            execute "service php-fpm reload"
        end  
    end
    
  4. Setting the user explicitly

    task :fpmreload do
      on "user@123.45.67.89" do
        execute "service php-fpm reload"
      end
    end
    
  5. Using 'run' instead of execute

    task :fpmreload do
      on "user@123.45.67.89" do
        run "service php-fpm reload"
      end
    end
    

Hrrmph, and about a million variations thereon. I'm really just guessing now, even with verbose error reporting the error messages are not helping much. Just one working example of a deploy.rb file with a simple post deploy hook running a command would be great but I can't find one.

McNab
  • 6,767
  • 4
  • 35
  • 55

1 Answers1

5

This works for me

before :published, :fpm_reload
desc 'Fpm reload'
task :fpm_reload do
  on release_roles :all do |host|
    execute :service, 'php5-fpm', :reload
  end
end

The docs: http://capistranorb.com/documentation/getting-started/flow/

Sander Visser
  • 4,144
  • 1
  • 31
  • 42
  • What an unexpected pleasant surprise after 6 months! Good on you sir. Can I just clarify, I take it `|host|` is the IP address wrapped in double quotes? – McNab May 12 '15 at 13:48
  • 1
    the `host` is an instance of Capistrano::Configuration::Server to get the ip/host you can do `host.hostname` http://www.rubydoc.info/github/capistrano/capistrano/Capistrano/Configuration/Server – Sander Visser May 12 '15 at 14:26