惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
量子位
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
J
Java Code Geeks
B
Blog
V
V2EX
博客园 - 三生石上(FineUI控件)
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
F
Fortinet All Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
A
About on SuperTechFans
D
DataBreaches.Net
阮一峰的网络日志
阮一峰的网络日志
博客园 - Franky
H
Help Net Security
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
MongoDB | Blog
MongoDB | Blog
L
LangChain Blog

drew's dev blog

Hunting a production-only proxy bug in SvelteKit Artisanal Handcrafted Git Repositories Everything you need to know about Python 3.13 – JIT and GIL went up the hill How Postgres stores oversized values – let's raise a TOAST How Postgres stores data on disk – this one's a page turner Bumper Comtrade QuEST Rust Wrapper Dotfiles How long is a cucumber? 🥒 Jekyll KaTeX Block Using make and latexmk for easy LaTeX compilation Calculating the overlap of aerial photos Calculating meters per pixel from aerial photographs Proper line numbers with Jekyll Compiling zsh without root Coding cheatsheet
Custom Jekyll plugins with GitHub Pages
Drew Silcock · 2014-07-29 · via drew's dev blog

· 4 min read

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:

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:

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:

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:

1

jekgit.sh "Latest build"