0

In Capistrano 2 the deploy:cold task looks like this:

namespace :deploy do
  task :cold do
    update
    migrate
    start
  end

  task :migrate do
     ...
  end
end

I would like to create a before hook on migrate:

before('deploy:migrate', createdb)

but have it apply only when running deploy:cold and not on anything else that calls migrate. I do not want to redefine deploy:cold.

1
  • "I do not want to redefine deploy:cold" – any reason for that? Adding create_db inside deploy:cold right before migrate would be the most straight-forward solution I can think of. Commented Oct 20 at 9:35

2 Answers 2

2

It's idea as some kind of workaround using boolean flag cold_deploy

namespace :deploy do
  task :_mark_cold do
    set :cold_deploy, true
  end
end

before "deploy:cold", "deploy:_mark_cold"

task :createdb_if_cold do
  createdb if fetch(:cold_deploy, false)
end

before "deploy:migrate", "createdb_if_cold"

Running deploy:cold triggers deploy:_mark_cold, which sets cold_deploy as true

When deploy:cold later calls deploy:migrate, the before hook runs createdb_if_cold, sees the flag and runs createdb

Calling deploy:migrate directly (or via other tasks) will not set this flag, so createdb will be skipped

In this case you don't need to redefine deploy:cold task

May be you can redefine the task? Solution will be much cleaner:

namespace :deploy do
  task :cold do
    update
    createdb # just add one line instead of hooks workarounds
    migrate
    start
  end
end
Sign up to request clarification or add additional context in comments.

Comments

1

It turns out that you can actually nest hooks like this:

before('deploy:cold') do
  before('deploy:migrate', createdb)
end

which answers my question exactly. But keep in mind that you actually want to hook createdb in a different spot rather than at deploy:migrate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.