Add automatic book deployment

This commit is contained in:
Simon 2026-08-20 09:19:11 +01:00
parent 0e87a3b71d
commit 134cb8aa49
3 changed files with 65 additions and 0 deletions

44
deploy/deploy-eacompanion Normal file
View file

@ -0,0 +1,44 @@
#!/bin/bash
set -euo pipefail
repository_url="https://codeberg.org/simoncarrignon/EACompanion.git"
repository_dir="/srv/EACompanion"
web_root="/var/www/html/randomdrift"
releases_dir="${web_root}/.EACompanion-releases"
public_link="${web_root}/EACompanion"
exec 9>/run/lock/eacompanion-deploy.lock
flock -n 9 || exit 0
if [[ ! -d "${repository_dir}/.git" ]]; then
git clone --branch main --single-branch "${repository_url}" "${repository_dir}"
fi
git -C "${repository_dir}" fetch --prune origin main
revision="$(git -C "${repository_dir}" rev-parse FETCH_HEAD)"
release_dir="${releases_dir}/${revision}"
if [[ -f "${release_dir}/index.html" ]] && [[ "$(readlink -f "${public_link}" 2>/dev/null || true)" == "${release_dir}" ]]; then
chmod 0755 "${release_dir}"
exit 0
fi
git -C "${repository_dir}" checkout --detach --force "${revision}"
mkdir -p "${releases_dir}"
if [[ ! -f "${release_dir}/index.html" ]]; then
staging_dir="$(mktemp -d "${releases_dir}/.${revision}.XXXXXX")"
trap 'rm -rf -- "${staging_dir}"' EXIT
cd "${repository_dir}"
EA_OUTPUT_DIR="${staging_dir}" Rscript -e \
'bookdown::render_book("index.Rmd", output_format = "bookdown::gitbook", output_dir = Sys.getenv("EA_OUTPUT_DIR"), clean = TRUE)'
test -f "${staging_dir}/index.html"
mv "${staging_dir}" "${release_dir}"
trap - EXIT
fi
chmod 0755 "${release_dir}"
temporary_link="${public_link}.new"
ln -sfn "${release_dir}" "${temporary_link}"
mv -Tf "${temporary_link}" "${public_link}"

View file

@ -0,0 +1,9 @@
[Unit]
Description=Render and deploy the EA Companion book
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
Environment=HOME=/root
ExecStart=/usr/local/sbin/deploy-eacompanion

View file

@ -0,0 +1,12 @@
[Unit]
Description=Check the EA Companion main branch for updates
[Timer]
OnBootSec=1min
OnUnitActiveSec=2min
RandomizedDelaySec=20s
Persistent=true
Unit=eacompanion-deploy.service
[Install]
WantedBy=timers.target