Most modern content management systems have features to control when an article is published and when the article should be removed. This functionality can be quite useful if content needs to be approved or finished ahead of time for an announcement or if the post would no longer be relevant after a certain date. Hugo is not a stranger to these features however, given that Hugo sites are static there is server as such to control when these posts are published or to remove them.
Configuring Posts in Hugo
When creating a post in Hugo there are a many variables that can be configured in the Front Matter, the ones that are important for this are PublishDate and ExpiryDate these are fairly self-explanatory.
---
draft: false
date: 2024-01-15T20:43:41Z
publishDate: 2024-01-16T00:00:00Z
expiryDate: 2024-01-20T23:59:00Z
title: "Super Awesome Flash Sale"
description: "The Super awesome flash sale from 16th January to 20th January"
---
Hugo can be configured to display the published date instead of the post date, this is useful as it allows the date the article was authored to be maintained but to display the published date as the post date on the published site. for more Front Matter variables See here.
Add the following to the config.toml file
[frontmatter]
date = ["publishDate", "date", ":default"]
This will set the date on the post to the publish date if it is present otherwise it will fall back to the date.
Viewing articles locally while writing them when the publish date is in the future
By default when running Hugo server it will not publish any draft, future or expired posts. To compile any of these the following arguments need to be used.
-D, --buildDrafts include content marked as draft
-E, --buildExpired include expired content
-F, --buildFuture include content with publishdate in the future
for example, the following will compile all drafts and future articles.
hugo server -D -F
For more information on Hugo server command line arguments See here
Publishing with GitHub Actions
Given that Hugo does not have a server that dynamically controls its content to change the currently published content this requires a full republish. An easy way to achieve this is setup a scheduled trigger in GitHub Actions.
name: Publish Hugo Site (CI/CD)
on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened, closed]
branches:
- main
schedule:
- cron: '0,30 * * * *'
Adding the schedule section will trigger the pipeline on a cron schedule, the above will publish the site every 30 minutes adding any new articles due to be published and removing any newly expired articles.