4 min read

Custom Jekyll plugins with GitHub Pages

So GitHub Pages is a fantastic resource for hosting your personal or organisation site on GitHub, for free. It even supports Jekyll! only thing is, it doesn’t support custom plugins because of the --safe flag that it compiles your site with. So what do you do?

Well, if you compile the site using jekyll yourself, then push the resulting compiled HTML to your GitHub Pages repository, then it works perfectly! You get your custom plugins, and you get your free GitHub Pages hosting.

So how do you organise the source and compiled code?

Some people, like Charlie Park, recommend two repos, one with the source code (e.g. github.com/username/username.github.io.raw for the website source code and github.com/username/username.github.io for the compiled HTML). I don’t particularly like this; it’s one project, it should be one repo.

Others, like Alexandre Rademaker, have two separate branches (a master for compiled HTML and a source for the Jekyll source), and change branches then copy the contents of _site into the master branch every time you want to push to your website.

I like the idea of separate branches within the same repo, but messing about with copying _site seems laborious and unnecessary. Here’s my solution:

Two branches: source and master.

Master contains compiled HTML, source contains the Jekyll source.

In the .gitignore of the source branch, you put the following:

Terminal window
1
_site

Then, when you run jekyll build and Jekyll produces all the HTML in _site, git doesn’t recognise it. That means that we can cd into _site, and seeing as git doesn’t know the difference, we can make _site itself into its own git repository.

Assuming you’re starting off with a bog standard single branch Pages repo, you run:

Terminal window
1
# Make sure _site is empty before we begin
2
rm -rf _site/*
3
4
# Make new source branch
5
git checkout -b source
6
7
# Tell Jekyll to ignore this dir
8
touch .nojekyll
9
10
# Tell git to track source remote branch
11
git branch --set-upstream source origin/source
12
13
# Upload your branch to GitHub
14
git push origin source
15
16
# Locally delete the original master branch
17
git branch -D master
18
19
# Make a new git repository within _site
20
cd _site
21
git init
22
23
# Tell Jekyll to ignore this directory
24
touch .nojekyll
25
26
# Set the remote repository to push the HTML to
27
git remote add origin https://github.com/username/username.github.io
28
29
# Tell it to push to the master remote branch
30
git branch --set-upstream master origin/master

Now you’ve got your source branch set up in your root directory and master branch set up in your _site directory, ready for rapid building and deployment of your Jekyll website.

Now each time you want to build your site locally, you just need to run:

Terminal window
1
jekyll build
2
cd _site
3
git add .
4
git commit
5
git push origin master

and you have successfully built and deployed your website with Jekyll. Note that by default Jekyll does not copy .nojekyll over to _site where we need it, because it is a dotfile, so you need to put the following in your _config.yml:

1
include: .nojekyll

Now, to automate this process, I wrote a small bash script to build, commit and push your site all in one command. Here is the gist of it, and this is the script:

1
#!/bin/bash
2
3
if [[ -z "$1" ]]; then
4
echo "Please enter a git commit message"
5
exit
6
fi
7
8
jekyll build && \
9
cd _site && \
10
git add . && \
11
git commit -am "$1" && \
12
git push origin master && \
13
cd .. && \
14
echo "Successfully built and pushed to GitHub."

So if I wanted to build my site locally and push it to my repository with the commit message “Latest build”, I would run:

Terminal window
1
jekgit.sh "Latest build"