ted
ted

Reputation: 14684

Add JEKYLL_ENV=production to rakefile

I have this script that builds my Jekyll static webstite ready to be used on Github Pages. I need to pass JEKYLL_ENV=production to the build arguments to be sure that I can use google analytics (there is a {% if jekyll.environment == 'production' %} tag in my template).

For instance if I did not use this script to publish I would build the site with JEKYLL_ENV=production jekyll build

I however have no knowledge of Ruby and Rakefiles...

I understand what it does but don't know where to put the modifications. I feel like it would look like (wrong code)

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

Here is the original script, thanks!

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 [email protected]:#{GITHUB_REPONAME}.git"
    system "git push origin master --force"

    Dir.chdir pwd
  end
end

Upvotes: 1

Views: 274

Answers (1)

Jordan Running
Jordan Running

Reputation: 106017

In Ruby, environment variables can be accessed in ENV, so if for some reason you can't specify it on the command line when you run your script, I think it will work to just specify it there:

ENV["JEKYLL_ENV"] = "production"

You ought to be able to put this anywhere in the Rakefile before the tasks.

Upvotes: 3

Related Questions