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


Still keeping half-baked ideas and drafts private!

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:

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

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!

# This is a basic workflow to help you get started with Actions

name: Hugo Build & Deploy - Private to Public

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    
    # Check Out Repository:   https://github.com/marketplace/actions/deploy-to-github-pages
    - name: Checkout 🛎️
      uses: actions/checkout@v2.3.4 
      with:
        persist-credentials: false

    # Setup Hugo
    - name: Hugo setup
      uses: peaceiris/actions-hugo@v2.4.13
      with:
        # The Hugo version to download (if necessary) and use. Example: 0.58.2
        hugo-version: 'latest'
        # Download (if necessary) and use Hugo extended version. Example: true
        extended: false

    # Runs Hugo to build the Static Site
    - name: Run Hugo
      run: |
                hugo --minify --verbose

    # Deploy the Static Site to Public Repo (GitHub Pages)
    - name: Deploy 🚀
      uses: JamesIves/github-pages-deploy-action@4.0.0
      with:
        token:  ${{ secrets.ACCESS_TOKEN }}
        repository-name: rterakedis/rterakedis.github.io
        branch: master # The branch the action should deploy to.
        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?


See also