Capistrano deploy.rb无法访问舞台文件中定义的任务

时间:2015-03-29 00:22:48

标签: ruby capistrano capistrano3

我试图为我的应用程序组合一个capistrano配方,基本上在本地克隆git repo,做一些构建处理,然后rsyncs到远程服务器。

我有2个环境 - 开发和生产:

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

我收到此错误:

$ 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)

如果部署在stop_server的同一命名空间(部署)中,部署为何不知道如何构建任务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

1 个答案:

答案 0 :(得分:1)

问题是Capistrano首先加载deploy.rb来初始化;然后加载dev.rb

当Capistrano解析这一行时:

after :move_output, :stop_server

它不知道:stop_server引用了什么(因为它还没有加载dev.rb)。因此,您看到的错误消息:

Don't know how to build task 'stop_server'

一个简单的解决方法是在:stop_server中声明一个空的deploy.rb任务。

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

然后当Capistrano稍后加载dev.rb时,:stop_server的真实实现将被插入。

现在,当您运行cap dev deploy时,您应该会得到所需的结果。