Jekyll Git commit
Published 2025-02-28 on Farid Zakaria's Blog
I wanted to include a hyperlink on my blog that included the commit it was published with.
Being a newbie in Jekyll, I did what anyone would do and asked the Internet which pointed me towards: jekyll-github-metadata.
This plugin worked well enough, except it needs network access on builds which didn’t work when I wanted to build my software with Nix.
There was no way to disable the network calls if I knew all the Git information I needed ahead of time.
Let’s do something simpler! We can write an incredibly simple Jekyll plugin that will provide a variable with the commit_hash for use in our pages. Place the following file in _plugins/git_commit.rb
.
module Jekyll
class GitCommitGenerator < Generator
priority :highest
def generate(site)
commit_hash = ENV['JEKYLL_BUILD_REVISION'] ||
`git rev-parse HEAD`.strip
site.config['commit_hash'] = commit_hash
end
end
end
I have also included an environment variable JEKYLL_BUILD_REVISION
, which if present, has the plugin short-circuit.
Our view commit source hyperlink is now pretty simple to cook up.
<p>
<a href="https://github.com/org/repo/tree/{{ site.commit_hash }}/{{ page.path }}"
>Improve this page @ {{ site.commit_hash | slice: 0, 7 -}}
</a>
</p>
🥳 Simplified our build by removing dependencies and removed the necessity of having network access to build our blog.
Improve this page @ 82dd1e1
The content for this site is
CC-BY-SA.