chore(ops): optimize update.sh to skip rebuild when no changes detected
NexusGuard CI / server-core-test (push) Failing after 30s
NexusGuard CI / device-agent-test (push) Failing after 33s
NexusGuard CI / dashboard-ui-build (push) Failing after 27s

This commit is contained in:
datadunia
2026-05-27 04:00:54 +07:00
parent 9c30fbd506
commit 0b943aead0
2 changed files with 63 additions and 27 deletions
+1
View File
@@ -40,6 +40,7 @@ dist/
.sisyphus/boulder.json .sisyphus/boulder.json
connect_remote.txt connect_remote.txt
.sisyphus/drafts/ .sisyphus/drafts/
.update-state
# Node # Node
node_modules/ node_modules/
+46 -11
View File
@@ -5,6 +5,10 @@ echo "========================================="
echo " NexusGuard SD-WAN: SYSTEM UPDATE " echo " NexusGuard SD-WAN: SYSTEM UPDATE "
echo "=========================================" echo "========================================="
STATE_FILE=".update-state"
FORCE_REBUILD=false
[ "$1" = "--force" ] && FORCE_REBUILD=true
# 1. Pastikan file env ada # 1. Pastikan file env ada
if [ ! -f .env ]; then if [ ! -f .env ]; then
echo "[-] ERROR: .env file not found. Please run ./setup.sh first to initialize configuration." echo "[-] ERROR: .env file not found. Please run ./setup.sh first to initialize configuration."
@@ -13,40 +17,71 @@ fi
# 2. Update Code dari Git (Main & Submodules) # 2. Update Code dari Git (Main & Submodules)
echo "[+] Pulling latest source code from repository..." echo "[+] Pulling latest source code from repository..."
git pull origin main || echo "[!] Git pull for main repo skipped or failed, continuing..." GIT_PULL_FAILED=false
git pull origin main || { GIT_PULL_FAILED=true; echo "[!] Git pull for main repo skipped or failed, continuing..."; }
echo "[+] Syncing and updating submodules..." echo "[+] Syncing and updating submodules..."
git submodule update --init --recursive --remote || echo "[!] Git submodule update skipped or failed." SUBMODULE_FAILED=false
git submodule update --init --recursive --remote || { SUBMODULE_FAILED=true; echo "[!] Git submodule update skipped or failed."; }
# 3. Source variabel dari .env untuk dimasukkan ke build process (contoh: VITE_API_BASE_URL) # 3. Hitung state hash (git + .env) untuk deteksi perubahan
CURRENT_HASH=$(echo "$(git rev-parse HEAD 2>/dev/null)$(git submodule status 2>/dev/null)$(sha256sum .env 2>/dev/null)" | sha256sum | cut -d" " -f1)
# 4. Tentukan apakah perlu rebuild
NEEDS_REBUILD=false
if [ "$FORCE_REBUILD" = true ]; then
echo "[+] --force flag detected. Will rebuild."
NEEDS_REBUILD=true
elif [ "$GIT_PULL_FAILED" = true ] || [ "$SUBMODULE_FAILED" = true ]; then
echo "[!] Git operation failed. Rebuilding as safe fallback."
NEEDS_REBUILD=true
elif [ ! -f "$STATE_FILE" ]; then
echo "[+] First run (no state file found). Full cycle required."
NEEDS_REBUILD=true
else
PREVIOUS_HASH=$(cat "$STATE_FILE" 2>/dev/null || echo "")
if [ "$CURRENT_HASH" != "$PREVIOUS_HASH" ]; then
echo "[+] State hash changed. Rebuilding."
NEEDS_REBUILD=true
else
echo "[-] No changes detected. Skipping build and restart."
fi
fi
# 5. Rebuild cycle — hanya jika ada perubahan
if [ "$NEEDS_REBUILD" = true ]; then
# Source .env untuk build process
echo "[+] Sourcing environment variables from root .env..." echo "[+] Sourcing environment variables from root .env..."
set -a && . .env && set +a set -a && . .env && set +a
# 4. Gracefully stop existing containers, Rebuild Images & Restart
echo "[+] Stopping existing containers..." echo "[+] Stopping existing containers..."
docker compose down docker compose down
echo "[+] Rebuilding Docker images (injecting new environment variables)..." echo "[+] Rebuilding Docker images..."
# Menggunakan --build-arg untuk memastikan Vue/Vite membaca domain API baru
docker compose build --build-arg VITE_API_BASE_URL=$VITE_API_BASE_URL docker compose build --build-arg VITE_API_BASE_URL=$VITE_API_BASE_URL
echo "[+] Restarting containers..." echo "[+] Restarting containers..."
docker compose up -d docker compose up -d
# 5. Run Database Migrations
echo "[+] Running automated database migrations..." echo "[+] Running automated database migrations..."
docker exec nexus-guard-suite-server-core-1 ./server-core -migrate-prod || echo "[!] Migration skipped or failed. It might not be needed." docker exec nexus-guard-suite-server-core-1 ./server-core -migrate-prod || echo "[!] Migration skipped or failed. It might not be needed."
# 5b. Backfill interface addresses for existing nodes (idempotent) echo "[+] Backfilling interface addresses..."
echo "[+] Backfilling interface addresses for existing nodes..."
docker exec nexus-guard-suite-server-core-1 ./server-core -backfill-interface 2>/dev/null || echo "[!] Backfill skipped or not needed." docker exec nexus-guard-suite-server-core-1 ./server-core -backfill-interface 2>/dev/null || echo "[!] Backfill skipped or not needed."
# 6. Cleanup echo "[+] Cleaning up unused dangling images..."
echo "[+] Cleaning up unused dangling images to free up space..."
docker image prune -f docker image prune -f
# Simpan state secara atomik (tmp + mv untuk cegah korupsi file)
echo "$CURRENT_HASH" > "$STATE_FILE.tmp" && mv "$STATE_FILE.tmp" "$STATE_FILE"
echo "=========================================" echo "========================================="
echo " Update & Deployment complete! " echo " Update & Deployment complete! "
echo " Dashboard : http://localhost:${WEB_PORT:-80}" echo " Dashboard : http://localhost:${WEB_PORT:-80}"
echo " API Port : ${API_PORT:-8080}" echo " API Port : ${API_PORT:-8080}"
echo "=========================================" echo "========================================="
else
echo "========================================="
echo " No update needed — system is current. "
echo "========================================="
fi