DEV Community

Fairywen
Fairywen

Posted on

How to schedule new blog posts

Some of you may be looking for a way to prepare Hugo-blog content, and post it automatically on a specific day without having any further action to do.

Here is a very simple way to handle this, using both Hugo and GitHub Action functionalities.

Step 1 : Hugo side

A Hugo-based blog content is basically a Markdown file, which contains at its beginning a front matter bloc (using yaml or toml syntax).

This front matter let us set several properties.

For example, here is one from a post of mine :

+++
title = "How to plan your blog posts in advance"
date = 2024-06-10
draft = false
tags = ["tuto","blogging"]
categories = ["tech"]
+++
Enter fullscreen mode Exit fullscreen mode

One can notice the presence of a date field, allowing not only to set up post's writing date, but will be used by Hugo while building the site.

Indeed, Hugo default behavior is to not build future contents.

That's exactly what we need here. Just write the desired publish date instead, and then build periodically the site !

Step 2 : GitHub side

GitHub Actions manages build of the site through a .github\workflows\hugo.yml file, created by GitHub while the workflow is configured the first time (note by the way that it looks like a Jenkinsfile a lot).

My first usage was to build on every push on the main branch :

# Sample workflow for building and deploying a Hugo site to GitHub Pages
name: Deploy Hugo site to Pages

on:
  # Runs on pushes targeting the default branch
  push:
    branches: ["main"]

# Deploying actions
Enter fullscreen mode Exit fullscreen mode

So I simply added a scheduled trigger (see documentation here) every friday morning.

Notice that time is UTC-based, and it is not possible for now to use timezone (see this discussion).

Let's add the scheduled trigger in hugo.yml file :

# Sample workflow for building and deploying a Hugo site to GitHub Pages
name: Deploy Hugo site to Pages

on:
  # Runs on pushes targeting the default branch
  push:
    branches: ["main"]

  # Runs every friday at 5 o'clock AM UTC to publish "music friday" posts
  schedule:
    - cron: '00 05 * * 5'
Enter fullscreen mode Exit fullscreen mode

Here we are !

From now on, the site will be also build once a week to generate contents that was in the future.

Top comments (0)