sightbox/scripts/prod.sh

89 lines
3.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 the
# backend test suite as the one gate before any of that happens, then
# pushes this branch to main so Dokploy's existing AutoDeploy (triggered
# by the git push itself, not by this script) pulls the new images.
#
# 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.
# - 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.
# 3. 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.
# 4. Runs backend/tests/ via pytest — any failure stops here, before
# anything is built or pushed.
# 5. ./erpnext-image/build.sh — sees the registry-path CUSTOM_IMAGE from
# step 3, takes its registry branch: builds for linux/amd64 via
# buildx (not this machine's own arch) and pushes straight to the
# registry.
# 6. ./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.
# 7. git push origin main — the actual deploy trigger. Dokploy's
# AutoDeploy watches main and redeploys on push, pulling both
# freshly-pushed images.
#
# 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
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
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."