Fix: Improve sync script mechanism and pm2 path

This commit is contained in:
2025-12-27 19:01:50 +11:00
parent d6cf8f6603
commit 719096be79

View File

@@ -4,6 +4,9 @@
APP_DIR="/var/www/Advanced-Smtp-Tester" APP_DIR="/var/www/Advanced-Smtp-Tester"
LOG_FILE="/var/log/smtp-tester-sync.log" LOG_FILE="/var/log/smtp-tester-sync.log"
# Add /usr/local/bin to PATH to ensure PM2 is found
export PATH=$PATH:/usr/local/bin
# Navigate to app directory # Navigate to app directory
cd "$APP_DIR" || exit 1 cd "$APP_DIR" || exit 1
@@ -12,33 +15,38 @@ log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE" echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
} }
log "Checking for updates..."
# Fetch latest changes # Fetch latest changes
git fetch origin git fetch origin
# Check if there are changes # Check if we are behind
HEADHASH=$(git rev-parse HEAD) LOCAL=$(git rev-parse HEAD)
UPSTREAMHASH=$(git rev-parse origin/main) REMOTE=$(git rev-parse @{u}) # This gets the upstream commit for the current branch
if [ "$HEADHASH" != "$UPSTREAMHASH" ]; then if [ "$LOCAL" != "$REMOTE" ]; then
log "Changes detected. Updating..." log "Changes detected. Updating..."
# Pull changes # Force reset to match origin (avoids merge conflicts)
git pull origin main >> "$LOG_FILE" 2>&1 git reset --hard origin/main >> "$LOG_FILE" 2>&1
# Check if package.json changed # Check if package.json changed (compare old local HEAD with new remote HEAD)
if git diff --name-only "$HEADHASH" "$UPSTREAMHASH" | grep "package.json"; then # Note: git diff --name-only $LOCAL $REMOTE would compare the old local HEAD with the new remote HEAD
# After git reset --hard, HEAD is already at REMOTE.
# To check if package.json changed *between the old state and the new state*,
# we need to compare the old LOCAL with the new REMOTE.
if git diff --name-only "$LOCAL" "$REMOTE" | grep "package.json"; then
log "package.json changed. Installing dependencies..." log "package.json changed. Installing dependencies..."
npm install >> "$LOG_FILE" 2>&1 npm install >> "$LOG_FILE" 2>&1
fi fi
# Restart application
log "Restarting application..." log "Restarting application..."
pm2 reload all >> "$LOG_FILE" 2>&1 pm2 restart Advanced-Smtp-Tester >> "$LOG_FILE" 2>&1
log "Update complete." log "Update complete."
else else
# Uncomment next line for verbose logging of no-changes # Uncomment next line for verbose logging of no-changes
# log "No changes detected." # log "No changes detected."
# echo "No changes." # echo "No changes."
: log "No changes found."
fi fi