运行 从 Capistrano 提取任务(如果存在)

Run rake task from Capistrano if it exists

我需要创建一个运行自定义 rake 任务的 Capistrano 预部署步骤。

deploy.rb:

before 'deploy:starting', 'db:rollback_staging'

namespace :db do
  desc 'Rollback staging db only if PR already deployed requires rollback'
  task :rollback_staging do
    on roles(:master) do
      within current_path.to_s do
        with rails_env: 'staging' do
          execute :rake, 'release:rollback_staging'
        end
      end
    end
  end
end

问题是当部署这段代码时,rake 任务还没有出现在服务器上,因此部署失败:

rake stdout: rake aborted!

Don't know how to build task 'release:rollback_staging' (See the list of available tasks with `rake --tasks`)

是否有办法从 Capistrano 检查 rake 任务是否存在? 像:

with rails_env: 'staging' do
  execute :rake, 'release:rollback_staging' if rake_exists? 'release:rollback_staging'
end

我最终只是忽略了使用 raise_on_non_zero_exit: false:

的 rake 任务的非 0 退出代码
with rails_env: 'staging' do
  execute :rake, 'release:rollback_staging', raise_on_non_zero_exit: false
end

这行得通吗?在 https://github.com/AgileConsultingLLC/capistrano3-delayed-job

中看到了这种模式
if Rake::Task.task_defined?('release:rollback_staging')