【问题标题】:Why isn't my Rake task using the environment that I used in my Cap command?为什么我的 Rake 任务不使用我在 Cap 命令中使用的环境?
【发布时间】:2014-11-01 02:33:54
【问题描述】:

我正在打电话给bundle exec cap staging demo:foo

demo:foo Cap 任务调用 Rake 任务,打印出 Rails.env

但是……Rails 任务正在打印“开发”而不是预期的“暂存”。为什么这样做?为什么不使用我传递给cap 的任何环境?

我知道我可以添加with rails_env: staging,但我不想硬编码环境。我希望能够将此 Cap 任务用于多个环境,并且我希望 Rake 使用我告诉 Cap 使用的任何内容。

### CAP TASK
namespace :demo do
  desc "run a demo command"
  task :foo do
    on roles(:app) do
      within release_path do
        execute 'pwd'
        execute :rake, 'grant:foo'
      end
    end
  end
end

### RAKE TASK (called by above cap task)
namespace :grant do
  desc "do-nothing demo task"
  task :foo do
    puts "args: #{ARGV}"
    puts "Rails.env: #{Rails.env}"
    puts "pwd: #{Dir.pwd}"
  end
end

输出:

3404 ~/dev/myblog$ bundle exec cap staging demo:foo
DEBUG[40846cbf] Running /usr/bin/env if test ! -d /home/blog/rails_apps/blog/staging/current; then echo "Directory does not exist '/home/blog/rails_apps/blog/staging/current'" 1>&2; false; fi on 172.245.32.193
DEBUG[40846cbf] Command: if test ! -d /home/blog/rails_apps/blog/staging/current; then echo "Directory does not exist '/home/blog/rails_apps/blog/staging/current'" 1>&2; false; fi
DEBUG[40846cbf] Finished in 0.815 seconds with exit status 0 (successful).
INFO[8615517c] Running /usr/bin/env pwd on 172.245.32.193
DEBUG[8615517c] Command: cd /home/blog/rails_apps/blog/staging/current && /usr/bin/env pwd
DEBUG[8615517c]     /home/blog/rails_apps/blog/staging/releases/20141031234442
INFO[8615517c] Finished in 0.230 seconds with exit status 0 (successful).
INFO[1afa8e9e] Running bundle exec rake grant:foo on 172.245.32.193
DEBUG[1afa8e9e] Command: cd /home/blog/rails_apps/blog/staging/current && bundle exec rake grant:foo
DEBUG[1afa8e9e]     args: ["grant:foo"]
DEBUG[1afa8e9e]     ENV: development       <<<<<<<<< WHY IS THIS?
DEBUG[1afa8e9e]     pwd: /home/blog/rails_apps/blog/staging/releases/20141031234442
INFO[1afa8e9e] Finished in 1.491 seconds with exit status 0 (successful).

版本:

  • capistrano 是 3.2.1
  • capistrano-rails 是 1.1.1

【问题讨论】:

    标签: rake capistrano3


    【解决方案1】:

    我想我刚刚解决了它。

    我将with rails_env: fetch(:rails_env) do 添加到我的 Cap 任务中。

    namespace :demo do
      desc "run a demo command"
      task :foo do
        on roles(:app) do
          within release_path do
            with rails_env: fetch(:rails_env) do    # ADDED
              execute 'pwd'
              execute :rake, 'grant:foo'
            end
          end
        end
      end
    end
    

    起初,这让我觉得很傻,因为我必须告诉 Cap 使用我已经告诉它使用的 env。

    但经过进一步思考,我似乎将部署环境与 Rails 环境混淆了。在我当前的系统中,它们是一对一的(例如,deployment-staging 部署 rails-staging,deployment-prod 部署 rails-prod),但在其他系统中可能不一定如此。

    【讨论】:

      猜你喜欢
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多