Automatically updating Hugo
Published:
| by Julian Knight Reading time ~2 min.
📖 Kb
| 📎
Hugo
| 🔖
Hugo, PowerShell, Automation, Commandline
Hugo is still changing very quickly. So it is helpful to be able to easily download and install the latest version directly from GitHub.
Summary 🔗︎
The tricks are firstly to use GitHub’s latest
link for software releases and download the webpage. Secondly, to find the link to the actual software download in
the content of the page. Once you have this, you can download the latest built version file automatically, unpack it over the top of the existing files and then
delete the downloaded file.
NOTE: For hopefully obvious reasons, the Hugo server cannot be running when you run this.
Many thanks to @pgr on the Hugo Discourse for the original idea.
Using PowerShell on Windows 10 🔗︎
# get-hugo.ps1
# Get the latest copy of Hugo for Windows (64bit) from GitHub and install it
# -- CHANGE THESE IF NEEDED -- #
# The folder containing the Hugo `bin` subfolder
$hugoRoot = "c:\Hugo"
# A regular expression to find the right platform link name
$hugoPlatform = "Windows\-64bit\.zip"
# Location of the Hugo executable (relative to $hugoRoot or use absolute path)
$hugoBin = "bin"
# ---------------------------- #
# ================================================== #
# -- Shouldn't need to change anything below here -- #
# ================================================== #
Set-LocationEx -Path $hugoRoot # AKA `cd ...`
# Make TLS 1.2 the primary version otherwise get from most sites will fail
[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
# Get the GitHub page containing the latest version of Hugo
$latest = Invoke-WebRequest -Uri https://github.com/gohugoio/hugo/releases/latest
# Find the file details for the Windows 64bit version
$file = $latest.ParsedHtml.links | Where-Object{$_.nameProp -match $hugoPlatform} | select pathname, nameProp
# Download the actual file
Invoke-WebRequest -Uri ("https://github.com/" + $file.pathname) -OutFile $file.nameProp
# Unpack the zip file into the `bin` folder - NOTE: This requires PowerShell v5 or above (Windows 10 1709)
Microsoft.PowerShell.Archive\Expand-Archive -Path $file.nameProp -DestinationPath $hugoBin -Force
# Delete the zip file
Remove-Item $file.nameProp
#EOF