hugo: Add service for building Hugo-based sites

By default, a `hugo-build-local@.service` systemd service is included,
for building Hugo-based sites as stored in a local Git repository (as
served by the `git` service), and into a volume (which is intended to be
served by the `nginx-serve-volume` service).
This commit is contained in:
Alex Palaistras 2022-04-18 18:07:20 +01:00
parent 564905663b
commit 3aea8f42d9
7 changed files with 77 additions and 0 deletions

View File

@ -18,6 +18,8 @@ ignition:
- local: service/coturn/spec.ign
- local: service/radicale/spec.ign
- local: service/navidrome/spec.ign
- local: service/rclone/spec.ign
- local: service/hugo/spec.ign
- local: private/spec.ign
passwd:

View File

@ -19,6 +19,8 @@ ignition:
- local: service/spectrum/spec.ign
- local: service/radicale/spec.ign
- local: service/navidrome/spec.ign
- local: service/rclone/spec.ign
- local: service/hugo/spec.ign
- local: private/spec.ign
passwd:

View File

@ -22,6 +22,7 @@ ignition:
- local: service/writefreely/spec.ign
- local: service/coturn/spec.ign
- local: service/rclone/spec.ign
- local: service/hugo/spec.ign
passwd:
users:

View File

@ -0,0 +1,15 @@
FROM docker.io/debian:bullseye-slim
ARG VERSION=0.97.2
RUN apt-get update -y && apt-get install -y --no-install-recommends \
ca-certificates curl git inotify-tools gosu
ENV PACKAGE_URL https://github.com/gohugoio/hugo/releases/download/v${VERSION}/hugo_${VERSION}_Linux-64bit.tar.gz
RUN curl -L ${PACKAGE_URL} | tar -C /bin --no-same-owner -xvzf - hugo && chmod +x /bin/hugo
RUN apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false curl
RUN addgroup --system --gid 10000 hugo
RUN adduser --system --uid 10000 --ingroup hugo --no-create-home hugo
COPY container/run-hugo /run-hugo
ENTRYPOINT ["/run-hugo"]

28
service/hugo/container/run-hugo Executable file
View File

@ -0,0 +1,28 @@
#!/bin/sh
set -eu
# Set up shared variables.
GIT_REPO="$1"
GIT_BRANCH="$2"
# Ensure correct usage and state.
if test -z "$GIT_REPO"; then
echo "Git repository path is empty, please provide a valid path as the first argument."
exit 1
elif test -z "$GIT_BRANCH"; then
echo "Git branch name is empty, please provide a valid branch name as the second argument."
exit 1
elif test ! -e "/src/${GIT_REPO}/refs/heads/${GIT_BRANCH}"; then
echo "Unable to find HEAD reference for '${GIT_REPO}', branch '${GIT_BRANCH}', is this a valid Git repository?"
exit 2
fi
chown -R hugo:hugo /dest
# Wait for updates to watched branch for the given site, and build static content anew.
while true; do
gosu hugo /usr/bin/git clone --quiet --shared "/src/${GIT_REPO}" /tmp/src
gosu hugo /bin/hugo --verbose --source /tmp/src --destination /dest && rm -Rf /tmp/src
inotifywait -qq --event modify "/src/${GIT_REPO}/refs/heads/${GIT_BRANCH}" && break
done

8
service/hugo/spec.bu Normal file
View File

@ -0,0 +1,8 @@
variant: fcos
version: 1.3.0
storage:
trees:
- path: /etc/coreos-home-server/hugo
local: service/hugo/
- path: /etc/systemd/system
local: service/hugo/systemd/

View File

@ -0,0 +1,21 @@
[Unit]
Description=Hugo Build for Local %I Repository
Wants=container-build@hugo.service container-volume@hugo-build-%i.service git.service
After=container-build@hugo.service container-volume@hugo-build-%i.service git.service
[Service]
Type=notify
NotifyAccess=all
SyslogIdentifier=%N
Restart=on-failure
Environment=PODMAN_SYSTEMD_UNIT=%n
Environment=GIT_REPO=%i.git GIT_BRANCH=master
ExecStart=/bin/podman run --replace --name %p-%i --net internal --sdnotify=conmon \
--volume git:/src:z,ro \
--volume hugo-build-%i:/dest:z \
localhost/hugo:latest $GIT_REPO $GIT_BRANCH
ExecStop=/bin/podman stop --ignore --time 10 %p-%i
ExecStopPost=/bin/podman rm --ignore --force %p-%i
[Install]
WantedBy=multi-user.target