PeerCortex/deploy-from-git.sh
Rene Fichtmueller b11eed9cf6
Some checks failed
build-verify / build-verify (push) Failing after 11s
build-verify / build-verify (pull_request) Failing after 12s
fix: don't fail build-verify/deploy on pre-existing mcp-server tsc exit code
Discovered live on Erik just now: 'npm run build' returns exit code 2
because of pre-existing src/mcp-server/index.ts type errors (unrelated to
the server.js/API-server deploy path -- already documented in the
2026-07-16 audit), but with noEmitOnError left at its default (false) it
still correctly emits dist/api/index.js, dist/api/server.js, etc. Both
build-verify.yml and deploy-from-git.sh were treating that non-zero exit
as a hard failure -- the CI run for the previous commit failed on exactly
this, and deploy-from-git.sh's  would have aborted a real deploy
at the same step had I used it instead of manual commands. Now both check
for the specific required output files instead of trusting tsc's exit code.
2026-07-16 21:48:37 +02:00

72 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# Git-pull-based deploy for Erik. Replaces the ad-hoc per-file `cat file | ssh`
# uploads used on 2026-07-16, which twice missed files that server.js actually
# requires (src/backend/config.js, src/backend/services/smtp.js) because
# uploading was a manual, easy-to-forget step instead of a single `git pull`.
#
# Run this ON Erik, from /opt/peercortex-app. Deliberately manual (not wired
# to a Gitea Action) -- Erik hosts many unrelated production services and
# main can contain code that hasn't been live-tested yet (see project memory).
#
# Usage: ./deploy-from-git.sh [branch] (default: main)
set -euo pipefail
cd "$(dirname "$0")"
BRANCH="${1:-main}"
BACKUP_DIR=/opt/_rollbacks/peercortex-app
TS=$(date -u +%Y%m%dT%H%M%SZ)
mkdir -p "$BACKUP_DIR"
echo "[1/7] Backing up current server.js + public/index.html..."
cp -p server.js "$BACKUP_DIR/server.js.$TS"
cp -p public/index.html "$BACKUP_DIR/index.html.$TS"
echo "[2/7] Fetching + checking out $BRANCH..."
git fetch origin
git checkout "$BRANCH"
git pull origin "$BRANCH"
echo "[3/7] npm ci..."
npm ci
echo "[4/7] Verifying every local require() target actually exists..."
MISSING=0
for f in server.js local-db-client.js bio-rd-client.js; do
[ -f "$f" ] || continue
for req in $(grep -oE "require\(['\"]\./[^'\"]+['\"]\)" "$f" | sed -E "s/require\(['\"]\.\/([^'\"]+)['\"]\)/\1/"); do
if [ ! -f "$req" ] && [ ! -f "$req.js" ] && [ ! -d "$req" ]; then
echo " MISSING: $f requires ./$req -- not found" >&2
MISSING=1
fi
done
done
if [ "$MISSING" = "1" ]; then
echo "Aborting: fix the missing file(s) above before deploying." >&2
exit 1
fi
echo "[5/7] Syntax check..."
node --check server.js
node --check local-db-client.js
echo "[6/7] Building dist/ (TypeScript API server)..."
# tsc exits non-zero on pre-existing src/mcp-server/* errors unrelated to this
# deploy, but still emits working output (default noEmitOnError:false) for
# files that DID typecheck cleanly -- confirmed 2026-07-16. Don't let `set -e`
# abort here; check the files this deploy actually needs instead.
npm run build || true
for f in dist/api/index.js dist/api/server.js dist/aspa/validator.js; do
if [ ! -f "$f" ]; then
echo "Aborting: required build output missing: $f" >&2
exit 1
fi
done
echo "[7/7] Restarting peercortex (and peercortex-api if configured)..."
pm2 restart peercortex --update-env
pm2 restart peercortex-api --update-env 2>/dev/null || echo " (peercortex-api not running yet, skipped)"
echo ""
echo "Done. Rollback: cp $BACKUP_DIR/server.js.$TS server.js && cp $BACKUP_DIR/index.html.$TS public/index.html && pm2 restart peercortex"