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

.github:
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

deploy-site:
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 entrypoint.sh

GitHub action workflow

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

action "Hugo Deploy" {
  uses = "./deploy-site/"
  secrets = ["SLACK_WEBHOOK", "GITHUB_TOKEN"]
}

Dockerfile

FROM alpine:latest

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

LABEL "com.github.actions.name"="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"="http://github.com/jpleger/jamespleger.com/"
LABEL "homepage"="http://github.com/jpleger/"
LABEL "maintainer"="James Pleger <jpleger@gmail.com>"

RUN apk update && apk upgrade
RUN apk add git curl
ADD https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/${HUGO_FILENAME} /tmp/
RUN tar -xzf /tmp/${HUGO_FILENAME} -C /usr/local/bin/

COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh" ]

Entrypoint

#!/bin/sh
cd ${GITHUB_WORKSPACE}
echo "[-] Updating submodules (themes)"
git submodule update --init
HUGO_SRC="${GITHUB_WORKSPACE}/site/"
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 user.name "${GITHUB_ACTOR}"
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
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 "jamespleger.com" > ./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
SLACK_NOTIFY=$?
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}
fi