sightbox/generate-dokploy-compose.sh

114 lines
6.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Regenerates docker-compose.dokploy.yml from docker-compose.yml.
#
# Why this exists: Dokploy's own compose validator doesn't support the
# top-level `include:` directive docker-compose.yml relies on to pull in
# frappe_docker's compose files without duplicating them (confirmed against
# a real deploy — "validating ...: (root) Additional property include is
# not allowed" — and matches two open, unresolved Dokploy feature requests:
# https://github.com/Dokploy/dokploy/issues/3116 (include support) and
# https://github.com/Dokploy/dokploy/issues/1727 (multiple compose files).
# Neither is implemented as of writing, so Dokploy needs one flat file with
# no `include:` — this script produces exactly that.
#
# --no-interpolate is the deliberate, load-bearing flag here: it resolves
# and flattens `include:` (merging in every frappe_docker file it
# references) while leaving every ${VAR} reference as a literal string
# instead of substituting today's local values into the committed file.
# Without it, this file would bake in whatever's in the machine's .env at
# generation time — including things like FRAPPE_IMAGE_PLATFORM=linux/arm64
# on an Apple Silicon dev machine — permanently, defeating the entire point
# of Dokploy's own Environment tab controlling these per deployment.
#
# --no-path-resolution is equally load-bearing: without it, Compose
# resolves every relative path (`./backend`, `./website/website-bettersightbox/bettersightbox-index.html`,
# `./backend/tenants.json`, `./backend/.env`) to an *absolute* path on
# whatever machine generates the file — confirmed by generating this once
# without the flag and finding the generating machine's own home directory
# baked into backend-billing's build context. The flag keeps them relative,
# which is what actually works once Dokploy clones this repo to its own,
# different path on the VPS.
#
# A second, separate Dokploy validator limitation, also confirmed on a real
# deploy: it rejects Compose's long-form `env_file` entries too —
# `services.backend-billing.env_file.0 must be a string` — because
# `docker compose config` always expands `env_file: [./backend/.env]` to
# the long form (`- path: ./backend/.env` / `required: true`) in its output,
# with no flag to suppress it (checked: --no-normalize doesn't affect this
# either). The Python filter below collapses that structure back to the
# plain string form, for every service, not just backend-billing — safe to
# do broadly, since the short form already implies "must exist", the exact
# behavior `required: true` spells out.
#
# A third, separate Dokploy validator rejection, also confirmed on a real
# deploy: `services.configurator.depends_on.db Additional property required
# is not allowed`. Compose's long-form `depends_on` has long supported
# `condition:` (service_started/healthy/completed_successfully) — that part
# is fine. `required:` alongside it (marking a dependency as optional) is
# the newer addition Dokploy's schema doesn't know about, same pattern as
# the other two rejections above. The second filter below drops just the
# `required:` line, keeping `condition:` — dropping it is safe because the
# default when omitted is already `required: true` (a hard dependency),
# which is what every `depends_on` entry in this file actually uses.
#
# Usage: ./generate-dokploy-compose.sh
# Run this any time docker-compose.yml changes,
# website/website-bettersightbox/bettersightbox-index.html changes, or
# frappe_docker is repointed at a different pinned commit (see
# .gitmodules) — this file is generated output, never hand-edited.
# scripts/prod.sh runs it on every ship, so in practice it's regenerated
# (and MARKETING_DEPLOY_TOKEN below changes, see that variable's own
# comment) on every single deploy regardless of what you were actually
# shipping.
#
# MARKETING_DEPLOY_TOKEN: baked in below as a literal value (not left as
# ${VAR}, unlike everything else --no-interpolate preserves) — forces
# Dokploy's AutoDeploy to recreate the marketing container on every
# deploy. Confirmed the hard way that this has to be EVERY deploy, not
# just ones that change bettersightbox-index.html's content (an earlier
# version of this hashed just that one file's bytes): Dokploy deletes and
# re-clones the ENTIRE git checkout on every deploy, which orphans
# marketing's assets/ directory bind mount (the container's existing
# mount still points at the old, now-deleted directory's inode) whenever
# the container itself isn't ALSO recreated in that same deploy — a
# content-only hash misses every deploy that doesn't touch that one file,
# even though the re-clone (and the orphaning) happens regardless. A
# fresh git commit SHA changes on every real ship, so it's a correct
# "did a deploy happen" signal in a way file-content hashing never was.
# Every other variable stays a live ${VAR} reference on purpose; this one
# specifically must not, since its entire job is to be a different
# literal string on every deploy.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
OUT="docker-compose.dokploy.yml"
MARKETING_DEPLOY_TOKEN="$(git rev-parse --short HEAD)"
{
echo "# GENERATED FILE — do not edit by hand."
echo "# Produced by ./generate-dokploy-compose.sh from docker-compose.yml + the"
echo "# frappe_docker files it includes. Regenerate after changing either, or"
echo "# any other time you deploy (see MARKETING_DEPLOY_TOKEN above)."
echo "# This is what Dokploy's Compose Path should point to — not"
echo "# docker-compose.yml itself, which Dokploy's validator rejects (see"
echo "# this script's own header comment for why)."
echo "#"
docker compose -f docker-compose.yml config --no-interpolate --no-path-resolution \
| python3 -c "
import sys, re
text = sys.stdin.read()
# env_file long form -> plain string (Dokploy rejection #2).
text = re.sub(r'^(\s*)- path: (.+)\n\s*required: (?:true|false)\s*\$', lambda m: f'{m.group(1)}- {m.group(2)}', text, flags=re.MULTILINE)
# depends_on: drop required:, keep condition: (Dokploy rejection #3).
text = re.sub(r'^(\s*condition: .+)\n\s*required: (?:true|false)\s*\$', r'\1', text, flags=re.MULTILINE)
sys.stdout.write(text)
" \
| sed "s/\${MARKETING_DEPLOY_TOKEN:-dev}/${MARKETING_DEPLOY_TOKEN}/"
} > "$OUT"
echo "Wrote ${OUT} (marketing deploy token: ${MARKETING_DEPLOY_TOKEN})."
echo "Set Dokploy's Compose Path to ./${OUT}, not ./docker-compose.yml, and redeploy."