From dbb54b6f81e459871017ff35215c547fbf7a9c15 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Sun, 17 Aug 2025 09:01:24 -0700 Subject: [PATCH 1/9] Do not continue if the git repo does not exist. --- Config/HugoExample/files/main.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Config/HugoExample/files/main.sh b/Config/HugoExample/files/main.sh index 75bad27..7a78f39 100644 --- a/Config/HugoExample/files/main.sh +++ b/Config/HugoExample/files/main.sh @@ -13,6 +13,14 @@ echo "DEV=$DEV" echo "*** Creating Git Repo ***" sudo -u hugo git clone --recurse-submodules $REPO /var/www/hugo/site +status="$?" + +echo "*** Validating Git Repo ***" +if [[ $status != 0 || ! -d /var/www/hugo/site/.git ]]; then + echo "ERROR: Hugo project may not have cloned correctly. status='$status'" + echo "Aborting." + exit 1 +fi echo "*** Copying Static Files to NGINX ***" sudo -u hugo /var/www/hugo/cronjob.sh From ddeadcf7232d0b43d599ab26c10adff2e4ecc1b2 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Sun, 17 Aug 2025 09:12:23 -0700 Subject: [PATCH 2/9] Add safeguards to cron job so that website does not get deleted without having replacement files. --- Config/HugoExample/files/hugo.cronjob.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Config/HugoExample/files/hugo.cronjob.sh b/Config/HugoExample/files/hugo.cronjob.sh index 8bb86b7..0e215bc 100644 --- a/Config/HugoExample/files/hugo.cronjob.sh +++ b/Config/HugoExample/files/hugo.cronjob.sh @@ -2,9 +2,10 @@ cd /var/www/hugo/site -git pull +git pull && hugo --gc --minify +status="$?" -hugo --gc --minify - -rm -rfv /var/www/html/* -cp -rfv public/* /var/www/html/ +if [[ $status == 0 && -e public/* ]]; then + rm -rfv /var/www/html/* + mv -rfv public/* /var/www/html/ +fi From 57ade9f67d4cff2486e560ba81ae4569ef8ee3b2 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Sun, 17 Aug 2025 11:00:34 -0700 Subject: [PATCH 3/9] Give the cronjob some safety so that it churns less. --- Config/HugoExample/files/hugo.cronjob.sh | 25 +++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/Config/HugoExample/files/hugo.cronjob.sh b/Config/HugoExample/files/hugo.cronjob.sh index 0e215bc..88accb9 100644 --- a/Config/HugoExample/files/hugo.cronjob.sh +++ b/Config/HugoExample/files/hugo.cronjob.sh @@ -2,10 +2,25 @@ cd /var/www/hugo/site -git pull && hugo --gc --minify -status="$?" +echo "*** Running cronjob @ `date` ***" -if [[ $status == 0 && -e public/* ]]; then - rm -rfv /var/www/html/* - mv -rfv public/* /var/www/html/ +# Pull any updates, and if the project is already up to date, exit successfully. +git pull | grep -v "up to date" +status="$?" +echo "* Pull status is '$status'." +if [[ $status != 0 && -e /var/www/html/index.html ]]; then + echo "* Site is already up to date and copied, exiting." + exit 0 fi + +# If the project was not already up to date, build the new files for NGINX. +hugo --gc --minify +status="$?" +echo "* Hugo status is '$status'." +if [[ $status == 0 && -e public/index.html ]]; then + echo "* Copying files..." + rm -rfv /var/www/html/* + mv -v public/* /var/www/html/ +fi + +exit 0 From d6e1186d86a06c09ff0ec6f58aa79db68520ab18 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Sun, 17 Aug 2025 11:01:10 -0700 Subject: [PATCH 4/9] Modify script to remove NGINX default files and check HTTP codes rather than curl response status. --- Config/HugoExample/files/main.sh | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Config/HugoExample/files/main.sh b/Config/HugoExample/files/main.sh index 7a78f39..77049d0 100644 --- a/Config/HugoExample/files/main.sh +++ b/Config/HugoExample/files/main.sh @@ -21,21 +21,27 @@ if [[ $status != 0 || ! -d /var/www/hugo/site/.git ]]; then echo "Aborting." exit 1 fi +echo "* Site exists!" echo "*** Copying Static Files to NGINX ***" +rm -rfv /var/www/html/* sudo -u hugo /var/www/hugo/cronjob.sh echo "*** Starting Cron ***" service cron start +service cron status if [[ "$PROD" == "Y"* || "$PROD" == "T"* ]]; then echo "*** Starting Production Server Loop ***" while true; do - curl -sS http://localhost:80 > /dev/null || { + http_code="`curl -sS http://localhost:80 -o /dev/null -w "%{http_code}"`" + if [[ $http_code != 200 ]]; then echo "* Prod server not detected, starting..." + service nginx status service nginx start - } + service nginx status + fi sleep 15 done & @@ -47,19 +53,20 @@ if [[ "$DEV" == "Y"* || "$DEV" == "T"* ]]; then echo "*** Starting Development Server Loop ***" while true; do - curl -sS http://localhost:1380 > /dev/null || { + http_code="`curl -sS http://localhost:1380 -o /dev/null -w "%{http_code}"`" + if [[ $http_code != 200 ]]; then 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 & - } + fi sleep 30 done & fi cd -echo "*** Finished $0 ***" +echo "*** Finished $0 @ `date` ***" wait -n From 4dd0661e1e8cffbe68cb6d5ad73628d161090d33 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Sun, 17 Aug 2025 11:02:13 -0700 Subject: [PATCH 5/9] Move permission commands to a single command after the cronjob file is copied. --- Config/HugoExample/Dockerfile | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Config/HugoExample/Dockerfile b/Config/HugoExample/Dockerfile index f55ff7b..9190490 100644 --- a/Config/HugoExample/Dockerfile +++ b/Config/HugoExample/Dockerfile @@ -11,17 +11,20 @@ RUN apt-get update && apt-get install -y git hugo nginx cron curl bash sudo htop RUN groupadd -r hugo && useradd -r -g hugo hugo # Hugo Directory Tree -RUN mkdir -pv /var/www/hugo && chown -Rv hugo:hugo /var/www/hugo +RUN mkdir -pv /var/www/hugo/ # NGINX Directory Tree -RUN mkdir -pv /var/www/html/ && chown -Rv hugo:hugo /var/www/html/ +RUN mkdir -pv /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 +# Hugo User Permissions +RUN chown -Rv hugo:hugo /var/www/ +RUN chmod +x /var/www/hugo/cronjob.sh + # Copy Start Script COPY files/main.sh /root/main.sh RUN chmod +x /root/main.sh From cfb07a6e9333d9090c96fa91c48b9548d590ac8b Mon Sep 17 00:00:00 2001 From: Hyperling Date: Sun, 17 Aug 2025 11:26:47 -0700 Subject: [PATCH 6/9] Use the www-data user rather than creating a hugo user. --- Config/HugoExample/Dockerfile | 4 ++-- Config/HugoExample/files/hugo.crontab | 2 +- Config/HugoExample/files/main.sh | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Config/HugoExample/Dockerfile b/Config/HugoExample/Dockerfile index 9190490..778195b 100644 --- a/Config/HugoExample/Dockerfile +++ b/Config/HugoExample/Dockerfile @@ -8,7 +8,7 @@ FROM debian:bookworm-slim RUN apt-get update && apt-get install -y git hugo nginx cron curl bash sudo htop # User and Group -RUN groupadd -r hugo && useradd -r -g hugo hugo +#RUN groupadd -r hugo && useradd -r -g hugo hugo # Hugo Directory Tree RUN mkdir -pv /var/www/hugo/ @@ -22,7 +22,7 @@ COPY files/hugo.cronjob.sh /var/www/hugo/cronjob.sh RUN crontab /etc/cron.d/hugo # Hugo User Permissions -RUN chown -Rv hugo:hugo /var/www/ +RUN chown -Rv www-data:www-data /var/www/ RUN chmod +x /var/www/hugo/cronjob.sh # Copy Start Script diff --git a/Config/HugoExample/files/hugo.crontab b/Config/HugoExample/files/hugo.crontab index 4bfa3ec..cd21442 100644 --- a/Config/HugoExample/files/hugo.crontab +++ b/Config/HugoExample/files/hugo.crontab @@ -1 +1 @@ -* * * * * hugo /var/www/hugo/cronjob.sh +* * * * * www-data /var/www/hugo/cronjob.sh diff --git a/Config/HugoExample/files/main.sh b/Config/HugoExample/files/main.sh index 77049d0..5ee4ac1 100644 --- a/Config/HugoExample/files/main.sh +++ b/Config/HugoExample/files/main.sh @@ -12,7 +12,7 @@ typeset -u DEV echo "DEV=$DEV" echo "*** Creating Git Repo ***" -sudo -u hugo git clone --recurse-submodules $REPO /var/www/hugo/site +sudo -u www-data git clone --recurse-submodules $REPO /var/www/hugo/site status="$?" echo "*** Validating Git Repo ***" @@ -25,7 +25,7 @@ echo "* Site exists!" echo "*** Copying Static Files to NGINX ***" rm -rfv /var/www/html/* -sudo -u hugo /var/www/hugo/cronjob.sh +sudo -u www-data /var/www/hugo/cronjob.sh echo "*** Starting Cron ***" service cron start @@ -58,7 +58,7 @@ if [[ "$DEV" == "Y"* || "$DEV" == "T"* ]]; then 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 & + sudo -u www-data hugo server -D --noBuildLock --bind 0.0.0.0 -p 1380 & fi sleep 30 done & From e86660d34e198d6e95fad33a4ede67575774c90b Mon Sep 17 00:00:00 2001 From: Hyperling Date: Sun, 17 Aug 2025 11:43:54 -0700 Subject: [PATCH 7/9] Using the crontab command adds the job to root's jobs. Allow cron to pick up the file automatically by not running any explicit calls. --- Config/HugoExample/Dockerfile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Config/HugoExample/Dockerfile b/Config/HugoExample/Dockerfile index 778195b..510f709 100644 --- a/Config/HugoExample/Dockerfile +++ b/Config/HugoExample/Dockerfile @@ -17,9 +17,8 @@ RUN mkdir -pv /var/www/hugo/ RUN mkdir -pv /var/www/html/ # Copy Cron Job to Update Git Repo -COPY files/hugo.crontab /etc/cron.d/hugo +COPY files/hugo.crontab /etc/crontab COPY files/hugo.cronjob.sh /var/www/hugo/cronjob.sh -RUN crontab /etc/cron.d/hugo # Hugo User Permissions RUN chown -Rv www-data:www-data /var/www/ From f622cba366e98994095197db987f5fdc0fd1e618 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Sun, 17 Aug 2025 11:44:16 -0700 Subject: [PATCH 8/9] Add the mail files to the container's log output. --- Config/HugoExample/files/main.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Config/HugoExample/files/main.sh b/Config/HugoExample/files/main.sh index 5ee4ac1..73771a9 100644 --- a/Config/HugoExample/files/main.sh +++ b/Config/HugoExample/files/main.sh @@ -64,6 +64,14 @@ if [[ "$DEV" == "Y"* || "$DEV" == "T"* ]]; then done & fi +echo "*** Following Mail Files ***" +cd /var/mail +touch mail www-data +chown -v mail:mail mail +chown -v www-data:mail www-data +chmod -v 660 mail www-data +tail -f mail www-data & + cd echo "*** Finished $0 @ `date` ***" From d7a1e6f58a9ee3f32bbdfe4b336c9f07641e093c Mon Sep 17 00:00:00 2001 From: Hyperling Date: Sun, 17 Aug 2025 11:49:05 -0700 Subject: [PATCH 9/9] Remove hugo user reference from files. --- Config/HugoExample/Dockerfile | 4 ++-- Config/HugoExample/files/{hugo.cronjob.sh => cronjob.sh} | 0 Config/HugoExample/files/{hugo.crontab => crontab} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename Config/HugoExample/files/{hugo.cronjob.sh => cronjob.sh} (100%) rename Config/HugoExample/files/{hugo.crontab => crontab} (100%) diff --git a/Config/HugoExample/Dockerfile b/Config/HugoExample/Dockerfile index 510f709..01a4ae5 100644 --- a/Config/HugoExample/Dockerfile +++ b/Config/HugoExample/Dockerfile @@ -17,8 +17,8 @@ RUN mkdir -pv /var/www/hugo/ RUN mkdir -pv /var/www/html/ # Copy Cron Job to Update Git Repo -COPY files/hugo.crontab /etc/crontab -COPY files/hugo.cronjob.sh /var/www/hugo/cronjob.sh +COPY files/crontab /etc/crontab +COPY files/cronjob.sh /var/www/hugo/cronjob.sh # Hugo User Permissions RUN chown -Rv www-data:www-data /var/www/ diff --git a/Config/HugoExample/files/hugo.cronjob.sh b/Config/HugoExample/files/cronjob.sh similarity index 100% rename from Config/HugoExample/files/hugo.cronjob.sh rename to Config/HugoExample/files/cronjob.sh diff --git a/Config/HugoExample/files/hugo.crontab b/Config/HugoExample/files/crontab similarity index 100% rename from Config/HugoExample/files/hugo.crontab rename to Config/HugoExample/files/crontab