Files
news-platform/deploy/scripts/webhook-deploy.sh
T
Artur Moreira 5e14e2df47 fix(deploy): auto-set WordPress public URL and sync wp_options on LXC
Add wordpress-ensure-public-url.sh (exit 2 when .env updated) and
wordpress-sync-options-url.sh to align siteurl/home with HTTPS. Wire
into deploy.sh and webhook-deploy.sh for /opt/news-platform only so
the board does not need manual .env or SQL for BIT-470/BIT-471.

Made-with: Cursor
2026-03-24 19:02:10 +00:00

94 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Lightweight Gitea webhook handler — triggered by POST from Gitea on push to main.
# Deploy: run this as a cron, or via a minimal webhook receiver (e.g. adnanh/webhook).
# Example crontab (pulls and redeploys every 5 min if new commits):
# */5 * * * * /opt/news-platform/deploy/scripts/webhook-deploy.sh >> /var/log/news-deploy.log 2>&1
set -euo pipefail
APP_DIR="/opt/news-platform"
LOCK_FILE="/tmp/news-platform-deploy.lock"
LOG_TAG="webhook-deploy"
log() { echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) [$LOG_TAG] $*"; }
# Prevent concurrent deploys
if [ -f "$LOCK_FILE" ]; then
log "Deploy already in progress, skipping."
exit 0
fi
touch "$LOCK_FILE"
trap 'rm -f "$LOCK_FILE"' EXIT
# Fetch remote state
cd "$APP_DIR"
git fetch origin main --quiet
LOCAL=$(git rev-parse HEAD)
REMOTE=$(git rev-parse origin/main)
if [ "$LOCAL" = "$REMOTE" ]; then
log "Already up-to-date ($LOCAL), nothing to deploy."
exit 0
fi
log "New commit detected: $LOCAL → $REMOTE. Starting deploy..."
git reset --hard origin/main
set +e
bash "$APP_DIR/deploy/scripts/wordpress-ensure-public-url.sh"
_wp_pub=$?
set -e
if [ "$_wp_pub" -eq 2 ]; then
log "WORDPRESS_PUBLIC_URL updated — recreating wordpress container..."
(cd "$APP_DIR/wordpress" && docker compose up -d --force-recreate wordpress) || log "wordpress recreate failed (compose may be down)"
fi
# Rebuild bridge if changed
if git diff --name-only "$LOCAL" "$REMOTE" | grep -q "^bridge/"; then
log "Bridge changed — rebuilding..."
cd "$APP_DIR/bridge"
npm ci --silent
npm run build
systemctl restart news-bridge
log "Bridge restarted."
fi
# Rebuild frontend if changed
if git diff --name-only "$LOCAL" "$REMOTE" | grep -q "^frontend/"; then
log "Frontend changed — rebuilding..."
cd "$APP_DIR/frontend"
npm ci --silent
npm run build
systemctl restart news-frontend
log "Frontend restarted."
fi
# Reload nginx if config changed
if git diff --name-only "$LOCAL" "$REMOTE" | grep -q "^deploy/nginx/"; then
log "Nginx config changed — reloading..."
cp "$APP_DIR/deploy/nginx/newsproject.bitreport.pt.conf" \
/etc/nginx/sites-available/newsproject.bitreport.pt.conf
nginx -t && systemctl reload nginx
log "Nginx reloaded."
fi
# Domain JSON is cached in the bridge process — restart when config changes
if git diff --name-only "$LOCAL" "$REMOTE" | grep -q "^config/"; then
log "Domain config changed — restarting bridge + frontend..."
systemctl restart news-bridge
systemctl restart news-frontend
fi
# WordPress compose or plugins
if git diff --name-only "$LOCAL" "$REMOTE" | grep -q "^wordpress/"; then
log "WordPress stack changed — docker compose up..."
cd "$APP_DIR/wordpress"
docker compose up -d --remove-orphans
systemctl restart news-wordpress || true
fi
bash "$APP_DIR/deploy/scripts/wordpress-sync-options-url.sh" || log "wordpress-sync-options-url skipped or failed"
log "Deploy complete at $REMOTE."