Where to place "before" and "after" in a Capistrano recipe?

13,564

Solution 1

I use a setup similar to:

after :deploy, "deploy:update_code", "deploy:pipeline_precompile"
before :deploy, "deploy:finalize_update", "deploy:copy_database_config"

namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  end

  # copy database.yml into project
  task :copy_database_config do
    production_db_config = "/Library/RoRconfig/#{application}.yml"
    run "cp #{production_db_config} #{current_release}/config/database.yml"
    `puts "replaced database.yml with live copy"` 
  end

  task :pipeline_precompile do
    run "cd #{release_path}; RAILS_ENV=production rake assets:precompile"
  end
end

Solution 2

According to capistrano's doc at https://capistranorb.com/documentation/getting-started/before-after/, here's how they suggest for inside and outside of :deploy namespace:

# call an existing task
before :starting, :ensure_user
after :finishing, :notify

# or define in block
namespace :deploy do
  before :starting, :ensure_user do
    #
  end

  after :finishing, :notify do
    #
  end
end

However, be careful not to put these hooks inside your custom capistrano rake file that you import as the load order may make it not exist.

Share:
13,564
Meltemi
Author by

Meltemi

Updated on July 18, 2022

Comments

  • Meltemi
    Meltemi almost 2 years

    Stupid question but we've got a broken Capistrano recipe and I want to verify that we're not using after & before incorrectly?

    Do these before & after tasks belong w/in the :deploy namespace block or outside of it? I see examples of both here.

    This is an excerpt from the problematic deploy.rb:

    namespace :deploy do
      task :start do ; end
      task :stop do ; end
      task :restart, :roles => :app, :except => { :no_release => true } do
        run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
      end
    
      # copy database.yml into project
      task :copy_database_config do
        production_db_config = "/Library/RoRconfig/#{application}.yml"
        run "cp #{production_db_config} #{current_release}/config/database.yml"
        `puts "replaced database.yml with live copy"` 
      end
    
      task :pipeline_precompile do
        run "cd #{release_path}; RAILS_ENV=production rake assets:precompile"
      end
    
      after "deploy:update_code", "deploy:pipeline_precompile"         ### <---
      before "deploy:finalize_update", "deploy:copy_database_config"   ### <---
    end