Github Actions Hugo Deploy

GitHub Actions

GitHub actions is a really simple way of executing code in the context of a GitHub repository. For example building images, testing or deployment in a very minimal way using docker images. It is currently in beta, so its very likely that things might have changed by the time you read this.

Automatic Hugo deployment on GitHub pages

There are a couple ways of automatically deploying hugo sites, but actions are a great way to automatically build/push websites. Below is an example setup (and how this website is deployed). There are a few features, for example posting to slack on successful push to the gh-pages branch.

Directory layout

total 8
drwxr-xr-x   3 jpleger  staff   96 Apr 29 17:22 .
drwxr-xr-x  11 jpleger  staff  352 Apr 29 17:08 ..
-rw-r--r--   1 jpleger  staff  169 Apr 29 20:43 main.workflow

total 16
drwxr-xr-x   4 jpleger  staff   128 Apr 29 17:22 .
drwxr-xr-x  11 jpleger  staff   352 Apr 29 17:08 ..
-rw-r--r--   1 jpleger  staff   739 Apr 29 20:59 Dockerfile
-rwxr-xr-x   1 jpleger  staff  1364 Apr 29 20:57

GitHub action workflow

workflow "Push Website" {
  on = "push"
  resolves = ["Hugo Deploy"]

action "Hugo Deploy" {
  uses = "./deploy-site/"


FROM alpine:latest

ENV HUGO_FILENAME hugo_${HUGO_VERSION}_linux-64bit.tar.gz

LABEL ""="Deploy hugo site"
LABEL "com.github.actions.description"="Generate static content to gh-pages branch"
LABEL "com.github.actions.icon"="activity"
LABEL "com.github.actions.color"="purple"
LABEL "repository"=""
LABEL "homepage"=""
LABEL "maintainer"="James Pleger <>"

RUN apk update && apk upgrade
RUN apk add git curl
RUN tar -xzf /tmp/${HUGO_FILENAME} -C /usr/local/bin/



echo "[-] Updating submodules (themes)"
git submodule update --init
HUGO_DEST=$(mktemp -d)
echo "[-] Rendering site to ${HUGO_DEST}"
hugo -v --minify -d "${HUGO_DEST}" -s "${HUGO_SRC}"
echo "[-] Configuring github"
git config --global "${GITHUB_ACTOR}"
git config --global "${GITHUB_ACTOR}"
echo "[-] Removing theme files to checkout gh-pages branch"
rm -rf site/themes/*
echo "[-] Checking out gh-pages branch"
git checkout gh-pages > /dev/null 2>&1
echo "[-] Clean gh-pages"
rm -rf ./*
echo "" > ./CNAME
echo "[-] Copying rendered Files"
cp -R ${HUGO_DEST}/* ./
TIMESTAMP=$(date +%s%3N)
echo "[-] Aadding changes to gh-pages branch"
git add . > /dev/null 2>&1
echo "[-] Committing changes to gh-pages branch"
git commit -am "Automated pages deployment at $TIMESTAMP" > /dev/null 2>&1
echo "[-] Pushing changes to gh-pages branch"
git push --set-upstream origin gh-pages  > /dev/null 2>&1
if [ "${SLACK_NOTIFY}" == "0" ]; then
  echo "[-] Posting slack message"
  curl -X POST --data-urlencode "payload={\"channel\": \"#sheeple-admin\", \"username\": \"github-actions\", \"text\": \"${GITHUB_ACTOR} successfully deployed ${GITHUB_REPOSITORY} to gh-pages\", \"icon_emoji\": \":github_octocat:\"}" ${SLACK_WEBHOOK}
Avatar photo James is a security nerd with a technology problem. Follow him on Twitter TwitterTweet