Automating Blog Updates using Scheduled GitHub Actions
As I said in a previous post, I routinely create blog posts ahead of when I want to publish them. My site is built & managed with Hugo, Forestry.io, and GitHub Actions.
I’m going to walk you through how I’ve configured GitHub Actions to periodically publish new content even while I’m on vacation.
Hugo Content Metadata
For this process to work, I use two variables in each post’s front matter:
publishdate: This is the future UTC date/time when the content should be published.
draft: This should be set to false when the content is ready for publication. (Forestry’s draft toggle handles this.)
As a real-world example, the front matter for this post looks like:
+++ comments = false date = 2020-11-08T21:30:00Z draft = false publishdate = 2020-11-09T05:30:00Z slug = "automating-blog-updates" tags = ["hugo", "github actions", "devops"] title = "Automating Blog Updates using Scheduled GitHub Actions" +++
Whenever possible, I try to set the time portion of
publishdate to 05:30:00Z or 17:30:00Z. The reason for this should become obvious in a moment.
GitHub Actions Scheduled Event Trigger
GitHub Actions can be triggered several different ways. By default, every time I push a change to this blog’s git repo, GitHub Actions automatically builds and pushes it to an S3 bucket. GitHub Actions can also be configured to trigger build events based on a schedule.
A scheduled event trigger can set setup in your project’s
.github/workflows/build.yml using schedule with a POSIX cron syntax. For example, this blog is published on push and via a daily scheduled event at 6am and 6pm. The trigger portion of this blog’s GitHub Actions is:
on: push: schedule: - cron: '0 6,18 * * *'
The syntax above automatically triggers my blog’s GitHub Action at 6am and 6pm daily. I usually set the time in each of my post’s
publishdate just before this time–at either 5:30am or 5:30pm–to ensure the content is published.
It’s that simple and if you are reading this post, everything above worked flawlessly.
If you have trouble figuring out the cron syntax, I suggest checking out crontab.guru.
If you are looking for more information about scheduled events in GitHub Actions, the documentation should answer most of your questions.