Let's Go Hugo - Part 1 VSTS Build

Part 1 of a series of unknown parts on my journey of migrating to Hugo and attempting to rub some DevOps on it. In this episode, I cover generating this site via VSTS build.

Before we start, I know Hugo build task is on the Visual Studio Marketplace, but I’ve opted not to use it as a learning exercise (it also gives me the excuse to write something).

VSTS Build Definition

Get Sources

Set this section up as appropriate for your project and processes. For reference, the following is what I’ve used:

Default branch for manual and scheduled builds
master
Clean
false
Tag sources
Never
Report build status
checked
Checkout submodules
checked
Recursion level
Any nested submodules within
Checkout files from LFS
unchecked
Don’t sync sources
unchecked
Shallow fetch
unchecked

I could enable shallow fetch, but I intend to use Hugo’s Git info feature in the future.

Download latest Hugo release (PowerShell)

The first task is to download the latest Hugo release. I’ve opted to download it from GitHub rather than keep it within the repo or Azure storage. This might change in the future when I revisit the build.

Version
2.*
Type
Inline
Script
Write-Host "Finding latest Hugo release"
$release = Invoke-RestMethod -Uri https://api.github.com/repos/gohugoio/hugo/releases/latest -Method Get
# Modify match expression if you're not using windows (e.g.: Hosted and Hosted VS2017)
$releaseBin = $release.assets | Where-Object { $_.name -match "Windows-64bit.zip" }
Write-Host "Downloading latest Hugo release from: $($releaseBin.browser_download_url)"
Invoke-WebRequest -Uri $releaseBin.browser_download_url -Method Get -OutFile hugo.zip

If you encounter The request was aborted: Could not create SSL/TLS secure channel., you can work around it by executing the following line before Invoke-RestMethod:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Extract Hugo (Extract Files)

Now we extract the Hugo binaries.

Version
1.*
Archive file patterns
*.zip
Destination folder
$(Agent.BuildDirectory)

Build website to $(Build.BinariesDirectory) (PowerShell)

Now we build the site. You can use a Command Line task instead of PowerShell if you prefer.

Type
Inline
Script
Write-Host "Building website"
$(Agent.BuildDirectory)\hugo.exe -d $(Build.BinariesDirectory)

Archive $(Build.BinariesDirectory) (Archive Files)

This is optional. I’ve chosen to archive my build artifacts because it’s quicker download during the release process as opposed to the many individual files. It’s not much of a difference for a tiny blog like this.

Version
2.*

Root folder or file to archive $(Build.BinariesDirectory)

Prepend root folder name to archive paths
unchecked
Archve type
zip
Archive file to create
$(Build.ArtifactStagingDirectory)/coolshitindustries-website-$(Build.BuildId).zip

Publish Artifact: drop (Publish Build Artifacts)

Version
1.*
Path to publish
$(Build.ArtifactStagingDirectory)
Artifact name
drop
Artifact publish location
Visual Studio Team Services/TFS