feat(deploy): auto-seed a known Payload admin user on every deploy
CI Pipeline with Test Management / 🧪 Run Tests & Generate Reports (push) Canceled after 0s
Test Reporting & Gherkin Analysis / 🧪 Run Tests & Generate Reports (push) Canceled after 0s
CI Pipeline with Test Management / 🐳 Docker Integration Tests (push) Canceled after 0s
CI Pipeline with Test Management / 🏗️ Build Docker Image (push) Canceled after 0s
CI Pipeline with Test Management / 📊 Generate Test Summary (push) Canceled after 0s
Test Reporting & Gherkin Analysis / 📊 Analyze Test Coverage (push) Canceled after 0s
Test Reporting & Gherkin Analysis / 🔄 Sync with Linear (push) Canceled after 0s
Test Reporting & Gherkin Analysis / ⚡ Performance Monitoring (push) Canceled after 0s

Follow-up to MITHOME-97: after every deploy, a brand-new database
(first-ever deploy, or a volume wipe) leaves the Payload admin behind
the "Create first user" screen — someone has to notice and fill it in
by hand, which means environments can silently end up with no known
admin credentials, or with whatever a random person happened to type
in at the time.

deploy.sh now calls Payload's built-in `POST /api/users/first-register`
REST endpoint right after the healthcheck passes, using ADMIN_EMAIL /
ADMIN_PASSWORD from the environment's .env file. That endpoint only
succeeds when the `users` collection is completely empty (throws 403
Forbidden otherwise) — which makes this naturally idempotent: the
first deploy against a fresh database creates the known admin, every
later deploy gets a harmless 403 and skips it. It never overwrites an
existing user's password. Missing ADMIN_EMAIL/ADMIN_PASSWORD in the
env file just skips the step with a warning, it doesn't fail the
deploy.

Documented the new variables in .env.staging.example and
.env.production.example (next to the existing PAYLOAD_SECRET
instructions), and the new deploy.sh step 4 in
.agent/workflows/deploy.md. Also dropped a stale "Content Editor is
staging feliratot kap" line from the same doc (that behavior belonged
to the CMS retired in MITHOME-93 and no longer exists).

Verified locally against real Payload instances (not just reading the
code): a fresh, empty MongoDB returns 200 and creates the user; a
second call against the same now-non-empty database returns 403 and
changes nothing; a database that already had a different user (the
existing dev DB) also correctly returns 403.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Do Siki
2026-09-11 23:12:12 +02:00
co-authored by Claude Sonnet 5
parent 94647d5293
commit 729268a3fe
4 changed files with 79 additions and 8 deletions
+57 -7
View File
@@ -42,8 +42,12 @@ fi
echo "🔑 Környezeti változók betöltése ($ENV_FILE)..."
# WHY: a --env-file kapcsoló csak a compose változó-helyettesítését táplálja;
# a shell nem látja belőle az APP_PORT-ot, ezért a healthcheckhez expliciten kiolvassuk.
APP_PORT_VALUE="$(grep -E '^APP_PORT=' "$ENV_FILE" | tail -n 1 | cut -d= -f2- | tr -d '[:space:]' | tr -d '"' | tr -d "'")"
# a shell nem látja belőle ezeket az értékeket, ezért a healthcheckhez és az
# admin-seedeléshez expliciten kiolvassuk.
env_value() {
grep -E "^$1=" "$ENV_FILE" | tail -n 1 | cut -d= -f2- | tr -d '[:space:]' | tr -d '"' | tr -d "'"
}
APP_PORT_VALUE="$(env_value APP_PORT)"
# 3. Docker konténerek újraépítése és indítása
echo "🐳 Build és indítás..."
@@ -53,15 +57,61 @@ docker compose --env-file "$ENV_FILE" -f "$COMPOSE_FILE" up --build --force-recr
# 4. Healthcheck
echo "⏳ Healthcheck (max 60s)..."
HEALTH_URL="http://localhost:${APP_PORT_VALUE:-$DEFAULT_PORT}/api/health"
HEALTHY=0
for i in $(seq 1 30); do
if curl -sf "$HEALTH_URL" > /dev/null 2>&1; then
echo "✅ Healthcheck OK: $HEALTH_URL"
echo "✅ Deploy sikeres: [$ENV]"
exit 0
HEALTHY=1
break
fi
sleep 2
done
echo "❌ Healthcheck sikertelen: $HEALTH_URL"
docker compose -f "$COMPOSE_FILE" logs app --tail 50
exit 1
if [ "$HEALTHY" -ne 1 ]; then
echo "❌ Healthcheck sikertelen: $HEALTH_URL"
docker compose -f "$COMPOSE_FILE" logs app --tail 50
exit 1
fi
# 5. Payload admin felhasználó biztosítása (MITHOME-97 follow-up)
#
# WHY: minden friss adatbázisú deploy (első staging/prod indítás, vagy egy
# volume-törlés utáni újrakezdés) a Payload "Create first user" képernyőjét
# mutatná — valaki ott manuálisan hozná létre a saját fiókját, ami könnyen
# elmarad vagy elfelejtődik, és ismeretlen/inkonzisztens admin-hozzáférést
# eredményez környezetenként. Payload REST API-ja egy beépített
# `POST /<usersCollection>/first-register` endpointot ad erre — DE csakis
# akkor enged bármit létrehozni, ha a `users` collection még teljesen üres
# (0 dokumentum); ha már van akár egy felhasználó is, 403 Forbidden-t ad.
# Ez pont idempotenssé teszi: minden deploy után lefuttatjuk, első alkalommal
# létrehozza az ismert admin fiókot ADMIN_EMAIL/ADMIN_PASSWORD alapján,
# utána minden további deploy-on ártalmatlanul 403-at kap és kihagyja.
ADMIN_EMAIL_VALUE="$(env_value ADMIN_EMAIL)"
ADMIN_PASSWORD_VALUE="$(env_value ADMIN_PASSWORD)"
if [ -z "$ADMIN_EMAIL_VALUE" ] || [ -z "$ADMIN_PASSWORD_VALUE" ]; then
echo "⚠️ ADMIN_EMAIL/ADMIN_PASSWORD nincs beállítva $ENV_FILE-ban — admin-seedelés kihagyva."
echo " (Egy teljesen friss adatbázisnál manuálisan kell létrehozni az első usert /admin alatt.)"
else
echo "👤 Admin felhasználó biztosítása..."
ADMIN_HTTP_CODE="$(curl -sS -o /tmp/mozdit-admin-seed-response.json -w '%{http_code}' \
-X POST "http://localhost:${APP_PORT_VALUE:-$DEFAULT_PORT}/api/users/first-register" \
-H 'Content-Type: application/json' \
--data-binary @- <<EOF
{"email":"${ADMIN_EMAIL_VALUE}","password":"${ADMIN_PASSWORD_VALUE}"}
EOF
)"
rm -f /tmp/mozdit-admin-seed-response.json
case "$ADMIN_HTTP_CODE" in
200)
echo "✅ Admin felhasználó létrehozva ($ADMIN_EMAIL_VALUE)."
;;
403)
echo "✅ Admin felhasználó már létezik — nincs teendő."
;;
*)
echo "⚠️ Admin-seedelés váratlan válasza: HTTP $ADMIN_HTTP_CODE (a deploy egyébként sikeres, ez nem állítja meg)."
;;
esac
fi
echo "✅ Deploy sikeres: [$ENV]"