From 3fec68a2a9238393fec3bdc605b8f894c674496e Mon Sep 17 00:00:00 2001 From: Ettore Dreucci Date: Tue, 7 Oct 2025 22:41:25 +0200 Subject: [PATCH] First try --- .gitignore | 2 ++ Dockerfile | 32 +++++++++++++++++++ docker-compose.yml | 15 +++++++++ entrypoint.sh | 75 ++++++++++++++++++++++++++++++++++++++++++++ hooks/update_site.sh | 25 +++++++++++++++ 5 files changed, 149 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 entrypoint.sh create mode 100644 hooks/update_site.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3bf780b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a8cbd52 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,32 @@ +FROM debian:bookworm-slim + +ENV DEBIAN_FRONTEND=noninteractive + +# Install dependencies +RUN apt-get update \ + && apt-get install -y --no-install-recommends git ca-certificates curl unzip bash gnupg \ + && rm -rf /var/lib/apt/lists/* + +# Install Hugo (Linux 64bit) +RUN curl -fsSL "https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_Linux-64bit.tar.gz" \ + | tar -xzf - -C /tmp \ + && mv /tmp/hugo /usr/local/bin/hugo \ + && chmod +x /usr/local/bin/hugo \ + && rm -rf /tmp/hugo + +# Install adnanh/webhook binary +RUN curl -fsSL "https://github.com/adnanh/webhook/releases/download/${WEBHOOK_VERSION}/webhook-linux-amd64.tar.gz" \ + | tar -xzf - -C /tmp \ + && mv /tmp/webhook-linux-amd64/webhook /usr/local/bin/webhook \ + && chmod +x /usr/local/bin/webhook \ + && rm -rf /tmp/webhook-linux-amd64 + +# App layout +WORKDIR /app +COPY entrypoint.sh /app/entrypoint.sh +COPY hooks/update_site.sh /app/hooks/update_site.sh +RUN chmod +x /app/entrypoint.sh /app/hooks/update_site.sh + +EXPOSE 1313 ${WEBHOOK_PORT} + +ENTRYPOINT ["/app/entrypoint.sh"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..6c21842 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,15 @@ +services: + site: + build: ./builder + container_name: hugo-webhook + env_file: + - .env + ports: + - "1313:1313" # hugo server + - "9000:9000" # webhook listener (only expose to Gitea or reverse-proxy) + volumes: + - site-data:/srv/site + restart: unless-stopped + +volumes: + site-data: diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..684f119 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Environment variables (can be passed via docker-compose) +: "${GIT_REPO:?You must set GIT_REPO environment variable}" +GIT_BRANCH="${GIT_BRANCH:-main}" +WEBHOOK_SECRET="${WEBHOOK_SECRET:-}" +WEBHOOK_PORT="${WEBHOOK_PORT:-9000}" +HUGO_FLAGS="${HUGO_FLAGS:-}" +HUGO_BASEURL="${HUGO_BASEURL:-http://localhost:1313}" +REPO_DIR="/app/site" +HOOKS_FILE="/app/hooks.json" +WEBHOOK_BIN="/usr/local/bin/webhook" + +# Clone repo into volume if missing +if [ ! -d "$REPO_DIR/.git" ]; then + echo "Cloning $GIT_REPO (branch $GIT_BRANCH) into $REPO_DIR" + git clone --branch "$GIT_BRANCH" --single-branch "$GIT_REPO" "$REPO_DIR" +else + echo "Repository already exists at $REPO_DIR" +fi + +# Ensure working tree matches origin branch +git -C "$REPO_DIR" fetch origin "$GIT_BRANCH" || true +git -C "$REPO_DIR" reset --hard "origin/$GIT_BRANCH" || true + +# Generate hooks.json for adnanh/webhook with secret and trigger rule for branch +cat > "$HOOKS_FILE" </dev/null || true +wait "$WEBHOOK_PID" 2>/dev/null || true diff --git a/hooks/update_site.sh b/hooks/update_site.sh new file mode 100644 index 0000000..4b8b696 --- /dev/null +++ b/hooks/update_site.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +set -euo pipefail + +# This script is invoked by adnanh/webhook when a matching hook is triggered. +# It updates the git checkout in /app/site to origin/$GIT_BRANCH. +# Hugo server runs separately and watches the files, so no explicit hugo build is required. + +REPO_DIR="/app/site" +GIT_BRANCH="${GIT_BRANCH:-main}" + +if [ ! -d "$REPO_DIR/.git" ]; then + echo "Repository not found at $REPO_DIR; nothing to update." + exit 1 +fi + +echo "Updating repository in $REPO_DIR to origin/$GIT_BRANCH" +cd "$REPO_DIR" + +# Fetch and reset +git fetch origin "$GIT_BRANCH" --depth=1 || true +git reset --hard "origin/$GIT_BRANCH" + +# Optionally, you can clear caches or perform other tasks here. +echo "Update completed at $(date -u +%Y-%m-%dT%H:%M:%SZ)" +exit 0