Publishing future blog posts with GitHub Pages and GitHub Actions
I switched to using GitHub Pages and Jekyll for my blog a while ago. There was one deficiency I noticed - the lack of being able to publish future blog posts. Because the site is static, the Jekyll processing is only performed when a new commit is pushed, and it only publishes content ‘in the past’. The problem is a future post has already been committed to Git, but there’s no subsequent commit to republish the site once the publish date comes around.
There’s been various hacks around (usually involving an scheduled commit followed by a revert), but after listening to a recent podcast from Scott Hanselman talking to Edward Thomson about GitHub Actions, I wondered if they might offer a way to automate refreshing the site on a daily schedule.
I started by adding a GitHub Action workflow to the website and took a look at some of the existing actions others had already written, figuring someone might have solved this problem already. I discovered that GitHub Pages treats ‘user’ sites like mine differently to ‘product’ sites. The actions I found seemed suited more to the ‘product’ kind (as they wanted to commit the static content to the master branch).
I then resorted to Twitter..
You can use the api to trigger a rebuild, doesn’t that work?https://t.co/2gaaIIMfWB— Rob Bos (@RobBos81) May 1, 2020
Rob (who I know through the Global DevOps Bootcamp) gave a good suggestion - using the Request a page build REST API. It took a few goes to get the API call working with octokit/request-action, but a test post later and I confirmed it worked!
Here’s the current version:
name: Build GitHub Pages on: schedule: - cron: '0 */12 * * *' # Rebuild twice a day (every twelve hours on the hour). jobs: logLatestRelease: runs-on: ubuntu-latest steps: - uses: octokit/[email protected] id: post_build with: route: POST /repos/flcdrg/flcdrg.github.io/pages/builds env: GITHUB_TOKEN: $
Nice, now I can schedule blog posts in the future, and know that they’ll be published as intended.