Files
nodejs-website/cronjob.sh
2025-10-14 14:03:28 -07:00

67 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# 2025-10-14 Hyperling
# Copied cronjob.sh from env-docker/Config/Hugo-Example/files/ to use for this
# project so that docker container can do periodic git pulls rather than having
# to reload /rebuild the container each time a release is pushed out.
## Setup ##
DIR="$(dirname -- "${BASH_SOURCE[0]}")"
PROG="$(basename -- "${BASH_SOURCE[0]}")"
cd $DIR
DIR="`pwd`"
NAME="'$PROG'"
function log {
echo -e "`date` : $NAME - $1"
}
function kill-project {
# Kill node.js which will complete run.sh and restart any Docker containers.
# This is more intended towards Development and Stage sites since Production
# will only see git changes when a pull request is manually completed.
log "Stopping continuous processes!"
pkill node
}
function reload-project {
# Nothing to do, run.sh and main.js automatically uses the latest files.
log "Project reloaded successfully!"
}
## Main ##
# Pull any updates, and if the project is already up to date, exit successfully.
output="`git pull`"
git_status="$?"
echo "$output" | grep -v "up to date"
grep_status="$?"
log "Pull status is '$git_status', checking for changes is '$grep_status'."
# Check whether the continuously running jobs have been updated.
echo "$output" | grep "main.js"
main_changed="$?"
echo "$output" | grep "run.sh"
run_changed="$?"
# Determine where we've landed and whether we need to do anything.
if [[ $git_status != 0 ]]; then
log "*** ERROR: Git reported a failure! ***"
exit 1
elif [[ $git_status == 0 && ($main_changed == 0 || $run_changed == 0) ]]; then
log "Either main ('$main_changed'), or run ('$run_changed') were changed!"
kill-project
elif [[ $git_status == 0 && $grep_status == 0 ]]; then
reload-project
elif [[ $git_status == 0 && $grep_status != 0 ]]; then
log "Nothing to do. '$output'"
else
log "*** WARNING: Unknown Situation ***"
fi
## Success! ##
exit 0