sightbox/generate-dokploy-compose.sh

100 lines
5.5 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`, `./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, bettersightbox-index.html
# changes, or frappe_docker is repointed at a different pinned commit (see
# .gitmodules) — this file is generated output, never hand-edited.
#
# MARKETING_HTML_HASH: baked in below as a literal value (not left as
# ${VAR}, unlike everything else --no-interpolate preserves) — this is
# what actually forces Dokploy's AutoDeploy to recreate the marketing
# container when only bettersightbox-index.html changed; see that
# service's own comment in docker-compose.yml for the full story. 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 every time the file's content changes.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
OUT="docker-compose.dokploy.yml"
MARKETING_HTML_HASH="$(sha256sum bettersightbox-index.html | cut -c1-12)"
{
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 "# after changing bettersightbox-index.html (see MARKETING_HTML_HASH 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_HTML_HASH:-dev}/${MARKETING_HTML_HASH}/"
} > "$OUT"
echo "Wrote ${OUT} (marketing content-hash: ${MARKETING_HTML_HASH})."
echo "Set Dokploy's Compose Path to ./${OUT}, not ./docker-compose.yml, and redeploy."