#!/usr/bin/env bash # Pack the current extension, serve it, and point the shared nginx at it. # Safe to re-run any time — this is the normal "publish a release" command. set -euo pipefail cd "$(dirname "$0")" log() { printf '\n\033[1;34m==> %s\033[0m\n' "$1"; } fail() { printf '\n\033[1;31mERROR: %s\033[0m\n' "$1" >&2; exit 1; } [ -f .env ] || fail "no .env — copy .env.example to .env and fill it in" set -a; . ./.env; set +a : "${EXT_HOST:?set EXT_HOST in .env}" SHARED_STACK="${SHARED_STACK:-/opt/autotrader/deploy}" command -v envsubst >/dev/null || fail "envsubst missing — apt-get install -y gettext-base" [ -d "$SHARED_STACK/nginx/conf.d" ] || fail "no nginx/conf.d under $SHARED_STACK" ./pack-extension.sh log "Starting the file server" docker compose up -d # Rendered into the *other* stack's conf.d, because that is the directory its # nginx has mounted. The file is untracked there, and survives that stack's # pulls and deploys; only its bootstrap-tls.sh wipes conf.d, and re-running this # script puts it back. # # Held back until the certificate exists: nginx refuses to load a server block # whose ssl_certificate is missing, and a broken file in that directory would # also abort the autotrader deploys that reload the same nginx. if docker run --rm -v autotrader-vps_letsencrypt:/le alpine \ test -f "/le/live/$EXT_HOST/fullchain.pem" 2>/dev/null; then template=nginx/ext.conf.template else log "No certificate for $EXT_HOST yet — rendering the HTTP-only config" log "Run ./bootstrap-tls.sh once DNS points here, then this script again" template=nginx/ext-acme.conf.template fi log "Rendering $template" # Named explicitly so envsubst leaves nginx's own $host, $remote_addr and the # rest alone. envsubst '${EXT_HOST}' < "$template" > "$SHARED_STACK/nginx/conf.d/ext.conf" log "Reloading the shared nginx" docker exec nginx nginx -t docker exec nginx nginx -s reload log "Status" docker compose ps