From 719096be7900cb8faaf56030af5e2d275934854e Mon Sep 17 00:00:00 2001 From: DeNNiiInc Date: Sat, 27 Dec 2025 19:01:50 +1100 Subject: [PATCH] Fix: Improve sync script mechanism and pm2 path --- scripts/sync-and-restart.sh | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/scripts/sync-and-restart.sh b/scripts/sync-and-restart.sh index f5a2462..c89f44b 100644 --- a/scripts/sync-and-restart.sh +++ b/scripts/sync-and-restart.sh @@ -4,6 +4,9 @@ APP_DIR="/var/www/Advanced-Smtp-Tester" 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 cd "$APP_DIR" || exit 1 @@ -12,33 +15,38 @@ log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE" } +log "Checking for updates..." + # Fetch latest changes git fetch origin -# Check if there are changes -HEADHASH=$(git rev-parse HEAD) -UPSTREAMHASH=$(git rev-parse origin/main) +# Check if we are behind +LOCAL=$(git rev-parse HEAD) +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..." - # Pull changes - git pull origin main >> "$LOG_FILE" 2>&1 + # Force reset to match origin (avoids merge conflicts) + git reset --hard origin/main >> "$LOG_FILE" 2>&1 - # Check if package.json changed - if git diff --name-only "$HEADHASH" "$UPSTREAMHASH" | grep "package.json"; then + # Check if package.json changed (compare old local HEAD with new remote HEAD) + # 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..." npm install >> "$LOG_FILE" 2>&1 fi - # Restart application log "Restarting application..." - pm2 reload all >> "$LOG_FILE" 2>&1 + pm2 restart Advanced-Smtp-Tester >> "$LOG_FILE" 2>&1 log "Update complete." else # Uncomment next line for verbose logging of no-changes # log "No changes detected." # echo "No changes." - : + log "No changes found." fi