Add scripts/dev.sh and scripts/prod.sh for the local build-and-ship workflow

Formalizes the flow now that images are pre-built and pushed rather than
built on the VPS: dev.sh is the fast local iteration loop (build both
custom images for this machine's own architecture, recreate the affected
services, no git/registry involved); prod.sh is the gate before anything
ships (backend test suite must pass, requires a clean main checkout,
rebuilds both images for linux/amd64 and pushes them, then `git push
origin main` to trigger Dokploy's existing AutoDeploy).

Fixes a real bug exposed while wiring prod.sh: erpnext-image/build.sh and
backend/build.sh unconditionally `source` the root .env, whose own active
CUSTOM_IMAGE/CUSTOM_TAG/PULL_POLICY lines would silently clobber any
registry-path override a caller (like prod.sh) exports beforehand —
confirmed the hard way earlier today when a manual override was ignored.
Both scripts now preserve and restore a caller's pre-set values around
that sourcing. Can't fix this by changing .env itself to conditional
defaults, since docker compose reads that same file as plain dotenv, not
bash.

Verified end-to-end: dev.sh built both images and recreated every
affected local service cleanly; prod.sh's guards correctly refused to run
against a dirty working tree; backend's full test suite (98 tests) passes.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012pc2x7vyuUofdT3kLuqF3Y
This commit is contained in:
JasonFraser 2026-10-03 14:02:54 -04:00
parent d33281a8e4
commit e28715ba34
7 changed files with 152 additions and 0 deletions

3
.gitignore vendored
View File

@ -8,6 +8,9 @@
# pattern as backend/tenants.json. # pattern as backend/tenants.json.
backup/.env backup/.env
# Real registry image paths scripts/prod.sh pushes to — same pattern.
scripts/.env.prod
# A local scratch copy of bind-mounted volume data, if one ever gets # A local scratch copy of bind-mounted volume data, if one ever gets
# created here for testing (see docker-compose.data-dir.yml) — the real # created here for testing (see docker-compose.data-dir.yml) — the real
# data lives at $SIGHTBOX_DATA_DIR outside the repo, never inside it. # data lives at $SIGHTBOX_DATA_DIR outside the repo, never inside it.

View File

@ -93,6 +93,17 @@ sightbox/
│ │ in docker-compose.yml now (see .env.example) │ │ in docker-compose.yml now (see .env.example)
│ ├── requirements.txt │ ├── requirements.txt
│ └── tests/ │ └── tests/
├── scripts/ ← the two-script dev/ship workflow, see below
│ ├── dev.sh ← local iteration loop: builds both custom images for this
│ │ machine's own architecture (fast, no push) and recreates
│ │ the affected services. Run as often as you want.
│ ├── prod.sh ← ships what dev.sh already tested: runs backend/tests/,
│ │ rebuilds both images for linux/amd64 and pushes them, then
│ │ `git push origin main` (Dokploy's existing AutoDeploy
│ │ picks that up). Requires being on main, clean working tree,
│ │ and scripts/.env.prod (gitignored) with the real registry
│ │ paths — see scripts/.env.prod.example.
│ └── .env.prod.example
└── frappe_docker/ ← official clone, native, unmodified └── frappe_docker/ ← official clone, native, unmodified
``` ```

View File

@ -35,6 +35,14 @@ set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_ENV="${SCRIPT_DIR}/../.env" ROOT_ENV="${SCRIPT_DIR}/../.env"
# Preserve any value a caller already exported (e.g. scripts/prod.sh setting
# a registry path) before sourcing .env below — see erpnext-image/build.sh's
# identical comment; no active line in .env collides with these today, but
# this keeps both scripts consistent and safe if one ever gets added.
_PRESET_BACKEND_BILLING_IMAGE="${BACKEND_BILLING_IMAGE:-}"
_PRESET_BACKEND_BILLING_TAG="${BACKEND_BILLING_TAG:-}"
_PRESET_BACKEND_BILLING_PULL_POLICY="${BACKEND_BILLING_PULL_POLICY:-}"
if [ -f "$ROOT_ENV" ]; then if [ -f "$ROOT_ENV" ]; then
set -a set -a
# shellcheck disable=SC1090 # shellcheck disable=SC1090
@ -42,6 +50,10 @@ if [ -f "$ROOT_ENV" ]; then
set +a set +a
fi fi
[ -n "$_PRESET_BACKEND_BILLING_IMAGE" ] && BACKEND_BILLING_IMAGE="$_PRESET_BACKEND_BILLING_IMAGE"
[ -n "$_PRESET_BACKEND_BILLING_TAG" ] && BACKEND_BILLING_TAG="$_PRESET_BACKEND_BILLING_TAG"
[ -n "$_PRESET_BACKEND_BILLING_PULL_POLICY" ] && BACKEND_BILLING_PULL_POLICY="$_PRESET_BACKEND_BILLING_PULL_POLICY"
BACKEND_BILLING_IMAGE="${BACKEND_BILLING_IMAGE:-sightbox-backend-billing}" BACKEND_BILLING_IMAGE="${BACKEND_BILLING_IMAGE:-sightbox-backend-billing}"
BACKEND_BILLING_TAG="${BACKEND_BILLING_TAG:-latest}" BACKEND_BILLING_TAG="${BACKEND_BILLING_TAG:-latest}"
FULL_TAG="${BACKEND_BILLING_IMAGE}:${BACKEND_BILLING_TAG}" FULL_TAG="${BACKEND_BILLING_IMAGE}:${BACKEND_BILLING_TAG}"

View File

@ -15,6 +15,16 @@ set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_ENV="${SCRIPT_DIR}/../.env" ROOT_ENV="${SCRIPT_DIR}/../.env"
# Preserve any value a caller already exported (e.g. scripts/prod.sh setting
# a registry path) before sourcing .env below — .env's own CUSTOM_IMAGE/
# CUSTOM_TAG/PULL_POLICY lines are plain assignments, not conditional
# defaults (docker compose reads this same file directly, so it can't use
# bash's ${VAR:=default} syntax), and would otherwise silently clobber a
# caller's override the moment it's sourced.
_PRESET_CUSTOM_IMAGE="${CUSTOM_IMAGE:-}"
_PRESET_CUSTOM_TAG="${CUSTOM_TAG:-}"
_PRESET_PULL_POLICY="${PULL_POLICY:-}"
if [ -f "$ROOT_ENV" ]; then if [ -f "$ROOT_ENV" ]; then
set -a set -a
# shellcheck disable=SC1090 # shellcheck disable=SC1090
@ -22,6 +32,10 @@ if [ -f "$ROOT_ENV" ]; then
set +a set +a
fi fi
[ -n "$_PRESET_CUSTOM_IMAGE" ] && CUSTOM_IMAGE="$_PRESET_CUSTOM_IMAGE"
[ -n "$_PRESET_CUSTOM_TAG" ] && CUSTOM_TAG="$_PRESET_CUSTOM_TAG"
[ -n "$_PRESET_PULL_POLICY" ] && PULL_POLICY="$_PRESET_PULL_POLICY"
ERPNEXT_VERSION="${ERPNEXT_VERSION:?Set ERPNEXT_VERSION in .env first (see .env.example)}" ERPNEXT_VERSION="${ERPNEXT_VERSION:?Set ERPNEXT_VERSION in .env first (see .env.example)}"
CUSTOM_IMAGE="${CUSTOM_IMAGE:-sightbox-erpnext}" CUSTOM_IMAGE="${CUSTOM_IMAGE:-sightbox-erpnext}"
CUSTOM_TAG="${CUSTOM_TAG:-${ERPNEXT_VERSION}-branded}" CUSTOM_TAG="${CUSTOM_TAG:-${ERPNEXT_VERSION}-branded}"

18
scripts/.env.prod.example Normal file
View File

@ -0,0 +1,18 @@
# Copy to scripts/.env.prod (gitignored) and fill in. Sourced only by
# scripts/prod.sh, to build and push the real production images — never
# consumed by docker compose or Dokploy directly. Dokploy's own Environment
# tab still needs these same image values pasted in separately (see the
# production block at the bottom of the root .env).
CUSTOM_IMAGE=git.bettertend.net/jasonfraser/sightbox-erpnext
CUSTOM_TAG=v16.34.1-branded
BACKEND_BILLING_IMAGE=git.bettertend.net/jasonfraser/sightbox-backend-billing
BACKEND_BILLING_TAG=latest
# Deliberately not included here: DOKPLOY_URL/DOKPLOY_API_TOKEN/
# DOKPLOY_COMPOSE_ID. prod.sh relies on the one already-proven trigger (git
# push to main, which Dokploy's webhook picks up) instead of also firing
# backend/build.sh's own direct-API redeploy — setting these here would
# make one prod.sh run trigger a redeploy twice. Add them yourself only if
# you'd rather switch entirely to the direct-API trigger instead.

28
scripts/dev.sh Executable file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env bash
# Local iteration loop: build both custom images for this machine's own
# architecture (fast, no registry push — see erpnext-image/build.sh and
# backend/build.sh's own local-vs-registry branching) and bring the
# affected services up immediately so you can test against them. Run this
# as often as you want while developing; nothing here touches git or the
# registry — see scripts/prod.sh for shipping what you've tested here.
#
# Usage (from the sightbox/ project root or from here — either works):
# ./scripts/dev.sh
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
cd "$ROOT_DIR"
./erpnext-image/build.sh
./backend/build.sh
echo
echo "Bringing up the affected services locally..."
docker compose up -d --force-recreate \
configurator backend frontend websocket queue-short queue-long scheduler \
backend-billing
echo
echo "Done — test against your local stack now."

66
scripts/prod.sh Executable file
View File

@ -0,0 +1,66 @@
#!/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.
#
# 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."