首页 文章

为什么我的Rake任务不是使用我在Cap命令中使用的环境?

提问于
浏览
4

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

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

BUT... the Rails task is printing "development" instead of "staging" as expected. Why is it doing that? Why isn't it using whatever environment I passed to cap?

我知道我可以添加 with rails_env: staging ,但我不想硬编码env . 我希望能够在多个环境中使用此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).

Versions:

  • capistrano是3.2.1

  • capistrano-rails是1.1.1

1 回答

  • 4

    我想我刚解决了 .

    我在我的Cap任务中添加了 with rails_env: fetch(:rails_env) do .

    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环境的部署环境感到困惑 . 在我当前的系统中,它们是1对1(例如,部署分段部署rails-staging,deployment-prod部署rails-prod),但在其他系统中可能不一定如此 .

相关问题