Merge pull request 'Improve Hugo Configuration' (#9) from dev into main

Reviewed-on: #9
This commit is contained in:
2025-08-17 17:06:11 -07:00
6 changed files with 66 additions and 25 deletions

View File

@@ -8,19 +8,21 @@ FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y git hugo nginx cron curl bash sudo htop RUN apt-get update && apt-get install -y git hugo nginx cron curl bash sudo htop
# User and Group # User and Group
RUN groupadd -r hugo && useradd -r -g hugo hugo #RUN groupadd -r hugo && useradd -r -g hugo hugo
# Hugo Directory Tree # 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 # 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 Cron Job to Update Git Repo
COPY files/hugo.crontab /etc/cron.d/hugo COPY files/crontab /etc/crontab
COPY files/hugo.cronjob.sh /var/www/hugo/cronjob.sh COPY files/cronjob.sh /var/www/hugo/cronjob.sh
# Hugo User Permissions
RUN chown -Rv www-data:www-data /var/www/
RUN chmod +x /var/www/hugo/cronjob.sh RUN chmod +x /var/www/hugo/cronjob.sh
RUN crontab /etc/cron.d/hugo
# Copy Start Script # Copy Start Script
COPY files/main.sh /root/main.sh COPY files/main.sh /root/main.sh

View File

@@ -0,0 +1,26 @@
#!/usr/bin/env bash
cd /var/www/hugo/site
echo "*** Running cronjob @ `date` ***"
# 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

View File

@@ -0,0 +1 @@
* * * * * www-data /var/www/hugo/cronjob.sh

View File

@@ -1,10 +0,0 @@
#!/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/

View File

@@ -1 +0,0 @@
* * * * * hugo /var/www/hugo/cronjob.sh

View File

@@ -12,22 +12,36 @@ typeset -u DEV
echo "DEV=$DEV" echo "DEV=$DEV"
echo "*** Creating Git Repo ***" 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 ***"
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 "* Site exists!"
echo "*** Copying Static Files to NGINX ***" echo "*** Copying Static Files to NGINX ***"
sudo -u hugo /var/www/hugo/cronjob.sh rm -rfv /var/www/html/*
sudo -u www-data /var/www/hugo/cronjob.sh
echo "*** Starting Cron ***" echo "*** Starting Cron ***"
service cron start service cron start
service cron status
if [[ "$PROD" == "Y"* || "$PROD" == "T"* ]]; then if [[ "$PROD" == "Y"* || "$PROD" == "T"* ]]; then
echo "*** Starting Production Server Loop ***" echo "*** Starting Production Server Loop ***"
while true; do 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..." echo "* Prod server not detected, starting..."
service nginx status
service nginx start service nginx start
} service nginx status
fi
sleep 15 sleep 15
done & done &
@@ -39,19 +53,28 @@ if [[ "$DEV" == "Y"* || "$DEV" == "T"* ]]; then
echo "*** Starting Development Server Loop ***" echo "*** Starting Development Server Loop ***"
while true; do 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..." echo "* Dev server not detected, starting..."
cd /var/www/hugo/site cd /var/www/hugo/site
killall hugo 2>/dev/null 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 sleep 30
done & done &
fi 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 cd
echo "*** Finished $0 ***" echo "*** Finished $0 @ `date` ***"
wait -n wait -n