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