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
This commit is contained in:
Artur Moreira committed 2026-03-24 19:02:10 +00:00
1 parent 1141e6d840
commit 5e14e2df47
5 files changed
+130

No files matched your search

+9
View File
@@ -43,7 +43,16 @@ if [ ! -f .env ]; then
echo "Copy .env.example to .env and fill in credentials before running this script."
exit 1
fi
set +e
bash "$APP_DIR/deploy/scripts/wordpress-ensure-public-url.sh"
_wp_pub=$?
set -e
docker compose up -d --remove-orphans
if [ "$_wp_pub" -eq 2 ]; then
log "WORDPRESS_PUBLIC_URL was corrected — recreating wordpress container..."
docker compose up -d --force-recreate wordpress
fi
bash "$APP_DIR/deploy/scripts/wordpress-sync-options-url.sh" || log "wordpress-sync-options-url skipped (db not ready or still local URL)"
# --- Environment files ---
log "Checking environment files..."
+11
View File
@@ -35,6 +35,15 @@ 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..."
@@ -79,4 +88,6 @@ if git diff --name-only "$LOCAL" "$REMOTE" | grep -q "^wordpress/"; then
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."
+53
View File
@@ -0,0 +1,53 @@
#!/usr/bin/env bash
# On the BitReport LXC (/opt/news-platform), ensure wordpress/.env has a public
# WORDPRESS_PUBLIC_URL so Docker and wp_options sync are aligned with NPM HTTPS.
#
# Exits: 0 = no change, 2 = .env was updated (caller should recreate wordpress).
# No-op on developer machines (APP_DIR != /opt/news-platform).
set -euo pipefail
APP_DIR="${APP_DIR:-/opt/news-platform}"
WP_DIR="$APP_DIR/wordpress"
DOMAIN_MARKER="$APP_DIR/config/domains/newsproject.bitreport.pt.json"
TARGET_URL="https://newsproject.bitreport.pt"
log() { echo "[wordpress-ensure-public-url] $*"; }
if [ "$APP_DIR" != "/opt/news-platform" ]; then
exit 0
fi
if [ ! -f "$DOMAIN_MARKER" ] || [ ! -f "$WP_DIR/.env" ]; then
exit 0
fi
current=""
if grep -qE '^[[:space:]]*WORDPRESS_PUBLIC_URL=' "$WP_DIR/.env"; then
current=$(grep -E '^[[:space:]]*WORDPRESS_PUBLIC_URL=' "$WP_DIR/.env" | tail -n1 | cut -d= -f2- | tr -d '"' | tr -d "'" | tr -d '\r' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
fi
need_update=0
if [ -z "$current" ]; then
need_update=1
elif [ "$current" = "http://localhost:8080" ] || [ "$current" = "http://127.0.0.1:8080" ]; then
need_update=1
fi
if [ "$need_update" -eq 0 ]; then
exit 0
fi
log "Setting WORDPRESS_PUBLIC_URL=$TARGET_URL in wordpress/.env"
ts="$(date +%Y%m%d%H%M%S)"
cp "$WP_DIR/.env" "$WP_DIR/.env.bak.${ts}"
tmp="${WP_DIR}/.env.new.$$"
if grep -qE '^[[:space:]]*WORDPRESS_PUBLIC_URL=' "$WP_DIR/.env"; then
sed -E "s|^[[:space:]]*WORDPRESS_PUBLIC_URL=.*|WORDPRESS_PUBLIC_URL=${TARGET_URL}|" "$WP_DIR/.env" > "$tmp"
mv "$tmp" "$WP_DIR/.env"
else
printf '\nWORDPRESS_PUBLIC_URL=%s\n' "$TARGET_URL" >> "$WP_DIR/.env"
fi
exit 2
+55
View File
@@ -0,0 +1,55 @@
#!/usr/bin/env bash
# Align wp_options siteurl/home with WORDPRESS_PUBLIC_URL (fixes wp-login redirects
# when the DB still has localhost from an early install). Runs MariaDB inside the
# Compose network. No-op outside /opt/news-platform or when URL is still local.
set -euo pipefail
APP_DIR="${APP_DIR:-/opt/news-platform}"
WP_DIR="$APP_DIR/wordpress"
ENV_FILE="$WP_DIR/.env"
log() { echo "[wordpress-sync-options-url] $*"; }
if [ "$APP_DIR" != "/opt/news-platform" ]; then
exit 0
fi
if [ ! -f "$ENV_FILE" ]; then
exit 0
fi
set -a
# shellcheck disable=SC1090
source "$ENV_FILE"
set +a
URL="${WORDPRESS_PUBLIC_URL:-}"
URL="${URL%/}"
if [ -z "$URL" ]; then
exit 0
fi
if [[ "$URL" == *"localhost"* ]] || [[ "$URL" == *"127.0.0.1"* ]]; then
exit 0
fi
PREFIX="${WORDPRESS_TABLE_PREFIX:-wp_}"
TABLE="${PREFIX}options"
cd "$WP_DIR"
if ! docker compose ps -q db >/dev/null 2>&1; then
exit 0
fi
if [ -z "$(docker compose ps -q db --status running 2>/dev/null || true)" ]; then
log "db container not running, skip"
exit 0
fi
escaped="${URL//\'/\'\\\'\'}"
docker compose exec -T db mariadb -uwordpress -p"${MYSQL_PASSWORD}" wordpress \
-e "UPDATE ${TABLE} SET option_value='${escaped}' WHERE option_name IN ('siteurl','home');"
log "Updated ${TABLE} siteurl/home → ${URL}"
+2
View File
@@ -110,6 +110,8 @@ A stack real está versionada em **`wordpress/docker-compose.yml`** (não duplic
Operação e recuperação de **«Error establishing a database connection»**: `wordpress/README.md` (secção homónima). No CT, após `cd /opt/news-platform/wordpress`: `bash scripts/verify-db-connection.sh` confirma em cadeia `ps` + healthcheck do MariaDB + ligação desde o contentor `wordpress`.
**Automação LXC (`APP_DIR=/opt/news-platform` apenas):** `deploy/scripts/wordpress-ensure-public-url.sh` corrige `WORDPRESS_PUBLIC_URL` no `wordpress/.env` para `https://newsproject.bitreport.pt` quando ainda está vazio ou em `http://localhost:8080`. `deploy/scripts/wordpress-sync-options-url.sh` actualiza `siteurl`/`home` na tabela `wp_*options` para o mesmo URL (resolve redirects para `localhost` mesmo após instalação antiga). São invocados por `deploy/scripts/deploy.sh` e `deploy/scripts/webhook-deploy.sh`; o primeiro devolve código de saída `2` quando alterou o `.env` para o *caller* fazer `docker compose up -d --force-recreate wordpress`.
---
## Service Start (PM2)