Search code examples
capistrano

What does deploy:initial do in Capistrano task


I use Capistrano for deploy. My Capistrano tasks are almost quoted from many blogs. I often find following structure.

namespace :deploy do
  desc 'Say something before Deploy'
  task :initial do
    on roles(:app) do
      before 'deploy:hoge', 'deploy:bazz'
      invoke 'deploy'
    end
  end

  task :hoge do
    on roles(:app) do
      puts "'hello, world'"
    end
  end

  task :bazz do
    on roles(:app) do
      puts "'goodnight, world'"
    end
  end
end

What does before 'deploy:hoge', 'deploy:bazz' do in task statement? It doesn't display any messages. I think before statement must be outside of task statement.


Solution

  • In Capistrano 3.x at least, there is no such thing as a built-in deploy:initial task. Declaring a task with that name does not do anything special.

    I think before statement must be outside of task statement.

    You are exactly right. Any before and after declarations should be done at the top level and never within a task block.

    As it stands, the example that you gave does nothing. If you want to run a certain task before deploy begins, you would hook into the deploy:starting task, like this:

    before "deploy:starting", "deploy:hoge"
    

    Furthermore, there is nothing special about the deploy namespace. I recommend declaring your own custom tasks in a different namespace, to keep them visually separated. Then you can easily distinguish between a built-in Capistrano task and your custom ones.

    So I would rewrite the example like this:

    namespace :myapp do
      task :hoge do
        on roles(:app) do
          puts "'hello, world'"
        end
      end
    
      task :bazz do
        on roles(:app) do
          puts "'goodnight, world'"
        end
      end
    end
    
    # Invoke hoge and bazz before deployment begins
    before "deploy:starting", "myapp:hoge"
    before "deploy:starting", "myapp:bazz"
    

    The full list of built-in Capistrano task that you can use with before and after can be found here:

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