Scheduling Hugo + Azure Static WebApp Posts with GitHub Actions

How to use a scheduler to automatically post Hugo articles when hosting on an Azure Static Website with Github Actions.

Since a refresh in early 2021 this site has been hosted on an Azure Static Website, backed by a GitHub repository and using GitHub Actions to run the Hugo site generator to convert my markdown files into a responsive static website.

Hugo allows me to define a publish date, and even an expiry date, in the Front Matter for each article. Using this I can line up a series of articles to be published on future dates and commit them to my repository, however they won’t be published to the live website unless something triggers the GitHub Action after the publish date has been reached. By default this action will only run when I push changes to the repository. To ensure that I can use future-dated posts, I’ve set up a daily schedule to run the GitHub Action to publish the site.

The changes are made to the workflow configuration file in your repository- this will be called something like /.github/workflows/azure-static-web-apps-my-site-1234.yml. In the top of the default file there is a section which gets the GitHub Action to run when certain conditions are met. for example, when a push or pull request is made. To trigger this action to also run on a schedule we add a schedule: entry as highlighted in the following code sample:

 1name: Azure Static Web Apps CI/CD
 4  push:
 5    branches:
 6      - master
 7  pull_request:
 8    types: [opened, synchronize, reopened, closed]
 9    branches:
10      - master
11  schedule:
12      # Build website at 6am daily to get scheduled future posts.
13      - cron: '0 6 * * *'

We also need to modify the build-and-deploy job to run when the schedule event is triggered, adding the condition OR github-event-name EQUALS schedule to the end of the default line as follows should do that trick.

2  build_and_deploy_job:
3    if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed') || github.event_name == 'schedule'

The cron: line here is important to get right as it sets the time(s) the schedule will trigger the action. If you’ve ever configured cron this is essentially the same format, stating the time of day (minute and hour), day of the month, month of the year, and day of the week. Multiple schedules can be added for more complex arrangements, but mine is set to run daily at 6am (UTC). There’s lots more detail on the GitHub docs site.

It’s also worth noting that the scheduler can be delayed, or even ignored, if there isn’t capacity to run at a given time. Therefore I continue to use manual publishing when on an important release, or when needing to meet terms of a publishing embargo for example.

Adding a similar scheduling method is definitely something I’d recommend if you host your site in this way and want to make use of the date variable in Hugo to line up future posts for publication.