从 Rake 获取 Ruby 环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14457084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:40:58  来源:igfitidea点击:

Getting Ruby environment variables from Rake

rubyrakeenvironment-variablesrakefile

提问by Sergio Tulentsev

I have a Rakefile which has tasks for deploying or building an application. This Rakefile is used in both production and development.

我有一个 Rakefile,它有部署或构建应用程序的任务。这个 Rakefile 用于生产和开发。

I would like the buildtask to know what the environment is. Can this be done without passing a parameter to the task when I run it? Can it be done with environment variables?

我希望build任务知道环境是什么。当我运行它时,可以在不将参数传递给任务的情况下完成吗?可以用环境变量来完成吗?

When in development, I need the task to look like this:

在开发过程中,我需要任务如下所示:

task :build => :clean do
  compass compile -e development
  jekyll
end

And in production, like this:

在生产中,像这样:

task :build => :clean do
  compass compile -e production
  jekyll
end

回答by Sergio Tulentsev

Yes, you can use environment variables. Here's skeleton implementation:

是的,您可以使用环境变量。这是骨架实现:

task :build do |t, args|
  puts "Current env is #{ENV['RAKE_ENV']}"
end

Usage:

用法:

% rake build
Current env is 

% RAKE_ENV=development rake build
Current env is development