| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * systemd ExecStartPre helper: pull/ff-only only when a restart manifest |
| 4 | * requested a code deployment. Crashes without a manifest never pull. |
| 5 | * |
| 6 | * Env: |
| 7 | * QUESTWARD_REPO — git working tree (default: cwd) |
| 8 | * QUESTWARD_DATA — directory containing restart-request.json (default: <repo>/server/data) |
| 9 | * QUESTWARD_GIT_BRANCH — remote branch (default: master) |
| 10 | * (TILE_MMO_* env vars are still accepted as fallbacks.) |
| 11 | */ |
| 12 | import { spawnSync } from "node:child_process"; |
| 13 | import { join } from "node:path"; |
| 14 | import { consumeRestartManifest, restartManifestPath } from "../server/src/deploy/manifest.js"; |
| 15 | |
| 16 | const repo = process.env.QUESTWARD_REPO || process.env.TILE_MMO_REPO || process.cwd(); |
| 17 | const dataDir = process.env.QUESTWARD_DATA || process.env.TILE_MMO_DATA || join(repo, "server", "data"); |
| 18 | const branch = process.env.QUESTWARD_GIT_BRANCH || process.env.TILE_MMO_GIT_BRANCH || "master"; |
| 19 | const manifestPath = restartManifestPath(dataDir); |
| 20 | |
| 21 | const manifest = consumeRestartManifest(manifestPath); |
| 22 | if (!manifest) |
| 23 | { |
| 24 | console.log("[deploy] No restart manifest; starting checked-out version."); |
| 25 | process.exit(0); |
| 26 | } |
| 27 | |
| 28 | console.log("[deploy] Consumed restart manifest:", JSON.stringify(manifest)); |
| 29 | |
| 30 | if (!manifest.deployCode) |
| 31 | { |
| 32 | console.log("[deploy] World-only / non-code restart; skipping git pull."); |
| 33 | process.exit(0); |
| 34 | } |
| 35 | |
| 36 | function git (args) |
| 37 | { |
| 38 | const result = spawnSync("git", args, { |
| 39 | cwd: repo, |
| 40 | stdio: "inherit", |
| 41 | windowsHide: true |
| 42 | }); |
| 43 | if (result.status !== 0) |
| 44 | { |
| 45 | console.error(`[deploy] git ${args.join(" ")} failed with status ${result.status}`); |
| 46 | process.exit(result.status ?? 1); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | git(["fetch", "origin", branch]); |
| 51 | if (manifest.targetCommit) |
| 52 | { |
| 53 | git(["merge", "--ff-only", manifest.targetCommit]); |
| 54 | } |
| 55 | else |
| 56 | { git(["merge", "--ff-only", `origin/${branch}`]); } |
| 57 | |
| 58 | console.log("[deploy] Fast-forward complete."); |
| 59 | process.exit(0); |