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:
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:
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:
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
:
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:
So if I wanted to build my site locally and push it to my repository with the commit message “Latest build”, I would run: