Search code examples
rubycapistranocapistrano3

Capistrano deploy.rb can't access task defined in stage file


I'm trying to put together a capistrano recipe for my app that basically clones the git repo locally, does some build processing and then rsyncs to the remote server.

I have 2 environments - dev and prod:

deploy.rb
deploy/dev.rb
deploy/prod.rb

I'm getting this error:

$ cap dev deploy 
(Backtrace restricted to imported tasks)
cap aborted!
Don't know how to build task 'stop_server'

Tasks: TOP => dev
(See full trace by running task with --trace)

Why does the deploy not know how to build task stop_server if it's in the same namespace (deploy) in dev.rb?

Capfile

# Load DSL and set up stages
require 'capistrano/setup'

# Include default deployment tasks
#require 'capistrano/deploy' # COMMENTED OUT B/C I'M TRYING TO BUILD LOCALLY INSTEAD OF DOING A GIT CLONE ON THE REMOTE SERVER

# Load custom tasks from `lib/capistrano/tasks' if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

deploy.rb

# config valid only for current version of Capistrano
lock '3.3.5'

set :repo_url, 'mygiturl'

# Default value for :pty is false
set :pty, true

set :deploy_dir, Dir.pwd
set :tmp_dir, "#{fetch(:deploy_dir)}/tmp"
set :output_dir, "#{fetch(:deploy_dir)}/output"

namespace :deploy do

  desc 'Kick off the deploy'
  task :init do
    invoke 'deploy:create_tmp_dir'
  end

  ... other tasks...


  after :create_tmp_dir, :fetch_code
  after :fetch_code, :build
  after :build, :move_output
  after :move_output, :stop_server

end

desc 'Deploy a new release'
task :deploy do
  invoke 'deploy:init'
end

dev.rb

role :app, fetch(:application)
role :web, fetch(:application)
role :db,  fetch(:application)

set :application, 'myapp'
set :env, 'dev'
set :ip, '123.456.78.901'
set :user, 'myuser'

set :deploy_to, '/var/www/myapp'

set :ssh_options, {
  user: fetch(:user),
  keys: %w(~/.ssh/id_rsa),
  forward_agent: true,
  auth_methods: %w(publickey),
  port: 22
}


namespace :deploy do

  desc "Stops the node forever server"
  task :stop_server do
    on roles(:app) do
      puts '**** STOPPING THE NODE SERVER *****'
      execute 'sudo /etc/init.d/myapp stop; true' # The "; true" ignores any error that may occur if there is no forever process running
    end
  end

  desc "Restarts the forever server"
  task :start_server do
    on roles(:app) do
      puts '**** STARTING THE NODE SERVER *****'
      execute 'sudo /etc/init.d/myapp start'
    end
  end

end

Solution

  • The problem is that Capistrano initializes by loading deploy.rb first; then it loads dev.rb.

    At the time that Capistrano parses this line:

    after :move_output, :stop_server
    

    It does not know what :stop_server refers to (since it hasn't loaded dev.rb yet). Hence the error message you are seeing:

    Don't know how to build task 'stop_server'
    

    One easy workaround is to declare an empty :stop_server task in deploy.rb.

    namespace :deploy do
      # "stub" the task, to be defined later in dev.rb
      task :stop_server
      after : move_output, :stop_server
    end
    

    Then when Capistrano later loads dev.rb, the real implementation of :stop_server will get slotted in.

    Now when you run cap dev deploy you should get the desired result.