From e28715ba3480a149f556324a13b5ac14e91335cb Mon Sep 17 00:00:00 2001 From: JasonFraser Date: Sat, 3 Oct 2026 14:02:54 -0400 Subject: [PATCH] Add scripts/dev.sh and scripts/prod.sh for the local build-and-ship workflow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Claude-Session: https://claude.ai/code/session_012pc2x7vyuUofdT3kLuqF3Y --- .gitignore | 3 ++ CLAUDE.md | 11 +++++++ backend/build.sh | 12 +++++++ erpnext-image/build.sh | 14 +++++++++ scripts/.env.prod.example | 18 +++++++++++ scripts/dev.sh | 28 +++++++++++++++++ scripts/prod.sh | 66 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 152 insertions(+) create mode 100644 scripts/.env.prod.example create mode 100755 scripts/dev.sh create mode 100755 scripts/prod.sh diff --git a/.gitignore b/.gitignore index 052ecfc..23c20de 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,9 @@ # pattern as backend/tenants.json. 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 # created here for testing (see docker-compose.data-dir.yml) — the real # data lives at $SIGHTBOX_DATA_DIR outside the repo, never inside it. diff --git a/CLAUDE.md b/CLAUDE.md index e2cf1f0..731cd14 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -93,6 +93,17 @@ sightbox/ │ │ in docker-compose.yml now (see .env.example) │ ├── requirements.txt │ └── 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 ``` diff --git a/backend/build.sh b/backend/build.sh index 5cff499..168c619 100755 --- a/backend/build.sh +++ b/backend/build.sh @@ -35,6 +35,14 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 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 set -a # shellcheck disable=SC1090 @@ -42,6 +50,10 @@ if [ -f "$ROOT_ENV" ]; then set +a 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_TAG="${BACKEND_BILLING_TAG:-latest}" FULL_TAG="${BACKEND_BILLING_IMAGE}:${BACKEND_BILLING_TAG}" diff --git a/erpnext-image/build.sh b/erpnext-image/build.sh index 6326150..86cd89f 100755 --- a/erpnext-image/build.sh +++ b/erpnext-image/build.sh @@ -15,6 +15,16 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 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 set -a # shellcheck disable=SC1090 @@ -22,6 +32,10 @@ if [ -f "$ROOT_ENV" ]; then set +a 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)}" CUSTOM_IMAGE="${CUSTOM_IMAGE:-sightbox-erpnext}" CUSTOM_TAG="${CUSTOM_TAG:-${ERPNEXT_VERSION}-branded}" diff --git a/scripts/.env.prod.example b/scripts/.env.prod.example new file mode 100644 index 0000000..769c55b --- /dev/null +++ b/scripts/.env.prod.example @@ -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. diff --git a/scripts/dev.sh b/scripts/dev.sh new file mode 100755 index 0000000..0f7d11f --- /dev/null +++ b/scripts/dev.sh @@ -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." diff --git a/scripts/prod.sh b/scripts/prod.sh new file mode 100755 index 0000000..134b1de --- /dev/null +++ b/scripts/prod.sh @@ -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."