Unify Wiki and Studio application and simplify deployment
Unified application CI and deployment / verify-and-release (pull_request) Waiting to run
python CI and deployment / verify-and-deploy (pull_request) Successful in 1m12s

This commit is contained in:
2026-11-04 03:41:16 +08:00
parent 80d23f0e3d
commit 6654a1456b
101 changed files with 304 additions and 1776 deletions
+1 -2
View File
@@ -5,7 +5,6 @@ const root=new URL('../',import.meta.url),read=p=>readFile(new URL(p,root)),raw=
const image=async p=>'/api/media/'+managedMedia['content/layout-reference/assets/'+p.replace(/^asset:/,'')].id;
const layouts=[];
for(const m of raw){const elements=[];for(const e of m.elements)elements.push({...e,raster:await image(e.asset),...(e.mask?{shape:await image(e.mask)}:{})});layouts.push({id:m.id,name:m.name,width:m.width,height:m.height,tutorialUrl:'/explore/'+ids[m.id],sourceSha256:m.source.sha256,source:await image(m.reference),background:await image(m.background),elements});}
await writeFile(new URL('apps/studio/src/content/templates/layouts.json',root),JSON.stringify(layouts));
await writeFile(new URL('apps/waterfall-wiki/src/studio/content/templates/layouts.json',root),JSON.stringify(layouts));
await writeFile(new URL('apps/waterfall-wiki/src/content/studio-layouts.json',root),JSON.stringify(layouts.map(({source,background,elements,...l})=>({...l,elements:elements.map(({id,kind,role,box,direction,text,reading})=>({id,kind,role,box,direction,text,reading}))}))));
await writeFile(new URL('apps/waterfall-wiki/src/lib/layout-parameters.ts',root),await read('apps/studio/src/lib/layout-parameters.ts'));
console.log('Packaged '+layouts.length+' corresponding layouts');
+2 -2
View File
@@ -2,8 +2,8 @@
import {readFile} from 'node:fs/promises';
import assert from 'node:assert/strict';
const root=new URL('../',import.meta.url),read=path=>readFile(new URL(path,root),'utf8');
for(const file of ['layout-parameters.ts','series.ts','canvas-document.ts'])assert.equal(await read('apps/studio/src/lib/'+file),await read('apps/waterfall-wiki/src/lib/'+file),file+' contract drift');
const canonical=await read('content/studio-examples/autumn-journal.json');for(const app of ['studio','waterfall-wiki'])assert.equal(canonical,await read('apps/'+app+'/fixtures/autumn-journal.json'),'Example fixture drift');
for(const file of ['layout-parameters.ts','series.ts','canvas-document.ts'])assert.equal(await read('apps/waterfall-wiki/src/studio/lib/'+file),`export * from '../../lib/${file}';\n`,file+' must use shared implementation');
const canonical=await read('content/studio-examples/autumn-journal.json');for(const file of ['apps/waterfall-wiki/fixtures/autumn-journal.json','apps/waterfall-wiki/fixtures/studio/autumn-journal.json'])assert.equal(canonical,await read(file),'Example fixture drift');
const doc=await read('apps/waterfall-wiki/src/lib/studio-design-documentation.ts'),encoded=doc.match(/export const designDocs=(.*);\n/);
assert.ok(encoded);assert.equal(JSON.parse(encoded[1]),(await read('content/studio-tools/layouts-api.md')).replace('{{LAYOUTS}}','读取 /studio/api/layouts 获取全部27套构图。'),'Wiki/agent documentation drift');
console.log('Studio content, validation and documentation contracts agree');
-24
View File
@@ -1,24 +0,0 @@
#!/usr/bin/env bash
set -Eeuo pipefail
if [ "$(id -u)" -ne 0 ]; then exec sudo -n bash "$0" "$@"; fi
sha=${1:?release SHA required}
[[ "$sha" =~ ^[0-9a-f]{40}$ ]]
exec 9>/var/lock/prompt-studio.lock
flock -w 300 9
latest=$(git -c safe.directory="$PWD" ls-remote origin refs/heads/main | cut -f1)
if [ "$latest" != "$sha" ]; then
git -c safe.directory="$PWD" fetch origin main
git -c safe.directory="$PWD" diff --quiet "$sha" origin/main -- apps/studio scripts/deploy-studio.sh .gitea/workflows/waterfall-wiki.yml pnpm-workspace.yaml || exit 0
fi
mkdir -p /opt/prompt-studio
previous=$(cat /opt/prompt-studio/image 2>/dev/null || true)
write_compose(){ printf 'services:\n studio:\n image: %s\n restart: unless-stopped\n ports: ["127.0.0.1:18134:3000"]\n networks:\n waterfall-wiki:\n aliases: [prompt-studio]\nnetworks:\n waterfall-wiki:\n external: true\n name: prompt_default\n' "$1" > /opt/prompt-studio/compose.yml; }
write_compose "prompt-studio:$sha"
rollback(){ if [ -n "$previous" ]; then write_compose "$previous"; docker compose -p prompt-studio -f /opt/prompt-studio/compose.yml up -d; fi; }
trap rollback ERR
docker compose -p prompt-studio -f /opt/prompt-studio/compose.yml up -d
for attempt in {1..40}; do
if curl -fsS http://127.0.0.1:18134/studio/api/health >/dev/null; then printf '%s' "prompt-studio:$sha" > /opt/prompt-studio/image; exit 0; fi
sleep 2
done
false
+12 -4
View File
@@ -17,7 +17,7 @@ test "$(git -c safe.directory="$PWD" rev-parse HEAD)" = "$RELEASE_SHA"
LATEST_MAIN=$(git -c safe.directory="$PWD" ls-remote origin refs/heads/main | cut -f1)
if [ "$LATEST_MAIN" != "$RELEASE_SHA" ]; then
git -c safe.directory="$PWD" fetch origin main
if ! git -c safe.directory="$PWD" diff --quiet "$RELEASE_SHA" origin/main -- apps/waterfall-wiki Dockerfile compose.yml scripts/deploy.sh scripts/wiki-image-lifecycle.py scripts/infra.sh scripts/integration.sh scripts/auth-admin.sh .gitea/workflows/waterfall-wiki.yml pnpm-workspace.yaml; then
if ! git -c safe.directory="$PWD" diff --quiet "$RELEASE_SHA" origin/main -- apps/waterfall-wiki Dockerfile compose.yml scripts/deploy.sh scripts/wiki-image-lifecycle.py scripts/infra.sh scripts/integration.sh scripts/auth-admin.sh .gitea/workflows/app.yml pnpm-workspace.yaml packages/brand; then
echo 'Skipping deployment: Wiki has a newer commit'; exit 0
fi
fi
@@ -61,7 +61,7 @@ rollback(){
echo 'Restored the database backup compatible with the previous application'
fi
if [ -n "$PREVIOUS" ] && [ -f "$PREVIOUS/compose.yml" ]; then
compose_at "$PREVIOUS" up -d --no-build "$PREVIOUS_WEB" "$PREVIOUS_WORKER"
compose_at "$PREVIOUS" up -d --no-build --no-deps "$PREVIOUS_WEB" "$PREVIOUS_WORKER"
echo 'Restored the previous Docker application image'
else
compose stop waterfall-wiki waterfall-wiki-worker
@@ -82,7 +82,8 @@ rollback(){
}
trap rollback ERR
# Keep the same prompt_postgres_data and prompt_objects volumes. No production down -v.
compose up -d --no-build --wait --wait-timeout 120 postgres objects
# Infrastructure already exists; never recreate it during an app release.
compose exec -T postgres pg_isready -U prompt -d prompt
install -d -o root -g root -m 0700 /var/lib/prompt/backups
CUTOVER=1
compose stop waterfall-wiki waterfall-wiki-worker
@@ -98,7 +99,7 @@ CUTOVER=1
# One-time migration: finish a current host worker job before switching the web port.
if [ "$LEGACY_WORKER" -eq 1 ] || [ "$LEGACY_WORKER_ENABLED" -eq 1 ]; then systemctl disable --now prompt-worker.service; fi
if [ "$LEGACY_WEB" -eq 1 ] || [ "$LEGACY_WEB_ENABLED" -eq 1 ]; then systemctl disable --now prompt.service; fi
compose up -d --no-build --wait --wait-timeout 180 waterfall-wiki waterfall-wiki-worker
compose up -d --no-build --no-deps --wait --wait-timeout 180 waterfall-wiki waterfall-wiki-worker
compose exec -T waterfall-wiki node container-health.mjs
compose exec -T waterfall-wiki node auth-cli.cjs verify
compose exec -T waterfall-wiki node auth-cli.cjs run scripts/import-character-packs.mjs
@@ -123,6 +124,8 @@ compose exec -T waterfall-wiki sh -c 'cat > /tmp/prompt-release-check.mjs && nod
})().catch(error=>{console.error(error.message);process.exit(1)});
NODE
bash scripts/verify-studio-production.sh
ln -sfn "$RELEASE_DIR" "$APP_ROOT/.current.next"
mv -Tf "$APP_ROOT/.current.next" "$APP_ROOT/current"
install -m 0755 scripts/auth-admin.sh "$APP_ROOT/auth"
@@ -134,6 +137,11 @@ done
if command -v systemctl >/dev/null; then
if systemctl is-active --quiet prompt.service || systemctl is-active --quiet prompt-worker.service; then echo 'Legacy host service is unexpectedly running'; false; fi
fi
if docker inspect prompt-studio-studio-1 >/dev/null 2>&1; then
if [ "$(docker inspect --format '{{index .Config.Labels "com.docker.compose.project"}}' prompt-studio-studio-1)" = prompt-studio ]; then
docker stop prompt-studio-studio-1
fi
fi
echo "Full Docker deployment verified: web+worker+postgres+objects port=$PORT release=$RELEASE_SHA"
# Register only after every deployment check succeeded; ledger failure retains old images.
if ! python3 scripts/wiki-image-lifecycle.py record-success --sha "$RELEASE_SHA"; then
+7 -15
View File
@@ -1,17 +1,9 @@
#!/usr/bin/env bash
set -Eeuo pipefail
IMAGE="${1:?Application image is required}"
# Preserve existing credentials and Docker project/volume names during migration.
if [ ! -f /etc/prompt.env ]; then
umask 077
TEMP_ENV=$(mktemp /etc/prompt.env.XXXXXX)
trap 'rm -f "$TEMP_ENV"' EXIT
docker run --rm --entrypoint node "$IMAGE" -e "const {randomBytes}=require('crypto');const hex=()=>randomBytes(32).toString('hex');console.log('POSTGRES_PASSWORD='+hex()+'\nS3_ACCESS_KEY='+hex()+'\nS3_SECRET_KEY='+hex()+'\nS3_BUCKET=prompt')" > "$TEMP_ENV"
mv "$TEMP_ENV" /etc/prompt.env
trap - EXIT
fi
# Current public entry uses HTTP. Operators can set true here after enabling HTTPS.
if ! grep -q '^AUTH_COOKIE_SECURE=' /etc/prompt.env; then
printf '\nAUTH_COOKIE_SECURE=false\n' >> /etc/prompt.env
fi
# Provision credentials and base containers separately; normal releases only verify.
test -s /etc/prompt.env || { echo 'Missing /etc/prompt.env; initialize infrastructure separately' >&2; exit 1; }
for service in postgres objects; do
id=$(docker compose -p prompt --env-file /etc/prompt.env -f compose.yml ps -q "$service")
test -n "$id" || { echo "Infrastructure missing: $service; run scripts/init-infra.sh separately" >&2; exit 1; }
test "$(docker inspect --format '{{.State.Running}}' "$id")" = true
done
+6
View File
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -Eeuo pipefail
# Explicit operator action, never invoked by the application release workflow.
if [ "$(id -u)" -ne 0 ]; then exec sudo -n bash "$0" "$@"; fi
test -s /etc/prompt.env || { echo 'Configure /etc/prompt.env before initializing infrastructure' >&2; exit 1; }
docker compose -p prompt --env-file /etc/prompt.env -f compose.yml up -d --no-build --wait postgres objects
+35 -35
View File
@@ -1,49 +1,49 @@
#!/usr/bin/env bash
set -Eeuo pipefail
: "${WATERFALL_WIKI_IMAGE:?Build the runtime image first}"
: "${WATERFALL_WIKI_TEST_IMAGE:?Build the integration image first}"
: "${WATERFALL_WIKI_IMAGE:?Build runtime image first}"
: "${WATERFALL_WIKI_TEST_IMAGE:?Build integration image first}"
export GITEA_RUN_ID="${GITEA_RUN_ID:-$$}"
export COMPOSE_PROJECT_NAME="prompt-ci-$GITEA_RUN_ID"
[[ "$GITEA_RUN_ID" =~ ^[1-9][0-9]*$ ]]
# Only one candidate uses the shared test slot at a time. Production is project prompt.
exec 8>/var/lock/prompt-ci-integration.lock
flock -w 900 8
export COMPOSE_PROJECT_NAME=prompt-ci-shared
export PG_PORT=0 S3_PORT=0 WEB_PORT=0 WEB_BIND_ADDRESS=127.0.0.1
TEST_ENV=$(mktemp)
chmod 0600 "$TEST_ENV"
COMPOSE_FILES=(-f compose.yml)
STUDIO_SERVICES=()
if [[ -n "${STUDIO_REVIEW_IMAGE:-}" ]]; then
COMPOSE_FILES+=(-f compose.creative-review.yml)
STUDIO_SERVICES+=(prompt-studio)
STATE=/var/lib/prompt-ci
umask 077
mkdir -p "$STATE"
if [ ! -s "$STATE/credentials.env" ]; then
docker run --rm --entrypoint node "$WATERFALL_WIKI_IMAGE" -e "const c=require('crypto');const hex=()=>c.randomBytes(24).toString('hex');console.log('POSTGRES_PASSWORD='+hex()+'\nS3_ACCESS_KEY='+hex()+'\nS3_SECRET_KEY='+hex())" > "$STATE/credentials.env.tmp"
mv "$STATE/credentials.env.tmp" "$STATE/credentials.env"
fi
compose(){ docker compose -p "$COMPOSE_PROJECT_NAME" --env-file "$TEST_ENV" "${COMPOSE_FILES[@]}" "$@"; }
TEST_ENV=$(mktemp)
cat "$STATE/credentials.env" > "$TEST_ENV"
printf '\nS3_BUCKET=ci-%s\nAUTH_DISABLED_FOR_TESTS=true\nACCOUNT_PUBLIC_ORIGIN=http://waterfall-wiki:3000\nAUTH_COOKIE_SECURE=false\n' "$GITEA_RUN_ID" >> "$TEST_ENV"
compose(){ docker compose -p "$COMPOSE_PROJECT_NAME" --env-file "$TEST_ENV" -f compose.yml "$@"; }
cleanup(){
code=$?
trap - EXIT INT TERM
if [ "$code" -ne 0 ]; then
echo 'Runner pressure at integration failure (before disposable stack cleanup)'
uptime || true
free -m || true
df -h / || true
for resource in cpu memory io; do cat "/proc/pressure/$resource" || true; done
timeout 10s docker stats --no-stream --format '{{.Name}} cpu={{.CPUPerc}} memory={{.MemUsage}} io={{.BlockIO}} pids={{.PIDs}}' || true
compose logs --tail 160 waterfall-wiki waterfall-wiki-worker "${STUDIO_SERVICES[@]}" || true
fi
cleanup_code=0
bash scripts/cleanup-integration.sh || cleanup_code=$?
trap - EXIT
if [ "$code" -ne 0 ]; then compose logs --tail 100 waterfall-wiki waterfall-wiki-worker || true; fi
compose stop waterfall-wiki waterfall-wiki-worker || true
docker run --rm --network prompt-ci-shared_default --env-file "$TEST_ENV" \
--mount "type=bind,src=$PWD/apps/waterfall-wiki/scripts/cleanup-ci-bucket.mjs,dst=/repo/apps/waterfall-wiki/cleanup-ci-bucket.mjs,readonly" \
--entrypoint node "$WATERFALL_WIKI_TEST_IMAGE" /repo/apps/waterfall-wiki/cleanup-ci-bucket.mjs || { echo 'CI bucket cleanup failed' >&2; code=1; }
rm -f "$TEST_ENV"
if [ "$code" -ne 0 ]; then exit "$code"; fi
exit "$cleanup_code"
exit "$code"
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
# Random credentials exist only for this disposable stack; no host Node required.
docker run --rm --entrypoint node "$WATERFALL_WIKI_IMAGE" -e "const c=require('crypto');const hex=()=>c.randomBytes(24).toString('hex');console.log('POSTGRES_PASSWORD='+hex()+'\nS3_ACCESS_KEY='+hex()+'\nS3_SECRET_KEY='+hex())" > "$TEST_ENV"
# Explicitly isolate legacy unauthenticated integration coverage; production fails closed.
printf '\nAUTH_DISABLED_FOR_TESTS=true\n' >> "$TEST_ENV"
if [[ -n "${STUDIO_REVIEW_IMAGE:-}" ]]; then printf '\nACCOUNT_PUBLIC_ORIGIN=http://waterfall-wiki:3000\nAUTH_COOKIE_SECURE=false\n' >> "$TEST_ENV"; fi
compose up -d --no-build --wait --wait-timeout 120 postgres objects
# Bootstrap once, otherwise reuse base containers and their volumes.
compose up -d --no-build --no-recreate --wait --wait-timeout 120 postgres objects
compose stop waterfall-wiki waterfall-wiki-worker
id=$(compose ps -q postgres)
[[ "$(docker inspect --format '{{index .Config.Labels "com.docker.compose.project"}}' "$id")" == prompt-ci-shared ]]
# Reset only this dedicated test database; no production volume or database is addressed.
compose exec -T postgres dropdb -U prompt --if-exists --force prompt
compose exec -T postgres createdb -U prompt prompt
compose run --rm --no-deps migrate
compose up -d --no-build --wait --wait-timeout 150 waterfall-wiki waterfall-wiki-worker "${STUDIO_SERVICES[@]}"
# The test runner calls the actual non-root production web/worker containers.
compose up -d --no-build --no-deps --wait --wait-timeout 150 waterfall-wiki waterfall-wiki-worker
compose run --rm --no-deps tests
compose exec -T waterfall-wiki node container-health.mjs
compose exec -T waterfall-wiki-worker sh -c 'test "$(id -u)" -ne 0 && ffmpeg -version >/dev/null'
@@ -51,8 +51,8 @@ compose exec -T waterfall-wiki-worker sh -c 'test "$(id -u)" -ne 0 && ffmpeg -ve
# Repeat route and login checks with production authentication enabled.
printf '\nAUTH_CONFIG_FILE=/auth/config.json\nAUTH_COOKIE_SECURE=false\n' >> "$TEST_ENV"
compose run --rm --no-deps auth-admin
compose up -d --no-build --wait --wait-timeout 150 waterfall-wiki
compose up -d --no-build --no-deps --wait --wait-timeout 150 waterfall-wiki
compose exec -T waterfall-wiki node auth-cli.cjs exercise
compose restart waterfall-wiki
compose up -d --no-build --wait --wait-timeout 150 waterfall-wiki
compose up -d --no-build --no-deps --wait --wait-timeout 150 waterfall-wiki
compose exec -T waterfall-wiki node auth-cli.cjs verify
+1 -1
View File
@@ -49,4 +49,4 @@ docker run --rm \
--mount "type=bind,src=$RESULT_DIR,dst=/tmp/studio-library" \
-e NODE_PATH=/repo/apps/waterfall-wiki/node_modules -e CHROMIUM_PATH=/usr/bin/chromium \
-e SCREENSHOT_DIR=/tmp/studio-library/visual \
--entrypoint node "$TEST_IMAGE" /delivery/apps/studio/scripts/infinite-canvas-smoke.cjs
--entrypoint node "$TEST_IMAGE" /delivery/apps/waterfall-wiki/scripts/infinite-canvas-smoke.cjs
-6
View File
@@ -175,12 +175,6 @@ def ci(root, sha):
code = 0
except subprocess.CalledProcessError as error:
code = error.returncode
finally:
try:
cleanup(root, apply=True, completed_test=sha if code == 0 else None)
except Exception as error:
# Housekeeping must not mask the build/deployment result or roll back healthy services.
print(f"Cleanup failed; images retained for later retry: {error}", file=sys.stderr)
return code