BlueGit

timfa questward

Files

questward / deploy / update-if-requested.mjs

update-if-requested.mjs

update-if-requested.mjs 1.7 KiB Download Raw
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 */
11import { spawnSync } from "node:child_process";
12import { join } from "node:path";
13import { consumeRestartManifest, restartManifestPath } from "../server/src/deploy/manifest.js";
14
15const repo = process.env.TILE_MMO_REPO || process.cwd();
16const dataDir = process.env.TILE_MMO_DATA || join(repo, "server", "data");
17const branch = process.env.TILE_MMO_GIT_BRANCH || "master";
18const manifestPath = restartManifestPath(dataDir);
19
20const manifest = consumeRestartManifest(manifestPath);
21if (!manifest)
22{
23 console.log("[deploy] No restart manifest; starting checked-out version.");
24 process.exit(0);
25}
26
27console.log("[deploy] Consumed restart manifest:", JSON.stringify(manifest));
28
29if (!manifest.deployCode)
30{
31 console.log("[deploy] World-only / non-code restart; skipping git pull.");
32 process.exit(0);
33}
34
35function 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
49git(["fetch", "origin", branch]);
50if (manifest.targetCommit)
51{
52 git(["merge", "--ff-only", manifest.targetCommit]);
53}
54else
55{ git(["merge", "--ff-only", `origin/${branch}`]); }
56
57console.log("[deploy] Fast-forward complete.");
58process.exit(0);