Updated GitHub Actions to Publish Hugo Site From Private to Public Repo

Background

When I restarted my blogging journey last year, I went with Hugo to generate a static website hosted as a GitHub Pages site. As mentioned, Blogger and WordPress always suffered recurring problems, and maintenance with WordPress still turned into a time suck due to its complexity. By comparison, GitHub has been a nearly painless hosting provider, and the way I've configured it has allowed me to keep drafts hidden by staging in a private repository.

My Workflow

As I sit down to blog, I start a new post by adding a new markdown page into my posts directory. In the front matter, I use the key-value pair draft: true to notate that the content is still under construction. In other words, my front matter starts to look like the following:

1---
2title: "Updated GitHub Actions to Publish Hugo Site From Private to Public Repo."
3subtitle: "Still keeping half-baked ideas and drafts private!"
4date: 2021-02-13
5draft: true
6tags: ["GitHub", "Actions", "Hugo"]
7---

Now, as I get to a good stopping point (either writing in Visual Studio Code for macOS or using Working Copy and iA Writer on my iPad), I can check-in the updated version of that post to my private GitHub repo. My Github Action workflow on the private repository notices when I push code to the main/master. Within the GitHub Action, the Hugo command building the static site ignores drafts, which means the front matter must contain draft: false for Hugo to generate the static webpage (and therefore the content getting copied to the public repository). If content does get developed and copied to the public repository, GitHub Pages (e.g. the live version of my blog) reflects those changes.

Publishing Hugo Site from a Private Repo to Public GitHub Pages

Remember I published the content of my Github Action in GitHub Actions Publish Private Hugo Repo to Public Pages Site. That Action had worked well for me, ... until it didn't. As I attempted to push a new draft recently, the GitHub Actions workflow failed. The specific failure pointed to GitHub deprecating set-env and add-path commands. Ironically, the YAML file contained neither of those two commands! What?! One of the Actions I sourced from the marketplace was misbehaving.

I set to work updating all the actions and am happy to report the new workflow working as expected.

As a reminder, here's how it works:

  1. The "name: Checkout" step pulls down the repository to /home/runner/work/<Repo Name>/<Repo Name> on the Ubuntu Runner
  2. The "name: Hugo Setup" step downloads and installs the latest non-extended Hugo binary
  3. The "name: Run Hugo" step is where Hugo parses the files from my private GitHub repo and builds the static site based the config.toml file (in my case, ./docs).
  4. Once parsing completes (and ./docs contains the static site), the workflow uses a Personal Access Token to connect to the Public repository and commit all the new/changed files.

Remember, GitHub provides a Free/Personal account up to 2000 minutes of GitHub Actions time per month. In my case, each run of the workflow is taking about 30 seconds. If you find yourself bumping up against this limit, you can quickly reduce your runs by creating a "working" branch to save your work-in-progress. Then you can publish more content at a single time by merging those changes into master and kicking off a new build - things to ponder.

Show me the Code!

Here is my updated main.yml file!

 1# This is a basic workflow to help you get started with Actions
 2
 3name: Hugo Build & Deploy - Private to Public
 4
 5# Controls when the action will run. Triggers the workflow on push or pull request
 6# events but only for the master branch
 7on:
 8  push:
 9    branches: [ master ]
10
11# A workflow run is made up of one or more jobs that can run sequentially or in parallel
12jobs:
13  # This workflow contains a single job called "build"
14  build:
15    # The type of runner that the job will run on
16    runs-on: ubuntu-latest
17
18    # Steps represent a sequence of tasks that will be executed as part of the job
19    steps:
20    
21    # Check Out Repository:   https://github.com/marketplace/actions/deploy-to-github-pages
22    - name: Checkout 🛎️
23      uses: actions/[email protected] 
24      with:
25        persist-credentials: false
26
27    # Setup Hugo
28    - name: Hugo setup
29      uses: peaceiris/[email protected]
30      with:
31        # The Hugo version to download (if necessary) and use. Example: 0.58.2
32        hugo-version: 'latest'
33        # Download (if necessary) and use Hugo extended version. Example: true
34        extended: false
35
36    # Runs Hugo to build the Static Site
37    - name: Run Hugo
38      run: |
39        hugo --minify --verbose        
40
41    # Deploy the Static Site to Public Repo (GitHub Pages)
42    - name: Deploy 🚀
43      uses: JamesIves/[email protected]
44      with:
45        token:  ${{ secrets.ACCESS_TOKEN }}
46        repository-name: rterakedis/rterakedis.github.io
47        branch: master # The branch the action should deploy to.
48        folder: docs # The folder the action should deploy.

Thoughts?

Let me know if you've set up something similar for blogging. Are you using GitHub Actions to automate your publishing workflow? What has been your experience?