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"
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