very much a lessons learned post and some automation! š¤¦āāļø
The Lesson!
Never, assume a demo, no matter how simple it is. Will always work!
September, Come Cloud With Us - The pre story to this post.
So in September, I had the opportunity to present my “Getting Started with Blogging” Talk at the Come Cloud with Us User Group. THe summary of the talk is a whistle stop tour of a slide deck, explaining why you should get into blogging and then a live demo of deploy Hugo to an Azure Static Web App. Nice and easy. nothing super crazy demo wise… So I thought (The BIGGEST Mistake Ever!). The its demo is pretty easy, install hugo, make a GitHub Repository, Static Web App and push š„³.
But no. this was not the case, As you can see in the video below. The GitHub action would not build. It failed every time and I even tried two different themes, that Ive had working in the past. Still nothing. - So I changed the end of the talk as you can see in the video, showing my Actual blog and a working action to show “What should happen”.
Post user group talk, I’ve spent some time trying to find the cause of said issue, Apart from the fact that I didn’t check the demo before hand.
Skip to 33:45 to see the Action error.
The issues during session
The post mortem begins, When you create a Static Web App and connect to a GitHub Repository, you get a GitHub Action to compile the Hugo site and upload.
On investigation currently (October 2024) the GitHub action uses this action: Azure/static-web-apps-deploy@v1. During the deployment process, I noticed that the Oryx builder used a very specific version of Hugo 0.112.5-48e33e10af2feb2a0e1c32d04880ed016619f2d8+extended and here is the problem.
Since the release of 0.128.0 Hugo has made a configuration change from using IsSite.Server to Hugo.IsServer. When looking a the theme I used during the User Group Demo Bilberry. I noticed there was an update on the 18th August.
Ok, So there is clearly an issue with the Azure/static-web-apps-deploy@v1 action. So how do we fix it?
Having done some google-fu I found a github issue which looked interesting and showed a possibility that this might be possible. Git Issue #332. I guess I can also thank Chris Reddington for the inspiration here. In short this simple GitHub Action gave me the idea and hope that we might be able to work around this issue.
name:Azure Static Web Apps CDon:push:branches:- mainjobs:build_and_deploy_job:runs-on:ubuntu-latestname:Build and Deploy Jobsteps:- uses:actions/checkout@v2with:submodules:true- name:Installing Hugo 0.91.2run:| wget https://github.com/gohugoio/hugo/releases/download/v0.91.2/hugo_extended_0.91.2_Linux-64bit.deb
sudo dpkg -i hugo_extended_0.91.2_Linux-64bit.deb
hugo version- name:Building siterun:| cd ./src
hugo --minify- name:Deployid:builddeployuses:Azure/static-web-apps-deploy@v1with:azure_static_web_apps_api_token:${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_GRAY_FOREST_040213F00 }}action:"upload"# For more info: https://aka.ms/swaworkflowconfigapp_location:"/src/public"# output directory after the hugo buildskip_app_build:true
So how do we improve?
Github Action Improvements
First thing first, we need to be able to download and install Hugo into a GitHub hosted runner. Sounds pretty easy right.
to save some time for you all, I’ve already spent the hours working out the cli magic to sort this and you can have two favours.
name:Hugo Check and Install (Linux)on:push:branches:- mainworkflow_dispatch:# Uncomment the following lines if you want to add pull request triggers# pull_request:# types: [opened, synchronize, reopened, closed]# branches:# - mainjobs:check-and-install:runs-on:ubuntu-lateststeps:- name:Checkout repositoryuses:actions/checkout@v4- name:Check Operating Systemrun:| echo "Operating System: Linux"
cat /etc/os-release
- name:Get latest Hugo versionid:get_latest_versionrun:| 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
- name:Download Hugorun:| 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- name:Install Hugorun:| HUGO_VERSION="${{ env.version }}"
echo ""
echo "Using Hugo version ${HUGO_VERSION}"
echo "Extracting Hugo for Linux"
tar -zxf "hugo_extended_${HUGO_VERSION#v}_Linux-64bit.tar.gz"
chmod +x hugo
echo "" # Verbose Spacing
echo "Hugo installed for Linux"
sudo mv hugo /usr/local/bin/
- name:Check Hugo versionrun:hugo version
name:Hugo Check and Install (Windows)on:push:branches:- mainworkflow_dispatch:# Uncomment the following lines if you want to add pull request triggers# pull_request:# types: [opened, synchronize, reopened, closed]# branches:# - mainjobs:check-and-install:runs-on:windows-lateststeps:- name:Checkout repositoryuses:actions/checkout@v4- name:Check Operating Systemrun:| Write-Host "Operating System: Windows"
systeminfo | Select-String -Pattern "OS Name|OS Version"- name:Get latest Hugo versionid:get_latest_versionrun:| $latest_version = (Invoke-RestMethod -Uri "https://api.github.com/repos/gohugoio/hugo/releases/latest").tag_name
Write-Host "Latest Hugo version is $latest_version"
echo "version=$latest_version" >> $env:GITHUB_ENV- name:Download Hugorun:| $HUGO_VERSION = $env:version
Write-Host "HUGO_VERSION is $HUGO_VERSION"
# Trim the 'v' character if it exists
$trimmed_version = if ($HUGO_VERSION.StartsWith('v')) { $HUGO_VERSION.Substring(1) } else { $HUGO_VERSION }
$DOWNLOAD_URL = "https://github.com/gohugoio/hugo/releases/download/$($HUGO_VERSION)/hugo_extended_${trimmed_version}_windows-amd64.zip"
Write-Host "Downloading Hugo from $DOWNLOAD_URL"
Invoke-WebRequest -Uri $DOWNLOAD_URL -OutFile "hugo_extended_${trimmed_version}_windows-amd64.zip"
Write-Host "Checking Local Directory [$PWD]"
Get-ChildItem- name:Install Hugorun:| $HUGO_VERSION = "${{ env.version }}"
$trimmed_version = if ($HUGO_VERSION.StartsWith('v')) { $HUGO_VERSION.Substring(1) } else { $HUGO_VERSION }
Write-Host "Using Hugo version $HUGO_VERSION"
Write-Host "Extracting Hugo for Windows"
Expand-Archive -Path "hugo_extended_${trimmed_version}_windows-amd64.zip" -DestinationPath '.' -Force
Write-Host "Hugo installed for Windows"
Move-Item -Path "hugo.exe" -Destination "C:\ProgramData\chocolatey\bin\"- name:Check Hugo versionrun:hugo version
Once I had this working, I started on “upgrading” the actual build-and-upload GitHUb action.
Below is the finalised working version of the updated script.
Useful Tips and Information
Under the Build and Deploy Job stage the following things have been changed.
> skip_app_build This disabled the Orynx builder, and means we need to use the hugo --minify step to build the source files for the web app.
> app_location Now that we are manually building the source files, I’ve created a dedicated directory for this build process action-hugo-swa-builder this means that anyone can take this action, replace with their secrets and get back to blogging!
> hugo –minify What I learnt about this step was that when running this (which is required and happens behind the scenes in the Oryx builder) is it creates a new folder public which is the actual files used for the web app.
name:Azure Static Web Apps CI/CD# Ref for initial Fix: https://github.com/Azure/static-web-apps/issues/322on:push:branches:- prodworkflow_dispatch:pull_request:types:[opened, synchronize, reopened, closed]branches:- prodjobs:build_and_deploy_job:if:github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')runs-on:ubuntu-lateststeps:- name:Download and Install Hugorun:|# Get Latest Release of Hugolatest_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# Set HUGO_VERSION from the environment variableHUGO_VERSION="$latest_version"# Construct the download URLDOWNLOAD_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 Spacingecho "Checking Local Directory [$PWD]"ls# Display the contents of the current directory# Extract Hugoecho "Extracting Hugo Tar File"tar -zxf "hugo_extended_${HUGO_VERSION#v}_Linux-64bit.tar.gz"chmod +x hugo# Install Hugoecho ""# Verbose Spacingsudo mv hugo /usr/local/bin/echo "Hugo: $HUGO_VERSION Installed!"- name:Check Hugo versionrun:hugo version- name:Checkout repositoryuses:actions/checkout@v4.2.0with:submodules:truepath:./action-hugo-swa-builder- name:Build Hugo Siterun:| echo "Building Hugo Site, Directory [./action-hugo-swa-builder]"
cd ./action-hugo-swa-builder
hugo --minify- name:Build and Deploy Jobid:builddeployuses:Azure/static-web-apps-deploy@v1with:azure_static_web_apps_api_token:${{ secrets."REPLACE_WITH_YOUR_SECRET"> }}# Replace "REPLACE_WITH_YOUR_SECRET" with Your GitHub Secretrepo_token:${{ secrets.GITHUB_TOKEN }}# Used for Github integrations (i.e. PR comments)action:"upload"###### Repository/Build Configurations - These values can be configured to match your app requirements. ####### For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig# Skip Oryx Build, Current (October 2024) - Uses Hugo v0.112.5, No Longer works due to .Site.IsServer depreciationskip_app_build:true#app_location: "/" # [DEFAULT] App source code path# Updated Application Path, Based on Hugo Minify Build Path# $PWD is the working directory for the GitHub Action,# public, Is the build folder from 'hugo --minify'app_location:"./action-hugo-swa-builder/public"api_location:""# Api source code path - optionaloutput_location:"public"# Built app content directory - optional###### End of Repository/Build Configurations ######close_pull_request_job:if:github.event_name == 'pull_request' && github.event.action == 'closed'runs-on:ubuntu-latestname:Close Pull Request Jobsteps:- name:Close Pull Requestid:closepullrequestuses:Azure/static-web-apps-deploy@v1with:azure_static_web_apps_api_token:${{ secrets."REPLACE_WITH_YOUR_SECRET"> }}# Replace "REPLACE_WITH_YOUR_SECRET" with Your GitHub Secretaction:"close"
Summary
Well here we are, at the end of a blog post. So I guess there are a couple of things I’ve learnt from this one. 1 - Don’t assume the worlds simplest demo will always work (Unless you butcher an action to fix the dependencies issue š) 2 - Updates happen we know this. Just as of posting this in October, I’ve not seen any response on the Static Web App Issues repository. GitHub Issue #1547 3 - I’ve learn’t a lot more about Hugo and building a static web app
So I hope this post was useful and that others, Should they run into the same decencies issues I did with the IsSite.Server problem, have a workaround until Microsoft releases an official update to the Github Action.