From dfb9a306c593a30daeba56158299ad3e480929db Mon Sep 17 00:00:00 2001 From: Hyperling Date: Fri, 15 Aug 2025 11:43:46 -0700 Subject: [PATCH 1/4] Allow copies of Hugo configuration to exist outside of the Git project. --- .gitignore | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ba5d670..8d1d62c 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,10 @@ docker-compose.yml # 2024-01-24 Hide static files for Hyperling.com. Config/Hyperling.com/files/* -# Ignore things like Config/Hyperling.com-Stage/ +# Ignore things like "Config/Hyperling.com-Stage/"" *-Stage Stage-* + +# Ignore copies of the Hugo configuration, such as "Config/Hugo-MyWebsite". +Hugo-* +*-Hugo -- 2.49.1 From d035f9d8e761950f573a2a24c8027fda00a2df51 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Fri, 15 Aug 2025 11:45:23 -0700 Subject: [PATCH 2/4] Refactor how the project is run, using `nginx` instead of `hugo server`, a cron job to pull git changes, and a start script to ensure the correct services are started each run and are monitored to stay up. --- Config/HugoExample/Dockerfile | 37 +++++++----- Config/HugoExample/docker-compose.example.yml | 13 ++++- Config/HugoExample/env.example | 10 ++++ Config/HugoExample/files/hugo.cronjob.sh | 10 ++++ Config/HugoExample/files/hugo.crontab | 1 + Config/HugoExample/files/main.sh | 58 +++++++++++++++++++ 6 files changed, 112 insertions(+), 17 deletions(-) create mode 100644 Config/HugoExample/files/hugo.cronjob.sh create mode 100644 Config/HugoExample/files/hugo.crontab create mode 100644 Config/HugoExample/files/main.sh diff --git a/Config/HugoExample/Dockerfile b/Config/HugoExample/Dockerfile index dc98984..d9f1d3b 100644 --- a/Config/HugoExample/Dockerfile +++ b/Config/HugoExample/Dockerfile @@ -4,25 +4,32 @@ FROM debian:bookworm-slim ## Setup ## -# Cache System Dependencies -RUN apt-get update && apt-get install -y git hugo sudo curl wget bash +# System Dependencies +RUN apt-get update && apt-get install -y git hugo nginx cron curl bash sudo htop +RUN service nginx start +RUN service cron start # User and Group -RUN groupadd -r hugo && useradd -r -s /usr/bin/bash -g hugo hugo +RUN groupadd -r hugo && useradd -r -g hugo hugo -# Directory Tree +# Hugo Directory Tree RUN mkdir -pv /var/www/hugo && chown -Rv hugo:hugo /var/www/hugo +# NGINX Directory Tree +RUN mkdir -pv /var/www/html/ && chown -Rv hugo:hugo /var/www/html/ + +# Copy Cron Job to Update Git Repo +COPY files/hugo.crontab /etc/cron.d/hugo +COPY files/hugo.cronjob.sh /var/www/hugo/cronjob.sh +RUN chmod +x /var/www/hugo/cronjob.sh +RUN crontab /etc/cron.d/hugo + +# Copy Start Script +COPY files/main.sh /root/main.sh +RUN chmod +x /root/main.sh + ## Main ## # Install + Run Website -WORKDIR /var/www/hugo -USER hugo -CMD echo "*** Cloning Repo $REPO ***" && \ - cd /var/www/hugo && \ - rm -rfv site && \ - git clone --recurse-submodules $REPO site && \ - cd site && \ - mkdir -pv public && \ - echo "*** Starting Hugo ***" && \ - hugo version && \ - hugo server -D --noBuildLock --bind 0.0.0.0 +WORKDIR /var/www/ +USER root +CMD /root/main.sh "$REPO" "$PROD" "$DEV" diff --git a/Config/HugoExample/docker-compose.example.yml b/Config/HugoExample/docker-compose.example.yml index 8e5a5e2..fafeb46 100644 --- a/Config/HugoExample/docker-compose.example.yml +++ b/Config/HugoExample/docker-compose.example.yml @@ -9,12 +9,21 @@ services: network: host restart: always ports: - - 8013:1313 + - 8013:80 # Production minified files served using NGINX. + - 1380:1380 # Development files with drafts served by Hugo Server. environment: - REPO=$REPO + - PROD=$PROD + - DEV=$DEV + healthcheck: + test: curl -sS http://localhost:80 || curl -sS http://localhost:1380 || exit 1 + interval: 1m + timeout: 10s + retries: 2 + start_period: 30s deploy: mode: global resources: limits: cpus: '0.10' - memory: 32M + memory: 64M diff --git a/Config/HugoExample/env.example b/Config/HugoExample/env.example index 7657cb4..fb87950 100644 --- a/Config/HugoExample/env.example +++ b/Config/HugoExample/env.example @@ -9,3 +9,13 @@ COMPOSE_BAKE=true ## Git Website Repository # REPO=https://git.hyperling.com/me/hugo-jackanope + +# +## Web Environments +# Please use values YES/TRUE and NO/FALSE. + +# Whether to start NGINX +PROD=YES + +# Whether to start Hugo Server +DEV=NO diff --git a/Config/HugoExample/files/hugo.cronjob.sh b/Config/HugoExample/files/hugo.cronjob.sh new file mode 100644 index 0000000..8bb86b7 --- /dev/null +++ b/Config/HugoExample/files/hugo.cronjob.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +cd /var/www/hugo/site + +git pull + +hugo --gc --minify + +rm -rfv /var/www/html/* +cp -rfv public/* /var/www/html/ diff --git a/Config/HugoExample/files/hugo.crontab b/Config/HugoExample/files/hugo.crontab new file mode 100644 index 0000000..4bfa3ec --- /dev/null +++ b/Config/HugoExample/files/hugo.crontab @@ -0,0 +1 @@ +* * * * * hugo /var/www/hugo/cronjob.sh diff --git a/Config/HugoExample/files/main.sh b/Config/HugoExample/files/main.sh new file mode 100644 index 0000000..beb9070 --- /dev/null +++ b/Config/HugoExample/files/main.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash + +REPO="$1" +echo "REPO=$REPO" + +PROD="$2" +typeset -u PROD +echo "PROD=$PROD" + +DEV="$3" +typeset -u DEV +echo "DEV=$DEV" + +echo "*** Creating Git Repo ***" +sudo -u hugo git clone --recurse-submodules $REPO /var/www/hugo/site + +echo "*** Copying Static Files to NGINX ***" +sudo -u hugo /var/www/hugo/cronjob.sh + +echo "*** Starting Cron ***" +service cron start + +if [[ "$PROD" == "Y"* || "$PROD" == "T"* ]]; then + echo "*** Starting Production Server Loop ***" + + while true; do + curl -sS http://localhost:80 > /dev/null || { + echo "* Prod server not detected, starting..." + service nginx start + } + sleep 30 + done & + + cd /var/log/nginx + tail -f access.log error.log & +fi + +if [[ "$DEV" == "Y"* || "$DEV" == "T"* ]]; then + echo "*** Starting Development Server Loop ***" + + while true; do + curl -sS http://localhost:1380 > /dev/null || { + echo "* Dev server not detected, starting..." + cd /var/www/hugo/site + killall hugo 2>/dev/null + sudo -u hugo hugo server -D --noBuildLock --bind 0.0.0.0 -p 1380 & + } + sleep 10 + done & +fi + +cd + +echo "*** Finished $0 ***" + +wait -n + +exit $? -- 2.49.1 From 6dac535211000b1159a136af822f4b38cfb2fb7c Mon Sep 17 00:00:00 2001 From: Hyperling Date: Fri, 15 Aug 2025 11:46:23 -0700 Subject: [PATCH 3/4] Remove running service commands since they don't actually keep the programs enabled. --- Config/HugoExample/Dockerfile | 2 -- 1 file changed, 2 deletions(-) diff --git a/Config/HugoExample/Dockerfile b/Config/HugoExample/Dockerfile index d9f1d3b..f55ff7b 100644 --- a/Config/HugoExample/Dockerfile +++ b/Config/HugoExample/Dockerfile @@ -6,8 +6,6 @@ FROM debian:bookworm-slim ## Setup ## # System Dependencies RUN apt-get update && apt-get install -y git hugo nginx cron curl bash sudo htop -RUN service nginx start -RUN service cron start # User and Group RUN groupadd -r hugo && useradd -r -g hugo hugo -- 2.49.1 From 5323b6647f3b4815636d768159c2a50c59ee68b5 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Fri, 15 Aug 2025 11:49:00 -0700 Subject: [PATCH 4/4] Change restart loop timers. --- Config/HugoExample/files/main.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Config/HugoExample/files/main.sh b/Config/HugoExample/files/main.sh index beb9070..75bad27 100644 --- a/Config/HugoExample/files/main.sh +++ b/Config/HugoExample/files/main.sh @@ -28,7 +28,7 @@ if [[ "$PROD" == "Y"* || "$PROD" == "T"* ]]; then echo "* Prod server not detected, starting..." service nginx start } - sleep 30 + sleep 15 done & cd /var/log/nginx @@ -45,7 +45,7 @@ if [[ "$DEV" == "Y"* || "$DEV" == "T"* ]]; then killall hugo 2>/dev/null sudo -u hugo hugo server -D --noBuildLock --bind 0.0.0.0 -p 1380 & } - sleep 10 + sleep 30 done & fi -- 2.49.1