Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何有条件地覆盖Capistrano';s部署:迁移任务?_Ruby On Rails_Capistrano3 - Fatal编程技术网

Ruby on rails 如何有条件地覆盖Capistrano';s部署:迁移任务?

Ruby on rails 如何有条件地覆盖Capistrano';s部署:迁移任务?,ruby-on-rails,capistrano3,Ruby On Rails,Capistrano3,我希望有条件地覆盖deploy:migrate,以便手动运行某些迁移 以下是我在config/deploy/tasks/deploy.rake中的内容: namespace :deploy do if ENV['DB_MIGRATE'] == 'skip' desc "Override Capistrano's default behavior, do not migrate on deploy" task :migrate do raise 'BOO!'

我希望有条件地覆盖
deploy:migrate
,以便手动运行某些迁移

以下是我在
config/deploy/tasks/deploy.rake中的内容:

namespace :deploy do
  if ENV['DB_MIGRATE'] == 'skip'
    desc "Override Capistrano's default behavior, do not migrate on deploy"
    task :migrate do
      raise 'BOO!'
    end
  end
end
下面是我运行
DB_MIGRATE=skip cap staging deploy:MIGRATE时看到的内容:

INFO [deploy:migrate] Run `rake db:migrate`
DEBUG [c0ed2f81] Running /usr/bin/env if test ! -d /path/current; then echo "Directory does not exist '/path/current'" 1>&2; false; fi as deploy@host.
DEBUG [c0ed2f81] Command: if test ! -d /path/current; then echo "Directory does not exist '/path/current'" 1>&2; false; fi
DEBUG [c0ed2f81] Finished in 1.061 seconds with exit status 0 (successful).
INFO [2f3a4cc7] Running bundle exec rake db:migrate as deploy@host.
DEBUG [2f3a4cc7] Command: cd /path/current && ( RAILS_ENV="staging" bundle exec rake db:migrate )
INFO [2f3a4cc7] Finished in 6.518 seconds with exit status 0 (successful).
(Backtrace restricted to imported tasks)
cap aborted!
BOO!

Tasks: TOP => deploy:migrate
(See full trace by running task with --trace)
从输出来看,似乎Capistrano正在做它一直做的事情,然后,除此之外,做了我要求它做的事情(引发异常)


如何摆脱Capistrano默认的
deploy:migrate

根据我的经验,我认为它是在远程查看环境变量,而不是本地。调用cap staging deploy将远程服务器上的环境变量设置为RAILS\u ENV=staging。但由于这意味着登录远程服务器并设置var,这就减少了capistrano的易用性

更好的方法可能是使用capistrano条件:

其自述说明如下:

从Capistrano 2到Capistrano 3的一个主要变化是任务定义现在是可添加的,因此定义新任务不会覆盖现有定义

文档中有一章解释了整个过程:

在Capistrano v2中重新定义任务时,原始任务被替换。但是,Capistrano v3构建的Rake DSL是可添加的[…]

但也可以完全清除任务,然后从头开始重新定义它


这正是我所需要的。

在这里扩展已接受的答案,这将OP和链接的文档整合到一个完整的解决方案中:

if ENV['DB_MIGRATE'] == 'skip'
  Rake::Task["deploy:migrate"].clear_actions
  namespace :deploy do
    desc "Override Capistrano's default behavior, do not migrate on deploy"
    task :migrate do
      puts 'BOO!'
    end
  end
end

我通过在
Capfile
中有条件地要求
capistrano/rails/migrations
来解决这个问题,即

# Migrations can be skipped by setting up local 'SKIP_MIGRATIONS' variable, i.e.
# SKIP_MIGRATIONS=true bundle exec cap production deploy
unless ENV['SKIP_MIGRATIONS']
  require 'capistrano/rails/migrations'
end

我使用条件来定义任务,而任务定义只在本地进行。但是谢谢你的链接,这让我走上了正确的轨道!capistrano conditional提供了一种向任务添加“如果资产被更改”之类的条件的方法,所以这并不是我想要的。