|
| 1 | +import * as fs from "node:fs"; |
| 2 | +import * as path from "node:path"; |
| 3 | +import { generateAppVdf, generateDepotVdf } from "./vdf-generator"; |
| 4 | +import { SteamCmdRunner } from "./steamcmd-runner"; |
| 5 | + |
| 6 | +export interface SteamDeployOptions { |
| 7 | + buildPath?: string; |
| 8 | + appId?: string; |
| 9 | + depotId?: string; |
| 10 | + branch?: string; |
| 11 | + description?: string; |
| 12 | + mode?: string; |
| 13 | + steamCmdPath?: string; |
| 14 | + steamConfigDir?: string; |
| 15 | + extraExclusions?: string; |
| 16 | + [key: string]: unknown; |
| 17 | +} |
| 18 | + |
| 19 | +interface YargsLike { |
| 20 | + option: (name: string, config: Record<string, unknown>) => YargsLike; |
| 21 | +} |
| 22 | + |
| 23 | +export class SteamDeployCommand { |
| 24 | + public readonly name = "Deploy steam"; |
| 25 | + |
| 26 | + public async configureOptions(yargs: YargsLike): Promise<void> { |
| 27 | + yargs |
| 28 | + .option("appId", { |
| 29 | + describe: "Steam App ID", |
| 30 | + type: "string", |
| 31 | + demandOption: true, |
| 32 | + }) |
| 33 | + .option("depotId", { |
| 34 | + describe: "Steam Depot ID", |
| 35 | + type: "string", |
| 36 | + demandOption: true, |
| 37 | + }) |
| 38 | + .option("branch", { |
| 39 | + describe: 'Steam branch to publish to (SteamCMD\'s "setlive" field)', |
| 40 | + type: "string", |
| 41 | + default: "default", |
| 42 | + }) |
| 43 | + .option("description", { |
| 44 | + describe: |
| 45 | + "Build description shown in the Steam build history. Defaults to the branch name and current timestamp.", |
| 46 | + type: "string", |
| 47 | + }) |
| 48 | + .option("mode", { |
| 49 | + describe: "How to run steamcmd: auto (default), local, or docker", |
| 50 | + type: "string", |
| 51 | + default: "auto", |
| 52 | + }) |
| 53 | + .option("steamCmdPath", { |
| 54 | + describe: "Explicit path to the steamcmd executable. Recommended for CI determinism; skips auto-detection.", |
| 55 | + type: "string", |
| 56 | + }) |
| 57 | + .option("steamConfigDir", { |
| 58 | + describe: |
| 59 | + "Host directory containing Steam's config.vdf, mounted into the container in docker mode so login sessions persist.", |
| 60 | + type: "string", |
| 61 | + }) |
| 62 | + .option("extraExclusions", { |
| 63 | + describe: |
| 64 | + "Comma-separated extra file-exclusion glob patterns for the depot, beyond the built-in defaults (*.pdb, *.log, *.vdf, Burst debug/backup folders).", |
| 65 | + type: "string", |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + public async execute(options: SteamDeployOptions): Promise<boolean> { |
| 70 | + const buildPath = options.buildPath; |
| 71 | + if (!buildPath) { |
| 72 | + throw new Error("A build path is required: game-ci deploy steam <buildPath>"); |
| 73 | + } |
| 74 | + if (!fs.existsSync(buildPath)) { |
| 75 | + throw new Error(`Build path does not exist: ${buildPath}`); |
| 76 | + } |
| 77 | + |
| 78 | + const username = process.env.STEAM_USERNAME; |
| 79 | + const password = process.env.STEAM_PASSWORD; |
| 80 | + if (!username || !password) { |
| 81 | + throw new Error( |
| 82 | + "STEAM_USERNAME and STEAM_PASSWORD must be set as environment variables (never as CLI arguments - argv can leak through process listings).", |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + const appId = options.appId!; |
| 87 | + const depotId = options.depotId!; |
| 88 | + const branch = options.branch ?? "default"; |
| 89 | + const description = options.description ?? `${branch} build ${new Date().toISOString()}`; |
| 90 | + const mode = (options.mode ?? "auto") as "auto" | "local" | "docker"; |
| 91 | + const extraExclusions = options.extraExclusions |
| 92 | + ? options.extraExclusions.split(",").map((s) => s.trim()) |
| 93 | + : undefined; |
| 94 | + |
| 95 | + const absoluteBuildPath = path.resolve(buildPath); |
| 96 | + const contentRoot = mode === "docker" ? "/build" : absoluteBuildPath.replace(/\\/g, "/"); |
| 97 | + const depotFileName = `depot_build_${depotId}.vdf`; |
| 98 | + |
| 99 | + const depotVdf = generateDepotVdf({ depotId, extraExclusions }); |
| 100 | + const appVdf = generateAppVdf({ appId, depotId, branch, description, depotVdfFileName: depotFileName }) |
| 101 | + // generateAppVdf's contentroot/buildoutput default to "./" - override to |
| 102 | + // the path the running steamcmd process will actually see (an absolute |
| 103 | + // host path for local mode, or the container mount point for docker). |
| 104 | + .replace('"contentroot" "./"', `"contentroot" "${contentRoot}"`) |
| 105 | + .replace('"buildoutput" "./"', `"buildoutput" "${contentRoot}"`); |
| 106 | + |
| 107 | + fs.writeFileSync(path.join(absoluteBuildPath, depotFileName), depotVdf, "utf8"); |
| 108 | + fs.writeFileSync(path.join(absoluteBuildPath, "manifest.vdf"), appVdf, "utf8"); |
| 109 | + |
| 110 | + console.log(`Deploying ${absoluteBuildPath} to Steam app ${appId}, depot ${depotId}, branch "${branch}"`); |
| 111 | + |
| 112 | + const runner = new SteamCmdRunner(); |
| 113 | + const result = await runner.run({ |
| 114 | + buildDir: absoluteBuildPath, |
| 115 | + username, |
| 116 | + password, |
| 117 | + mode, |
| 118 | + steamCmdPath: options.steamCmdPath, |
| 119 | + steamConfigDir: options.steamConfigDir, |
| 120 | + }); |
| 121 | + |
| 122 | + if (!result.success) { |
| 123 | + throw new Error(`Steam deployment failed: ${result.failureReason}`); |
| 124 | + } |
| 125 | + |
| 126 | + if (result.buildId) { |
| 127 | + console.log(`Steam deployment succeeded. BuildID: ${result.buildId}`); |
| 128 | + } else { |
| 129 | + console.log("Steam deployment succeeded (no BuildID found in output)."); |
| 130 | + } |
| 131 | + |
| 132 | + return true; |
| 133 | + } |
| 134 | +} |
0 commit comments