Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 383
Meltemi Avatar asked Aug 08 '11 16:08

Meltemi


People also ask

How do you know if Capistrano deploys?

If you are not developing a gem, but simply using Capistrano to deploy an application: Your deployment will be unique to your particular project, based on a combination of various Capistrano gems, configuration, server environment, etc. The only real way to test your deployment is by using a staging environment.


2 Answers

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
like image 119
ezkl Avatar answered Oct 16 '22 18:10

ezkl


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.

like image 1
konyak Avatar answered Oct 16 '22 18:10

konyak