在 ruby​​ Rake 任务中存储项目特定配置信息的最佳方法是什么?

发布于 2024-10-22 20:05:44 字数 264 浏览 1 评论 0原文

我有用于从远程服务器获取生产数据库等的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冧九 2024-10-29 20:05:44

将设置粘贴到 YAML 文件中,然后像这样读取它:

require 'yaml'
config = YAML.load("config.yaml") # or wherever

$remote_host = config['remote_host']
$ssh_username = config['ssh_username']
# and so on

或者您可以只读取一个大的配置哈希:

$config = YAML.load("config.yaml")

请注意,我在这里使用全局变量,而不是实例变量,因此不会对变量范围感到惊讶。

然后 config.yaml 看起来像这样:

--- 
remote_host: some.host.name
ssh_username: myusername
other_setting: foo
whatever: bar

我倾向于将 config.yaml.sample 与代码主体一起签入,其中包含示例但非工作设置,我可以将其复制到非版本化配置。 yaml。有些人喜欢将 config.yaml 签入服务器本身的实时分支,以便对其进行版本控制,但我从来没有为此烦恼过。

Stick the settings in a YAML file, and read it like this:

require 'yaml'
config = YAML.load("config.yaml") # or wherever

$remote_host = config['remote_host']
$ssh_username = config['ssh_username']
# and so on

Or you can just read one big config hash:

$config = YAML.load("config.yaml")

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:

--- 
remote_host: some.host.name
ssh_username: myusername
other_setting: foo
whatever: bar

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.

望她远 2024-10-29 20:05:44

为此,您应该使用 capistrano,您可以使用 mulitsage 或仅对任务进行单独的主机设置,示例 capistrano 看起来像这样:

task :development do
  server "development.host"
end

task :backup do
  run "cd #{current_path}; rake db:dump"
  download "remote_path", "local_path"
end

并这样调用它:

cap development backup

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:

task :development do
  server "development.host"
end

task :backup do
  run "cd #{current_path}; rake db:dump"
  download "remote_path", "local_path"
end

and call it like this:

cap development backup
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文