添加 JEKYLL_ENV=production 到 rakefile

Add JEKYLL_ENV=production to rakefile

我有这个脚本可以构建我的 Jekyll 静态网站,可以在 Github 页面上使用。 我需要将 JEKYLL_ENV=production 传递给构建参数 以确保我可以使用 google 分析(我的模板中有一个 {% if jekyll.environment == 'production' %} 标记) .

例如,如果我不使用此脚本发布,我将使用 JEKYLL_ENV=production jekyll build

构建站点

但是我不了解 Ruby 和 Rakefiles...

我明白它的作用,但不知道在哪里进行修改。我觉得它看起来像(错误代码)

Jekyll::Site.new(Jekyll.configuration({
    "source"      => ".",
    "destination" => "_site"
  }).build({
    "JEKYLL_ENV" => "production"
  })).process

这是原稿,谢谢!

require "rubygems"
require "tmpdir"

require "bundler/setup"
require "jekyll"

GITHUB_REPONAME = "my_reponame"


desc "Generate blog files"
task :generate do
  Jekyll::Site.new(Jekyll.configuration({
    "source"      => ".",
    "destination" => "_site"
  })).process
end


desc "Generate and publish blog to gh-pages"
task :publish => [:generate] do
  Dir.mktmpdir do |tmp|
    cp_r "_site/.", tmp

    pwd = Dir.pwd
    Dir.chdir tmp

    system "git init"
    system "git add ."
    message = "Site updated at #{Time.now.utc}"
    system "git commit -m #{message.inspect}"
    system "git remote add origin git@github.com:#{GITHUB_REPONAME}.git"
    system "git push origin master --force"

    Dir.chdir pwd
  end
end

在Ruby中,环境变量可以在ENV中访问,所以如果由于某些原因你在运行你的脚本时不能在命令行上指定它,我认为只要在此处指定它就可以工作:

ENV["JEKYLL_ENV"] = "production"

您应该能够在任务之前将它放在 Rakefile 中的任何位置。