A couple of years ago, I wrote how one could check the version of one’s deployed application. Recent technologies such as Spring Boot makes it even easier. It’s as important for regular applications as for static sites such as Jekyll.
I wanted to have this blog display some way for me to make sure it was deployed, using Git data. As I publish from the tip of master and not from tags, the data I want is the commit hash.
Getting the commit hash
I searched for an existing Jekyll plugin and found the Jekyll Version plugin. Having installed the gem, I used it in the following way:
<section class="poweredby">v. {% project_version commit long %}</section>
Publishing the Jekyll site on GitLab Pages yields the following:
<section class="poweredby">v. Oops, are you sure this is a git project?</section>
The issue is that GitLab Pages doesn’t copy the Git repository along with its metadata, but only the raw content. That means there’s no .git folder to get the commit hash from.
There’s also no git executable on the path
|
Another attempt
On GitLab Pages, the commit hash is made available through the CI_COMMIT_SHA
environment variable, along with a lot of other ones.
Fortunately, the Jekyll Environment Variables plugin is here for that:
it provides access to all environment variables through the site.env
object e.g. .
The snippet above can be changed to:
<section class="poweredby">v. {{ site.env.CI_COMMIT_SHA }}</section>
Let’s add the job number, just because we can:
<section class="poweredby">v. {{ site.env.CI_COMMIT_SHA }}/{{ site.env.CI_JOB_ID }}</section>
This displays something similar to the following:
<section class="poweredby">v. 700753b09969769c361ff6dbd35721885fb62468/87773905</section>
One can set local environment variables to "mock" and make sure everything works as expected |
Conclusion
While GitLab Pages doesn’t offer Git metadata on the filesystem, it’s possible to get some some through environment variables. Moreover, other interesting bits of data e.g. environment name, are available and can be either displayed or used to tweak the rendering: the usual "trick" is to have a dedicated banner on non-production environments.