在 ruby Rake 任务中存储项目特定配置信息的最佳方法是什么?
我有用于从远程服务器获取生产数据库等的 rake 任务。它始终是相同的任务,但每个项目的服务器信息都会发生变化。我这里有代码: https://gist.github.com/868423 在最后一个任务中,我收到 @local_db_dir_path = nil 错误。
我认为不想使用 shell 环境变量,因为我不想每次使用 rake 或打开新 shell 时都设置它们。
I have rake tasks for getting the production database from the remote server, etc. It's always the same tasks but the server info changes per project. I have the code here: https://gist.github.com/868423 In the last task, I'm getting a @local_db_dir_path = nil error.
I don't think want to use shell environment variables because I don't want to set them up each time I use rake or open a new shell.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将设置粘贴到 YAML 文件中,然后像这样读取它:
或者您可以只读取一个大的配置哈希:
请注意,我在这里使用全局变量,而不是实例变量,因此不会对变量范围感到惊讶。
然后 config.yaml 看起来像这样:
我倾向于将 config.yaml.sample 与代码主体一起签入,其中包含示例但非工作设置,我可以将其复制到非版本化配置。 yaml。有些人喜欢将 config.yaml 签入服务器本身的实时分支,以便对其进行版本控制,但我从来没有为此烦恼过。
Stick the settings in a YAML file, and read it like this:
Or you can just read one big config hash:
Note that I'm using globals here, not instance variables, so there's no chance of being surprised by variable scope.
config.yaml would then look like this:
I tend to keep a config.yaml.sample checked in with the main body of the code which has example but non-working settings for everything which I can copy across to the non-versioned config.yaml. Some people like to keep their config.yaml checked in to a live branch on the server itself, so that it's versioned, but I've never bothered with that.
为此,您应该使用 capistrano,您可以使用 mulitsage 或仅对任务进行单独的主机设置,示例 capistrano 看起来像这样:
并这样调用它:
you should be using capistrano for this, you could use mulitsage or just separate host setting to a task, example capistrano would look like this:
and call it like this: