【问题标题】:Why does deploy:assets:precompile fail with the error uninitialized constant RAILS_ENV?为什么 deploy:assets:precompile 失败并出现错误 uninitialized constant RAILS_ENV?
【发布时间】:2013-02-15 21:14:27
【问题描述】:

部署(到登台和生产,在同一台服务器上)错误:

    triggering after callbacks for `deploy:update_code'
  * executing `deploy:assets:precompile'
  * executing "cd /var/www/staging/releases/20130215205558 && bundle exec rake RAILS_ENV=staging RAILS_GROUPS=assets assets:precompile"
    servers: ["domain.com"]
    [domain.com] executing command
    [domain.com] cd /var/www/staging/releases/20130215205558 && bundle exec rake RAILS_ENV=staging RAILS_GROUPS=assets assets:precompile
 ** [out :: domain.com] rake aborted!
 ** [out :: domain.com] uninitialized constant RAILS_ENV
 ** [out :: domain.com] 
 ** [out :: domain.com] Tasks: TOP => environment
 ** [out :: domain.com] (See full trace by running task with --trace)
    command finished in 22567ms
*** [deploy:update_code] rolling back

此应用正在运行:

  • Rails 3.1.4
  • Ruby 1.9.2-p290

deploy.rb 文件包含:

set :stages, %w(staging production)
require 'capistrano/ext/multistage'
require "bundler/capistrano"
set :application, "appname"
role :app, "domain.com"
role :web, "domain.com"
role :db,  "domain.com", :primary => true
set :scm, :git
set :repository, "git@git.git:username/appname.git"
set :deploy_via, :remote_cache
set :user, "username"
set :runner, "username"
set :use_sudo, false
set :shared_children, %w(system log pids spree)
default_run_options[:pty] = true
default_run_options[:shell] = false
require './config/boot'
require 'airbrake/capistrano'
load 'deploy/assets'
namespace :deploy do
  task :restart, :roles => :app do
    run "touch #{current_path}/tmp/restart.txt"
  end
  task :default do
    update
    restart
    cleanup
  end
  task :finalize_update, :except => { :no_release => true } do
    run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
    run <<-CMD
      rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp/pids &&
      mkdir -p #{latest_release}/public &&
      mkdir -p #{latest_release}/tmp &&
      ln -s #{shared_path}/log #{latest_release}/log &&
      ln -s #{shared_path}/system #{latest_release}/public/system &&
      ln -s #{shared_path}/spree #{latest_release}/public/spree &&
      ln -s #{shared_path}/pids #{latest_release}/tmp/pids
    CMD
    if fetch(:normalize_asset_timestamps, true)
      stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
      asset_paths = fetch(:public_children, %w(images stylesheets javascripts)).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
      run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
    end
  end
  end
end

Gemfile 包含:

source 'http://rubygems.org'
gem 'rails', '~> 3.1'
gem 'spree', '~> 0.70.3'
gem 'tinymce-rails', '3.4.4'
gem 'honeypot-captcha'
gem 'spree_static_content', :git => 'https://github.com/spree/spree_static_content.git', :branch => '0-70-stable'
gem 'spree_editor', :git => 'https://github.com/commonmedia/spree_editor.git', :branch => 'master'
gem 'spree_contact_form', :git => 'git://github.com/commonmedia/spree-contact-form.git', :branch => 'master'
gem 'mysql2'
gem 'airbrake'
gem 'newrelic_rpm'
gem 'jquery-rails'
gem 'capistrano'
gem 'capistrano-ext'
group :assets do
  gem 'sass-rails',   '~> 3.1.5'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
end
group :development do
  gem 'highline'
end
group :test do
  gem 'turn', '0.8.2', :require => false
end
group :development, :test do
  gem 'itslog'
end
group :staging, :production do
  gem 'therubyracer'
end

【问题讨论】:

  • 不确定发生了什么,但让我感到奇怪的第一件事是它是一个未定义的常量,而不是(比如说)默认为development。请记住,Ruby 常量 RAILS_ENV 与环境变量 RAILS_ENV 不同(或者,在 Ruby 中,ENV['RAILS_ENV']。前者设置为后者的值,默认为 "development"。如果常量是undefined,这意味着由于某种原因,该分配被跳过或从未达到。希望这会有所帮助......

标签: ruby-on-rails capistrano bundler


【解决方案1】:

您的命令不正确。你应该改变:

bundle exec rake RAILS_ENV=staging RAILS_GROUPS=assets assets:precompile

...到:

RAILS_ENV=staging RAILS_GROUPS=assets bundle exec rake assets:precompile

【讨论】:

  • 有趣。正在运行的命令是 capistrano 为我执行的任何默认命令。你知道我怎么把它改成你建议的吗?也许对deploy.rb 文件进行了一些编辑?
  • @d_ethier:这是不正确的。原来的命令是正确的。这是一个 Gist 演示:gist.github.com/Peeja/4963777
  • 当环境变量继续rake时,shell 会导出它们。当它们在rake 之后出现时,它们将作为参数提供给rake 进程。但是,rake 然后将它们解释为环境变量。为什么会这样,我不确定。
  • 这是一种非常奇怪的做法。与大多数环境变量在管道命令中设置的方式相反。
【解决方案2】:

这个错误的原因是我从 Rails 2 应用程序复制了一些 Errbit 初始化程序的代码。该初始化程序中的代码使用了RAILS_ENV,这实际上导致了各种问题。我没有意识到应用程序甚至没有启动,因为我从直接添加 Errbit 初始化程序到尝试部署它。

因此,在吸取的一些经验教训中,在 Rails 3 中使用 Rails.env 而不是 RAILS_ENV

【讨论】:

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