Files
news-platform/deploy/scripts/webhook-deploy.sh
T
José AlvesandCursor be0b0c9c6f
Tests / writer-pipeline tests (push) Failing after 11s
Tests / bridge tests (push) Successful in 29s
Tests / frontend lib tests (push) Successful in 31s
Tests / writer-pipeline tests (pull_request) Failing after 11s
Tests / bridge tests (pull_request) Failing after 13s
Tests / frontend lib tests (pull_request) Successful in 29s
fix(deploy): ensure writer-pipeline var is writable by www-data
Writer systemd oneshots run as www-data but deploy never chowned
writer-pipeline/var, causing EACCES on aggregated-items.json when
the store was created by root. Mirror BIT-743 bridge/config/writers
fix; rebuild writer-pipeline on webhook when that tree changes.

Paperclip-Issue: BIT-25140
Co-authored-by: Cursor <[email protected]>
2026-06-14 20:04:03 +00:00

114 lines
3.7 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 + nginx containers..."
(cd "$APP_DIR/wordpress" && docker compose up -d --force-recreate wordpress nginx) || 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
mkdir -p "$APP_DIR/bridge/config/writers"
chown www-data:www-data "$APP_DIR/bridge/config/writers"
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..."
mkdir -p /etc/nginx/conf.d
if [ -f "$APP_DIR/deploy/nginx/conf.d/99-news-platform-wp-map.conf" ]; then
cp "$APP_DIR/deploy/nginx/conf.d/99-news-platform-wp-map.conf" \
/etc/nginx/conf.d/99-news-platform-wp-map.conf
fi
cp "$APP_DIR/deploy/nginx/newsproject.bitreport.pt.conf" \
/etc/nginx/sites-available/newsproject.bitreport.pt.conf
rm -f /etc/nginx/sites-enabled/default
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"
# Rebuild writer-pipeline if changed (systemd oneshots run as www-data)
if git diff --name-only "$LOCAL" "$REMOTE" | grep -q "^writer-pipeline/"; then
log "Writer pipeline changed — rebuilding..."
cd "$APP_DIR/writer-pipeline"
npm ci --silent
npm run build
log "Writer pipeline rebuilt."
fi
mkdir -p "$APP_DIR/writer-pipeline/var"
chown -R www-data:www-data "$APP_DIR/writer-pipeline/var"
log "Deploy complete at $REMOTE."