sightbox/scripts/prod.sh

125 lines
5.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Ships what scripts/dev.sh already built and you've tested locally:
# rebuilds both images for the real production target (linux/amd64, via
# each build.sh's own registry-path branch) and pushes them, runs both
# backend test suites as the one gate before any of that happens, then
# pushes this branch to main — which now triggers TWO Dokploy AutoDeploy
# webhooks (the ERPNext app and the separate website/ app, see
# website/README.md), not just one.
#
# Requires:
# - Currently checked out on main, with no uncommitted changes — this
# ships exactly what's committed, never a locally-dirty tree.
# - backend/tests/ passing — the one gate between this and production;
# see playbook/ENTERPRISE_STANDARDS.md's coverage rules for
# stripe_webhook.py/billing.py specifically.
# - website/backend/tests/ passing too — a smaller suite (no 100%-
# coverage standard applies there, ENTERPRISE_STANDARDS.md is scoped
# to the ERPNext app's backend/ only), but still gates the push since
# website/ ships on this same `git push origin main`.
# - scripts/.env.prod (gitignored, copy from scripts/.env.prod.example)
# with the real registry image paths. Deliberately does NOT set
# DOKPLOY_URL/DOKPLOY_API_TOKEN/DOKPLOY_COMPOSE_ID — this script
# relies on the one already-proven trigger (git push to main, which
# Dokploy's webhook picks up) rather than also firing backend/build.sh's
# own direct-API trigger, which would otherwise redeploy twice for one
# prod.sh run. Add those three to .env.prod yourself if you'd rather
# switch entirely to the direct-API trigger instead.
#
# What happens when this runs, in order — a hard stop at any step means
# nothing after it ever runs:
# 1. Branch check — exits unless currently on main.
# 2. Clean-tree check — exits if `git status --porcelain` shows anything
# uncommitted, so what ships always matches an actual commit (checked
# BEFORE step 3's own regenerate-and-commit, so that auto-commit is
# never mixed in with unrelated uncommitted work).
# 3. Regenerates docker-compose.dokploy.yml (./generate-dokploy-compose.sh)
# and auto-commits it if the content-hash (or anything else) changed —
# this is the one thing this script commits on its own, scoped to
# exactly this generated file. Without this, an edit to
# website/website-bettersightbox/bettersightbox-index.html with no other
# docker-compose.yml change
# would ship with a stale content-hash and the marketing container
# would silently never get recreated — see that service's own comment
# in docker-compose.yml for the full story.
# 4. Loads scripts/.env.prod — exits if it doesn't exist yet; otherwise
# exports the real CUSTOM_IMAGE/CUSTOM_TAG/BACKEND_BILLING_IMAGE/
# BACKEND_BILLING_TAG registry paths into this shell.
# 5. Runs backend/tests/ via pytest — any failure stops here, before
# anything is built or pushed.
# 6. Runs website/backend/tests/ via pytest too — same reasoning, before
# anything is built or pushed. website/'s own images are never built
# or pushed by this script (Dokploy builds them server-side from
# source on deploy, see website/docker-compose.yml) — this step is
# purely the test gate for it.
# 7. ./erpnext-image/build.sh — sees the registry-path CUSTOM_IMAGE from
# step 4, takes its registry branch: builds for linux/amd64 via
# buildx (not this machine's own arch) and pushes straight to the
# registry.
# 8. ./backend/build.sh — same thing for backend-billing: builds for
# linux/amd64, pushes to the registry. DOKPLOY_URL/DOKPLOY_API_TOKEN/
# DOKPLOY_COMPOSE_ID are deliberately unset (see above), so this step
# stops after pushing — it does not call Dokploy's API itself.
# 9. git push origin main — the actual deploy trigger (carries step 3's
# auto-commit too, if one happened). Both Dokploy apps' AutoDeploy
# webhooks fire on this one push and redeploy independently.
#
# Usage: ./scripts/prod.sh
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
cd "$ROOT_DIR"
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
if [ "$BRANCH" != "main" ]; then
echo "Not on main (currently on '${BRANCH}') — merge/checkout main first." >&2
exit 1
fi
if [ -n "$(git status --porcelain)" ]; then
echo "Uncommitted changes present — commit or stash before shipping." >&2
exit 1
fi
echo "Regenerating docker-compose.dokploy.yml..."
./generate-dokploy-compose.sh
if [ -n "$(git status --porcelain -- docker-compose.dokploy.yml)" ]; then
git add docker-compose.dokploy.yml
git commit -m "Regenerate docker-compose.dokploy.yml
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>"
echo "Committed updated docker-compose.dokploy.yml (content-hash changed)."
else
echo "docker-compose.dokploy.yml already up to date."
fi
PROD_ENV="${SCRIPT_DIR}/.env.prod"
if [ ! -f "$PROD_ENV" ]; then
echo "Missing ${PROD_ENV} — copy scripts/.env.prod.example and fill in the real registry paths first." >&2
exit 1
fi
set -a
# shellcheck disable=SC1090
source "$PROD_ENV"
set +a
echo "Running backend test suite..."
( cd backend && .venv/bin/pytest tests/ -q )
echo "Running website/backend test suite..."
( cd website/backend && .venv/bin/pytest tests/ -q )
echo
echo "Building and pushing for linux/amd64..."
./erpnext-image/build.sh
./backend/build.sh
echo
echo "Pushing to main (triggers Dokploy's AutoDeploy)..."
git push origin main
echo
echo "Shipped. Watch Dokploy's Deployments tab for the result."