从Rake获取Ruby环境变量

10 ruby rake environment-variables rakefile

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

我想让build任务知道环境是什么.在运行它时,如果不将参数传递给任务,是否可以完成此操作?它可以用环境变量完成吗?

在开发中,我需要任务看起来像这样:

task :build => :clean do
  compass compile -e development
  jekyll
end
Run Code Online (Sandbox Code Playgroud)

在生产中,像这样:

task :build => :clean do
  compass compile -e production
  jekyll
end
Run Code Online (Sandbox Code Playgroud)

Ser*_*sev 21

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

task :build do |t, args|
  puts "Current env is #{ENV['RAKE_ENV']}"
end
Run Code Online (Sandbox Code Playgroud)

用法:

% rake build
Current env is 

% RAKE_ENV=development rake build
Current env is development
Run Code Online (Sandbox Code Playgroud)