简体   繁体   English

在capistrano deploy.rb中加载app_config.yml

[英]Load app_config.yml in capistrano deploy.rb

Using Rails 3.2 and Capistrano 2. I have an app_config.yml which I load to the app this way: 使用Rails 3.2和Capistrano2。我有一个app_config.yml ,可以通过以下方式加载到应用程序:

# config/app_config.yml
global: &GLOBAL
  sitename: Shop

# config/application.rb
require File.expand_path('../boot', __FILE__)
APP_CONFIG = YAML.load_file(File.expand_path('../app_config.yml', __FILE__))[Rails.env]

# call using this method
<%= APP_CONFIG["sitename"] %>

I want to load the same app_config.yml in my deployment scripts in Capistrano, so that I don't have to re-set the variables again: 我想在Capistrano的部署脚本中加载相同的app_config.yml ,这样就不必再次重新设置变量:

# config/deploy.rb
require "bundler/capistrano"

require File.expand_path('../boot', __FILE__)
APP_CONFIG = YAML.load_file(File.expand_path('../app_config.yml', __FILE__))[Rails.env]

set :stages, %w(production staging)

And I hope to access the variable using this way too: APP_CONFIG["sitename"] , but as I run the deployment script, I get this error: 我希望也使用这种方式访问​​变量: APP_CONFIG["sitename"] ,但是在运行部署脚本时,出现以下错误:

./config/deploy.rb:4:in `load': uninitialized constant Capistrano::Configuration::Rails
(NameError) # points to the APP_CONFIG = YAML.load_file... line

How can I load the app_config.yml file correctly? 如何正确加载app_config.yml文件?

capistrano does not require any rails code. capistrano不需要任何rails代码。

that's why calling APP_CONFIG = YAML.load_file(File.expand_path('../app_config.yml', __FILE__))[Rails.env] raises the exception. 这就是为什么调用APP_CONFIG = YAML.load_file(File.expand_path('../app_config.yml', __FILE__))[Rails.env]引发异常的原因。

you need to replace the Rails.env with something else that is used in your capistrano deployment. 您需要用Rails.env部署中使用的其他东西替换Rails.env that might be the stage or just "production" depending on your setup. 根据您的设置,这可能是阶段,也可能只是"production"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM