sightbox/backend/build.sh

108 lines
5.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Builds the backend-billing image, optionally pushes it to the registry,
# and optionally triggers a Dokploy redeploy — the same build-locally,
# test-locally, ship-the-exact-tested-artifact pattern erpnext-image/build.sh
# already uses, so the VPS never builds anything itself, only pulls.
#
# Usage (from the sightbox/ project root or from here — either works):
# ./backend/build.sh
#
# Local-only build (no push, no redeploy trigger): just run it with
# BACKEND_BILLING_IMAGE left at its default (a bare local tag, not a
# registry path) — see .env.example. Nothing below pushes or redeploys
# unless BACKEND_BILLING_IMAGE actually points at a registry.
#
# Full build-push-redeploy: set BACKEND_BILLING_IMAGE to the registry path
# (e.g. git.bettertend.net/jasonfraser/sightbox-backend-billing) in .env,
# and additionally set DOKPLOY_URL/DOKPLOY_API_TOKEN/DOKPLOY_COMPOSE_ID (also
# .env.example) to have this script trigger the actual VPS redeploy itself
# once the push succeeds — "pull into the VPS automatically" meaning this
# one command does it, not that Dokploy polls the registry on its own
# (confirmed: Dokploy's native registry-webhook auto-deploy only exists for
# Docker Hub, not a self-hosted registry like this one — see
# playbook/CICD_PLAYBOOK.md). DOKPLOY_COMPOSE_ID, not an "application" id —
# this deployment is a Compose-type service, which is Dokploy's own API's
# own separate resource type (compose.deploy + composeId, not
# application.deploy + applicationId — confirmed against Dokploy's own API
# reference; the two are not interchangeable).
#
# Staged rollout (new container up, old one down) is Compose's own default
# `docker compose up -d` behavior for a changed image — nothing extra
# needed here for that part.
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
source "$ROOT_ENV"
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}"
# A bare local tag (no "/" at all, e.g. "sightbox-backend-billing") means
# "local dev only" — build for this machine's own architecture so it runs
# immediately, nothing to push, nothing to redeploy. A registry path always
# has at least one "/" (host/owner/name or owner/name).
if [[ "${BACKEND_BILLING_IMAGE}" != */* ]]; then
echo "Building ${FULL_TAG} (local dev, host architecture) from ${SCRIPT_DIR}/Dockerfile"
docker build -t "${FULL_TAG}" "${SCRIPT_DIR}"
echo
echo "Built ${FULL_TAG}."
echo "BACKEND_BILLING_IMAGE has no registry path (just \"${BACKEND_BILLING_IMAGE}\") — local build only."
echo "Set in .env (see .env.example):"
echo " BACKEND_BILLING_IMAGE=${BACKEND_BILLING_IMAGE}"
echo " BACKEND_BILLING_TAG=${BACKEND_BILLING_TAG}"
echo "Then: docker compose up -d --force-recreate backend-billing"
exit 0
fi
# A registry path: build specifically for linux/amd64, the real Dokploy VPS
# target — confirmed the hard way: a plain `docker build` on an Apple
# Silicon Mac produces an arm64-only image with no amd64 manifest entry, so
# the (amd64) VPS fails to pull it ("no matching manifest for linux/amd64").
# buildx --platform forces the amd64 target regardless of this machine's
# own architecture (via QEMU emulation if needed); --push uploads the
# result directly, since a cross-platform buildx result can't be loaded
# into the local `docker images` store the way a same-arch build can.
echo "Building ${FULL_TAG} for linux/amd64 (Dokploy VPS target) from ${SCRIPT_DIR}/Dockerfile"
docker buildx build --platform linux/amd64 -t "${FULL_TAG}" --push "${SCRIPT_DIR}"
echo
echo "Built and pushed ${FULL_TAG}."
if [[ -z "${DOKPLOY_URL:-}" || -z "${DOKPLOY_API_TOKEN:-}" || -z "${DOKPLOY_COMPOSE_ID:-}" ]]; then
echo
echo "DOKPLOY_URL/DOKPLOY_API_TOKEN/DOKPLOY_COMPOSE_ID not all set (see .env.example) —"
echo "skipping the redeploy trigger. Redeploy by hand in Dokploy's dashboard, or set"
echo "all three and re-run this script to have it trigger the redeploy itself."
exit 0
fi
echo
echo "Triggering Dokploy redeploy (composeId=${DOKPLOY_COMPOSE_ID})..."
curl -sSf -X POST "${DOKPLOY_URL%/}/api/compose.deploy" \
-H "accept: application/json" \
-H "x-api-key: ${DOKPLOY_API_TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"composeId\": \"${DOKPLOY_COMPOSE_ID}\"}" \
> /dev/null
echo "Triggered. Watch Dokploy's Deployments tab for the result."