Rebuild GitHub Pages

Date: 02 January 2020

Category: Git

Tag: General

I use GitHub Pages to host this website, which supports the Jekyll static site generator framework, that in turn supports writing pages in Markdown or HTML with the Liquid template language.

The following templated code in my footer HTML ensures that my copyright information always displays the latest year - meaning I shouldn’t have to remember to do it manually each January.

<span class="footer-notes">&copy; 2018-2020 ThatScotDataSci</span>

However, even though we’ve just entered 2020 my footer still showed 2019 - so what gives? As the website is static, I need the build and deploy process to run so that my templated HTML file is re-rendered with the latest year. However, GitHub does not expose the backend action (see GitHub Actions), or triggering it manually/on a schedule.

At present, the only way to trigger the website CI/CD process is by committing to master (see the docs).

Push an empty commit to master

As ever, Stack Overflow comes to the rescue - see this thread.

The trick is to push an empty commit to master, using the --allow-empty argument on the git commit command:

git commit -m 'rebuild pages' --allow-empty
git push origin master

Alternatively, I could of course just be patient and wait until I actually have something to commit.

Automating

This gets around the issue of being unable to manually trigger the website build and release, however it would still require me to remember to do this at the start of each year - at which point I may as well just update the year in the footer manually (which would also remove the need for the empty commit).

However, whilst the action to build and deploy GitHub Pages isn’t exposed, you can add your own actions to the website repository - and these can be scheduled.

I created the following scheduled action to push an empty commit on the first of every month (file in repo here). Note that a GitHub action for performing pushes does exist (see github-push-action), however what I needed was very simple, and easily implemented manually. Not only does this action mean that my footer will be updated at the start of January each year, but I’ll also get an email if there are any issues with the build and deployment of my website - flagging any problems which I might run into when I next make updates.

name: Monthly Build

on:
  schedule:
    - cron: '0 0 1 * *'

jobs:
  build:
    name: Create and push empty commit
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Create an empty commit
      run: |
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        echo "Committing with comment: Automated Monthly Build"
        git commit -m "Monthly build - $" --allow-empty
    - name: Push empty commit
      run: |
        echo "Pushing to: github.com/$.git"
        git push "https://$:$@github.com/$.git" master

However, this unfortunately does not work with the current version of GitHub Actions as chaining workflows is prohibited (see docs and thread):

An action in a workflow run can’t trigger a new workflow run. For example, if an action pushes code using the repository’s GITHUB_TOKEN, a new workflow will not run even when the repository contains a workflow configured to run when push events occur.

For now I’ve kept this action in place (it does no harm), and will wait to see when GitHub introduces functionality that would allow it to work.