#!/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" if [ -f "$ROOT_ENV" ]; then set -a # shellcheck disable=SC1090 source "$ROOT_ENV" set +a fi BACKEND_BILLING_IMAGE="${BACKEND_BILLING_IMAGE:-sightbox-backend-billing}" BACKEND_BILLING_TAG="${BACKEND_BILLING_TAG:-latest}" FULL_TAG="${BACKEND_BILLING_IMAGE}:${BACKEND_BILLING_TAG}" echo "Building ${FULL_TAG} from ${SCRIPT_DIR}/Dockerfile" docker build -t "${FULL_TAG}" "${SCRIPT_DIR}" echo echo "Built ${FULL_TAG}." # A bare local tag (no "/" at all, e.g. "sightbox-backend-billing") means # "local dev only" — 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 "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 echo "Pushing ${FULL_TAG}..." docker push "${FULL_TAG}" echo "Pushed." 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."