Hugo - GitHub Action Improvements [Part Two]

Adding Error Handing to your workflows

Introduction

Back in October 2024, I wrote this blog post after running into with the official marketplace offering ‘Azure/static-web-apps-deploy@v1’

3 days ago, the GoHugo Foundation released a patch edition ‘0.139.5’ which caused some issues with the GitHub Action I initially created back in October.

The Problem

I should have kind of thought about this, based on Happy and Sad workflows, I assumed (rightly or wrongly) that for every release pushed by the amazing GoHugo team would contain the required binaries for Hugo. For example if we look at the previous release ‘0.139.4’ - we can see the hugo_extended files.

As you can see, the .5 release, does not have this required files, and at the time of posting (albeit it’ll now be fixed from this post 😉) I had only written the action step based on the “Happy flow” or that there would always be the required files there. But well, they. are. not.

The Fix

So while on CHristmas Leave this week 🎉 I’ve tuesday morning reworking the two github action files I created for ‘Linux’ and ‘Windows’ and also moved them to a Github Repository. In short it was only one step which needed to be “updated”

The first step, checks to see the latest release

1
2
3
latest_version=$(curl -s https://api.github.com/repos/gohugoio/hugo/releases/latest | jq -r .tag_name)
echo "Latest Hugo version is $latest_version"
echo "version=$latest_version" >> $GITHUB_ENV

No changes here are required, but the second step is where the fun starts. for context this is what the ‘v1’ version looked like.

1
2
3
4
5
6
7
8
9
HUGO_VERSION="${{ env.version }}"
DOWNLOAD_URL="https://github.com/gohugoio/hugo/releases/download/${HUGO_VERSION}/hugo_extended_${HUGO_VERSION#v}_Linux-64bit.tar.gz"

echo "Downloading Hugo from $DOWNLOAD_URL"
curl -LO "$DOWNLOAD_URL"

echo "" # Verbose Spacing
echo "Checking Local Directory [$PWD]"
ls # Display the contents of the current directory

From the above code snippet, you can see that I did’nt factor in anything going wrong ever. Which rightly or wrongly isn’t the best way to write code. Having thought about it over a coffee, the revised plan was the following.

Should the latest release not include hugo_extended_«hugo-release»_Linux-64bit.tar.gz tar.gz
downgrade to the next patch release and check again, for example v0.139.5 moves to v0.139.4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Set HUGO_VERSION from the environment variable
HUGO_VERSION="$latest_version"

# Function to decrement patch version
decrement_version() {
    local version="$1"
    local major_minor="${version%.*}"   # Extract major and minor (e.g., v1.139)
    local patch="${version##*.}"       # Extract patch number (e.g., 5)
    local new_patch=$((patch - 1))     # Decrement patch number
    echo "${major_minor}.${new_patch}" # Return decremented version with 'v' intact
}

while true; do
    DOWNLOAD_URL="https://github.com/gohugoio/hugo/releases/download/${HUGO_VERSION}/hugo_extended_${HUGO_VERSION#v}_Linux-64bit.tar.gz"
    echo "Attempting to download Hugo from: $DOWNLOAD_URL"
    
    # Check if the URL exists
    if curl --head --silent --fail "$DOWNLOAD_URL" > /dev/null; then
        echo " " # Required for verbose spacing
        echo "URL is valid. Proceeding to download Hugo version $HUGO_VERSION."
        curl -LO "$DOWNLOAD_URL"
        break
    else
        echo " " # Required for verbose spacing
        echo "hugo_extended_${HUGO_VERSION#v}_Linux-64bit.tar.gz not found. Rolling back to previous patch version."
        HUGO_VERSION=$(decrement_version "$HUGO_VERSION")
        echo "version=$HUGO_VERSION" >> $GITHUB_ENV
        
        # Stop if no valid patch version remains
        if [[ "$HUGO_VERSION" == "v0.0.0" ]]; then
            echo "No valid versions found. Exiting."
            exit 1
        fi
    fi
done

Wrap Up

In this update to the original post from October 2024, we’ve tackled an issue caused by the GoHugo Foundation’s release of version 0.139.5, which lacked the expected hugo_extended binaries. This led to a breakdown in the previously working GitHub Action setup, which relied on the assumption that the necessary binaries would always be available.

The solution involved adding error handling and fallback logic to the GitHub Action. By implementing a version-checking mechanism, the workflow now automatically downgrades to the previous patch version if the latest release doesn’t include the required files. This change ensures that the action remains resilient, even when unexpected releases occur.

The updated workflow steps include:

  • Checking the latest Hugo release.
  • Attempting to download the hugo_extended binary.
  • If the download fails, the script will decrement the patch version and retry until a valid version is found or all options are exhausted.

This enhancement ensures that your Hugo-related GitHub Actions will remain functional, even when new releases don’t go as planned. It’s a simple yet powerful way to add error handling and make automation more reliable.

Finally, As promised if you wish to look at the code base and use it for yourself - please check out: https://github.com/builtwithcaffeine/bwc-github-actions

Share with your network!

Built with Hugo - Theme Stack designed by Jimmy