#!/bin/bash # Boot Update Script: Automatically pull latest main, build, and prepare for PM2 # This script runs on LXC container boot BEFORE PM2 starts. # It ensures the app is always up-to-date with the main branch. set -e APP_DIR="/root/My-Weekly-ToDo-List" LOG_FILE="/var/log/app-boot-update.log" log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE" } log "=== Boot update started ===" cd "$APP_DIR" # Check if there are remote changes log "Fetching from origin..." git fetch origin main 2>&1 | tee -a "$LOG_FILE" LOCAL=$(git rev-parse HEAD) REMOTE=$(git rev-parse origin/main) if [ "$LOCAL" = "$REMOTE" ]; then log "Already up-to-date. No rebuild needed." log "=== Boot update finished (no changes) ===" exit 0 fi log "Changes detected. Updating from $LOCAL to $REMOTE" log "Pulling latest changes..." git checkout main 2>&1 | tee -a "$LOG_FILE" git pull origin main 2>&1 | tee -a "$LOG_FILE" log "Installing dependencies..." npm install 2>&1 | tee -a "$LOG_FILE" log "Updating database schema..." npx prisma db push 2>&1 | tee -a "$LOG_FILE" log "Regenerating Prisma client..." npx prisma generate 2>&1 | tee -a "$LOG_FILE" log "Building application..." npm run build 2>&1 | tee -a "$LOG_FILE" log "=== Boot update finished (rebuilt successfully) ==="