Sites per domain with two skins (PhotoReport, ShootYourMove) in apps/site (Next.js); public, unlisted and private galleries with watermarked photos; products, cart, orders with VIES reverse charge; the organization's own Stripe Checkout and signed webhooks; bought files rendered from the original with edits and IPTC, emailed; Vendas module in the app. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW
130 lines
4.3 KiB
Bash
Executable File
130 lines
4.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Deploys one PhotoReport service at a given commit, on the container that runs it.
|
|
# Called by the deploy receiver: deploy.sh <service> <sha>
|
|
# Rules (docs/DEPLOY.md): never touch .env files or data; migrations only move forward;
|
|
# a failed health check rolls the code back to the previous commit and reloads it.
|
|
set -Eeuo pipefail
|
|
|
|
SERVICE="${1:?usage: deploy.sh <api|app|site|workers|ml> <sha>}"
|
|
SHA="${2:?usage: deploy.sh <service> <sha>}"
|
|
REPO_DIR="${DEPLOY_REPO_DIR:-/srv/photoreport}"
|
|
BRANCH="${DEPLOY_BRANCH:-main}"
|
|
API_PORT="${API_PORT:-3000}"
|
|
WEB_ROOT="${DEPLOY_WEB_ROOT:-/var/www/photoreport-app}"
|
|
HEALTH_TRIES="${DEPLOY_HEALTH_TRIES:-30}"
|
|
|
|
log() { printf '[%s] %s\n' "$(date -u +%FT%TZ)" "$*"; }
|
|
die() { log "ERROR: $*"; exit 1; }
|
|
|
|
[[ "$SHA" =~ ^[0-9a-f]{40}$ ]] || die "invalid sha: $SHA"
|
|
cd "$REPO_DIR"
|
|
|
|
# Keep a copy of every .env before touching anything (norm BIT-25005). Deploys never write them.
|
|
backup_env() {
|
|
while IFS= read -r -d '' f; do cp -p "$f" "$f.bak.deploy"; done \
|
|
< <(find . -name .env -not -path './node_modules/*' -print0)
|
|
}
|
|
|
|
checkout() {
|
|
local target="$1"
|
|
git fetch --quiet origin "$BRANCH"
|
|
git merge-base --is-ancestor "$target" "origin/$BRANCH" || die "$target is not on origin/$BRANCH"
|
|
git reset --quiet --hard "$target"
|
|
}
|
|
|
|
node_install() { pnpm install --frozen-lockfile --prefer-offline; }
|
|
|
|
wait_for_version() {
|
|
local url="$1" want="$2" i
|
|
for ((i = 1; i <= HEALTH_TRIES; i++)); do
|
|
if curl -fsS --max-time 3 "$url" 2>/dev/null | grep -q "\"version\":\"$want\""; then return 0; fi
|
|
sleep 2
|
|
done
|
|
return 1
|
|
}
|
|
|
|
deploy_api() {
|
|
local sha="$1"
|
|
node_install
|
|
pnpm --filter "@photoreport/api..." build
|
|
(cd apps/api && pnpm exec prisma migrate deploy)
|
|
# The background worker runs the Rust face and render engines when configured (FACES_BIN, RENDER_BIN in apps/api/.env).
|
|
if command -v cargo >/dev/null 2>&1; then cargo build --release --locked -p photoreport-faces -p photoreport-render; fi
|
|
APP_VERSION="$sha" pm2 startOrReload infra/pm2/api.config.cjs --update-env
|
|
wait_for_version "http://127.0.0.1:${API_PORT}/health" "$sha"
|
|
}
|
|
|
|
deploy_app() {
|
|
local sha="$1" release="$WEB_ROOT/releases/$1"
|
|
node_install
|
|
rm -rf apps/app/dist # never publish a stale build
|
|
# The develop module's engine (WASM). Without the Rust toolchain the app still deploys; Revelar says the engine is missing.
|
|
if command -v cargo >/dev/null 2>&1 && command -v wasm-bindgen >/dev/null 2>&1; then
|
|
infra/engine/build-wasm.sh
|
|
else
|
|
log "WARNING: cargo/wasm-bindgen not found: the develop engine is not rebuilt"
|
|
fi
|
|
VITE_APP_VERSION="$sha" pnpm --filter "@photoreport/app..." build
|
|
mkdir -p "$release"
|
|
cp -a apps/app/dist/. "$release/"
|
|
ln -sfn "$release" "$WEB_ROOT/current.tmp" && mv -Tf "$WEB_ROOT/current.tmp" "$WEB_ROOT/current"
|
|
[[ -f "$WEB_ROOT/current/index.html" ]]
|
|
# Keep the last 5 releases.
|
|
ls -1dt "$WEB_ROOT"/releases/* 2>/dev/null | tail -n +6 | xargs -r rm -rf
|
|
}
|
|
|
|
deploy_site() {
|
|
local sha="$1"
|
|
node_install
|
|
rm -rf apps/site/.next
|
|
pnpm --filter "@photoreport/site..." build
|
|
APP_VERSION="$sha" pm2 startOrReload infra/pm2/site.config.cjs --update-env
|
|
local i
|
|
for ((i = 1; i <= HEALTH_TRIES; i++)); do
|
|
if curl -fsS --max-time 3 -o /dev/null "http://127.0.0.1:${SITE_PORT:-3100}/"; then return 0; fi
|
|
sleep 2
|
|
done
|
|
return 1
|
|
}
|
|
|
|
deploy_pending() { log "service $SERVICE has no deployable build yet; nothing to do"; }
|
|
|
|
run_service() {
|
|
case "$SERVICE" in
|
|
api) deploy_api "$1" ;;
|
|
app) deploy_app "$1" ;;
|
|
site) deploy_site "$1" ;;
|
|
workers | ml) deploy_pending ;;
|
|
*) die "unknown service: $SERVICE" ;;
|
|
esac
|
|
}
|
|
|
|
PREVIOUS="$(git rev-parse HEAD)"
|
|
log "deploying $SERVICE: $PREVIOUS -> $SHA"
|
|
backup_env
|
|
checkout "$SHA"
|
|
|
|
# Bash ignores `set -e` in anything called from `if`, `&&` or `||` (including subshells), so each attempt
|
|
# runs as a plain statement in a subshell and its exit code is read afterwards.
|
|
set +e
|
|
(set -e; run_service "$SHA")
|
|
rc=$?
|
|
set -e
|
|
if [[ $rc -eq 0 ]]; then
|
|
log "deploy of $SERVICE at $SHA succeeded"
|
|
exit 0
|
|
fi
|
|
|
|
log "deploy failed (exit $rc); rolling back to $PREVIOUS"
|
|
checkout "$PREVIOUS"
|
|
set +e
|
|
(set -e; run_service "$PREVIOUS")
|
|
rc=$?
|
|
set -e
|
|
if [[ $rc -eq 0 ]]; then
|
|
log "rolled back to $PREVIOUS"
|
|
else
|
|
log "rollback to $PREVIOUS also failed (exit $rc); service needs attention"
|
|
fi
|
|
exit 1
|