Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capistrano v3 - Hook to run command post deploy

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,  "[email protected]: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 "[email protected]" do
        execute "service php-fpm reload"
      end
    end
    
  5. Using 'run' instead of execute

    task :fpmreload do
      on "[email protected]" 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.

like image 345
McNab Avatar asked Dec 18 '14 12:12

McNab


1 Answers

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/

like image 101
Sander Visser Avatar answered Nov 06 '22 00:11

Sander Visser