A Rakefile for Jekyll
The Rakefile
For Rubyists, rake is one of the most commonly used tools used to define make-like tasks. The reason to build out your Rakefile is to standardize how you want to perform your tasks. These could be tasks for building the site, automatically testing it, or deploying your site.
Installing rake
I’m going to use a group start adding some development dependencies. For now, it’s going to be one gem, but we will grow it. Add the following to your Gemfile:
group :development do
gem 'rake'
end
Once it’s inserted, your Gemfile may look something like this:
source 'https://rubygems.org'
group :development do
gem 'rake'
end
gem 'jekyll'
Run bundle
to install everything in your Gemfile.
Have Jekyll ignore the Rakefile
Similar to what we did to ignore the Gemfile and Gemfile.lock when building our Jekyll site, we’ll be doing with the Rakefile. In the _config.yml
, add “Rakefile” to the exclude list. It may look something like this afterwards:
exclude: ["Gemfile*", "Rakefile"]
This exclusion only matters when Jekyll is building the static site. The excludes will not impact the rake tasks.
First Rakefile
A Rakefile contains pure Ruby. You can do anything you want that’s Ruby based here. The reason to use a Rakefile over a bash script, is the Rakefile largely for better error handling. You could use the Jekyll binaries to do all of this, but, you’d have to write things in bash to parse the output to see if things worked as expected, as well as error handling. By building it with Ruby, we now have access to all the Ruby goodness.
We’re going to do a couple things with our Rakefile.
First, we’re going to create a method called bold
that will make text that we select appear bold. This can be helpful as the number of tasks you are running grows. When using CI, you may call a task called integration
that may kick off several tasks. Using a bold comment at the beginning of a sequence can help you better identify where failures happened.
Second, we’re going to create tasks to called build
and clean
. build
will be used to generate your static site in _site
. clean
will be used to delete the statically generated site.
# coding: utf-8
require 'jekyll'
# Extend string to allow for bold text.
class String
def bold
"\033[1m#{self}\033[0m"
end
end
# Rake Jekyll tasks
task :build do
puts 'Building site...'.bold
Jekyll::Commands::Build.process(profile: true)
end
task :clean do
puts 'Cleaning up _site...'.bold
Jekyll::Commands::Clean.process({})
end
Running a Rake task
To run the build
task, we call bundle exec rake build
. If you’ve worked with Ruby before, you’re likely familiar with using bundle exec
. Here’s sample output:
$ bundle exec rake build
Building site...
Configuration file: /Users/brint/src/spock.rocks/_config.yml
Source: /Users/brint/src/spock.rocks
Destination: /Users/brint/src/spock.rocks/_site
Incremental build: disabled. Enable with --incremental
Generating...
Filename | Count | Bytes | Time
---------------------------------------------------------+-------+--------+------
_layouts/default.html | 7 | 60.79K | 0.034
_includes/head.html | 7 | 4.45K | 0.012
_includes/footer.html | 7 | 19.31K | 0.011
_includes/header.html | 7 | 7.24K | 0.005
sitemap.xml | 1 | 1.17K | 0.004
feed.xml | 1 | 37.22K | 0.003
_includes/icon-github.html | 7 | 7.11K | 0.002
_layouts/post.html | 5 | 27.08K | 0.002
_includes/icon-twitter.html | 7 | 6.20K | 0.002
_includes/comments.html | 7 | 0.01K | 0.002
index.html | 1 | 1.22K | 0.001
_includes/icon-github.svg | 7 | 6.33K | 0.000
_includes/icon-twitter.svg | 7 | 5.38K | 0.000
_posts/2016-03-18-disqus-with-jekyll.markdown | 1 | 4.77K | 0.000
_posts/2016-03-17-jekyll-sitemap.markdown | 1 | 2.27K | 0.000
robots.txt | 1 | 0.04K | 0.000
_layouts/page.html | 1 | 0.32K | 0.000
_posts/2016-03-16-starting-with-jekyll.markdown | 1 | 5.13K | 0.000
_posts/2016-03-20-jekyll-rake-tasks.markdown | 1 | 2.14K | 0.000
_posts/2016-03-17-jekyll-sitemap.markdown/#excerpt | 1 | 0.24K | 0.000
_posts/2016-03-18-disqus-with-jekyll.markdown/#excerpt | 1 | 0.48K | 0.000
_posts/2016-03-16-starting-with-jekyll.markdown/#excerpt | 1 | 0.55K | 0.000
css/main.scss | 1 | 0.96K | 0.000
_posts/2016-03-20-jekyll-rake-tasks.markdown/#excerpt | 1 | 0.29K | 0.000
_posts/2016-03-19-jekyll-future-posts.markdown/#excerpt | 1 | 0.39K | 0.000
_posts/2016-03-19-jekyll-future-posts.markdown | 1 | 0.56K | 0.000
about.md | 1 | 0.15K | 0.000
done in 0.319 seconds.
Auto-regeneration: disabled. Use --watch to enable.
The clean task is executed the same way: bundle exec rake clean
. Here’s sample output from the clean
task:
$ bundle exec rake clean
Cleaning up _site...
Configuration file: /Users/brint/src/spock.rocks/_config.yml
Cleaning /Users/brint/src/spock.rocks/_site...
done.
Nothing to do for /Users/brint/src/spock.rocks/.jekyll-metadata.
All of this is very basic to give you a working example that you could use as a typing exercise to get started. As you begin adding additional testing, your Rakefile will grow.