Files
bookstore/deploy/deploy.sh
T
twooeyandClaude Sonnet 5 b43852e733 Switch deploy pipeline to Gitea polling; fix env-parsing and volume-isolation bugs
- Replace the bare-repo post-receive hook with deploy/poll-deploy.sh: Gitea
  and the Docker hosts are separate machines, so each box polls its branch
  via host crontab instead of needing an exposed webhook receiver.
- Add Blocksy theme + Blocksy Companion auto-install to deploy.sh (free
  tier; the paid Book Store starter site still needs a manual license step).
- Fix deploy.sh/backup.sh/restore.sh sourcing .env files as bash: a bcrypt
  hash's `$2a$14$...` shape breaks under `set -u`. Replaced with
  deploy/lib/env.sh, a literal (non-executing) KEY=VALUE reader.
- Fix docker compose itself mangling the same kind of value: both
  `environment: ${VAR}` and `env_file:` run values through Compose's
  interpolation, which silently blanks `$identifier`-shaped substrings.
  The staging basic-auth hash is now rendered directly into the Caddyfile
  by deploy.sh, bypassing Compose's variable system entirely.
- Fix dev/staging/production silently sharing one Compose project (and
  therefore one db_data volume) by pinning an explicit -p per environment.
- cron and wordpress now share one environment anchor so they can't drift
  apart again (cron was silently missing WORDPRESS_CONFIG_EXTRA before).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-27 10:10:12 -04:00

97 lines
4.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Builds and brings up an environment, then makes sure WordPress/WooCommerce
# are installed and configured to match design doc §01/§03/§07. Safe to run
# repeatedly — every step is idempotent.
set -euo pipefail
ENVIRONMENT="${1:?Usage: deploy.sh <dev|staging|production>}"
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$REPO_ROOT"
case "$ENVIRONMENT" in
dev|staging|production) ;;
*) echo "environment must be 'dev', 'staging', or 'production'" >&2; exit 1 ;;
esac
ENV_FILE=".env.${ENVIRONMENT}"
if [[ ! -f "$ENV_FILE" ]]; then
echo "missing $ENV_FILE — copy .env.example to $ENV_FILE and fill it in" >&2
exit 1
fi
# shellcheck source=lib/env.sh
source "$REPO_ROOT/deploy/lib/env.sh"
SITE_URL="$(env_get "$ENV_FILE" SITE_URL)"
SITE_TITLE="$(env_get "$ENV_FILE" SITE_TITLE)"
WP_ADMIN_USER="$(env_get "$ENV_FILE" WP_ADMIN_USER)"
WP_ADMIN_PASSWORD="$(env_get "$ENV_FILE" WP_ADMIN_PASSWORD)"
WP_ADMIN_EMAIL="$(env_get "$ENV_FILE" WP_ADMIN_EMAIL)"
# -p pins the Compose project name to the environment (default is the
# directory name, which every environment shares — that made staging quietly
# reuse dev's db_data volume/credentials the first time this ran).
COMPOSE="docker compose -p bookstore-${ENVIRONMENT} -f docker-compose.yml -f docker-compose.${ENVIRONMENT}.yml --env-file ${ENV_FILE}"
if [[ "$ENVIRONMENT" == "staging" ]]; then
echo "==> rendering Caddyfile.staging (basic-auth hash bypasses compose entirely)"
mkdir -p docker/caddy/.generated
AUTH_USER="$(env_get "$ENV_FILE" STAGING_BASIC_AUTH_USER)"
AUTH_HASH="$(env_get "$ENV_FILE" STAGING_BASIC_AUTH_HASH)"
: "${AUTH_USER:?set STAGING_BASIC_AUTH_USER in ${ENV_FILE}}"
: "${AUTH_HASH:?set STAGING_BASIC_AUTH_HASH in ${ENV_FILE} — see .env.example for how to generate it}"
TEMPLATE="$(cat docker/caddy/Caddyfile.staging)"
TEMPLATE="${TEMPLATE//__STAGING_BASIC_AUTH_USER__/$AUTH_USER}"
TEMPLATE="${TEMPLATE//__STAGING_BASIC_AUTH_HASH__/$AUTH_HASH}"
printf '%s\n' "$TEMPLATE" > docker/caddy/.generated/Caddyfile.staging
fi
echo "==> building and starting ${ENVIRONMENT}"
$COMPOSE up -d --build
echo "==> waiting for the wordpress container to come up"
for i in $(seq 1 30); do
if $COMPOSE exec -T wordpress php -v >/dev/null 2>&1; then
break
fi
if [[ "$i" -eq 30 ]]; then
echo "wordpress container did not become ready in time" >&2
exit 1
fi
sleep 2
done
echo "==> ensuring WordPress is installed"
if ! $COMPOSE exec -T wordpress wp core is-installed --allow-root; then
$COMPOSE exec -T wordpress wp core install \
--url="${SITE_URL:?set SITE_URL in ${ENV_FILE}}" \
--title="${SITE_TITLE:-Bookstore}" \
--admin_user="${WP_ADMIN_USER:?set WP_ADMIN_USER in ${ENV_FILE}}" \
--admin_password="${WP_ADMIN_PASSWORD:?set WP_ADMIN_PASSWORD in ${ENV_FILE}}" \
--admin_email="${WP_ADMIN_EMAIL:?set WP_ADMIN_EMAIL in ${ENV_FILE}}" \
--skip-email \
--allow-root
fi
echo "==> installing/activating theme (Blocksy — free; Book Store starter site needs a manual license + import, see README)"
$COMPOSE exec -T wordpress wp theme install blocksy --activate --allow-root
echo "==> installing/activating required plugins"
$COMPOSE exec -T wordpress wp plugin install woocommerce redis-cache blocksy-companion --activate --allow-root
$COMPOSE exec -T wordpress wp plugin activate bookstore-core --allow-root
$COMPOSE exec -T wordpress wp redis enable --allow-root || true
echo "==> enabling WooCommerce HPOS (custom order tables)"
$COMPOSE exec -T wordpress wp option update woocommerce_custom_orders_table_enabled yes --allow-root
$COMPOSE exec -T wordpress wp option update woocommerce_custom_orders_table_data_sync_enabled yes --allow-root
if [[ "$ENVIRONMENT" != "production" ]]; then
echo "==> discouraging search engines (non-production)"
$COMPOSE exec -T wordpress wp option update blog_public 0 --allow-root
fi
echo "==> flushing caches and rewrite rules"
$COMPOSE exec -T wordpress wp cache flush --allow-root
$COMPOSE exec -T wordpress wp rewrite flush --allow-root
echo "==> deploy complete: ${ENVIRONMENT}"