Files
news-platform/deploy/scripts/webhook-deploy.sh
T
Artur MoreiraandPaperclip a77c293d0e feat(wordpress): nginx + PHP-FPM stacks; retire Apache images (BIT-578)
- Use wordpress:6-php8.3-fpm with nginx:alpine sidecars; keep host ports 8080/8081
- Add shared wp_html / wp_andebol_html volumes so nginx can serve the same docroot as FPM
- Nginx: try_files permalinks, proxy headers, HTTP_AUTHORIZATION for App Passwords
- Update deploy scripts to force-recreate nginx with wordpress when public URL is fixed
- Refresh README and deployment docs

Co-Authored-By: Paperclip <[email protected]>
Made-with: Cursor
2026-03-28 01:15:51 +00:00

100 lines
3.2 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
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"
log "Deploy complete at $REMOTE."