gcp-job-runner 1.6.1 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/cli.mjs +21 -1
- package/dist/cli.mjs.map +1 -1
- package/dist/cloud/deploy.mjs +6 -1
- package/dist/cloud/deploy.mjs.map +1 -1
- package/dist/cloud/hash.mjs +1 -1
- package/dist/config.d.mts +25 -0
- package/dist/config.mjs +14 -1
- package/dist/config.mjs.map +1 -1
- package/dist/discover-jobs.mjs +1 -1
- package/dist/env-file.mjs +1 -1
- package/dist/exports.d.mts +29 -0
- package/dist/exports.mjs +138 -0
- package/dist/exports.mjs.map +1 -0
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +2 -1
- package/dist/interactive.mjs +1 -1
- package/dist/run-job.mjs +1 -1
- package/package.json +2 -1
- package/src/cli.ts +35 -1
- package/src/cloud/deploy.ts +8 -0
- package/src/config.test.ts +66 -0
- package/src/config.ts +43 -0
- package/src/exports.integration.test.ts +76 -0
- package/src/exports.test.ts +240 -0
- package/src/exports.ts +238 -0
- package/src/index.ts +2 -0
package/README.md
CHANGED
|
@@ -44,6 +44,7 @@ Same code, same arguments, same secrets. The cloud command automatically builds
|
|
|
44
44
|
- **Cloud Run deployment** — no Terraform or manual GCP config needed, just `job cloud run`
|
|
45
45
|
- **Smart caching** — a single Docker image contains all jobs; running different jobs or different arguments doesn't rebuild
|
|
46
46
|
- **GCP Secret Manager** — secrets are loaded transparently for both local and cloud execution
|
|
47
|
+
- **Unified artifact output** — `getExportsWriter()` writes JSON/CSV/binary to a local directory during development and to a Cloud Storage bucket in production, with the same handler code
|
|
47
48
|
- **Multi-environment** — configure staging, production, etc. and switch with a single argument
|
|
48
49
|
|
|
49
50
|
## Install
|
package/dist/cli.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { resolveLocalExportsPath } from "./config.mjs";
|
|
2
3
|
import { discoverJobs } from "./discover-jobs.mjs";
|
|
3
4
|
import { promptForArgs, selectJob } from "./interactive.mjs";
|
|
4
5
|
import { runJob } from "./run-job.mjs";
|
|
@@ -7,8 +8,8 @@ import { createOrUpdateJob, deployIfChanged, prepareImage } from "./cloud/deploy
|
|
|
7
8
|
import { execute } from "./cloud/execute.mjs";
|
|
8
9
|
import { deriveJobResourceName } from "./cloud/job-name.mjs";
|
|
9
10
|
import { getSecrets } from "./secrets/loader.mjs";
|
|
10
|
-
import { consola } from "consola";
|
|
11
11
|
import path from "node:path";
|
|
12
|
+
import { consola } from "consola";
|
|
12
13
|
import process from "node:process";
|
|
13
14
|
import { execSync } from "node:child_process";
|
|
14
15
|
|
|
@@ -203,6 +204,13 @@ async function handleLocalRun(options) {
|
|
|
203
204
|
if (envConfig.env) for (const [key, value] of Object.entries(envConfig.env)) process.env[key] = value;
|
|
204
205
|
/** Project always takes highest precedence */
|
|
205
206
|
process.env.GOOGLE_CLOUD_PROJECT = envConfig.project;
|
|
207
|
+
/**
|
|
208
|
+
* `localExportsPath` wins over the environment's `exportsPath` for local
|
|
209
|
+
* runs so developers can use one destination regardless of whether they
|
|
210
|
+
* point at stag or prod data.
|
|
211
|
+
*/
|
|
212
|
+
const localExportsPath = resolveLocalExportsPath(config, envConfig, process.cwd());
|
|
213
|
+
if (localExportsPath !== void 0) process.env.JOB_EXPORTS_PATH = localExportsPath;
|
|
206
214
|
if (envConfig.secrets && envConfig.secrets.length > 0) {
|
|
207
215
|
const secrets = await getSecrets(envConfig.secrets);
|
|
208
216
|
for (const [key, value] of Object.entries(secrets)) process.env[key] = value;
|
|
@@ -252,6 +260,7 @@ async function handleCloudDeploy(options) {
|
|
|
252
260
|
consola.error("No cloud configuration found in runner config.\nAdd a `cloud` section to your job-runner.config.ts");
|
|
253
261
|
process.exit(1);
|
|
254
262
|
}
|
|
263
|
+
assertCloudExportsPath(envConfig);
|
|
255
264
|
const { imageUri } = await prepareImage({
|
|
256
265
|
cloud,
|
|
257
266
|
envConfig,
|
|
@@ -260,6 +269,16 @@ async function handleCloudDeploy(options) {
|
|
|
260
269
|
consola.info(`Image: ${imageUri}`);
|
|
261
270
|
consola.success("Deploy complete");
|
|
262
271
|
}
|
|
272
|
+
/**
|
|
273
|
+
* Reject local exportsPath values for cloud commands — they would silently
|
|
274
|
+
* write to a container filesystem that is discarded on task exit.
|
|
275
|
+
*/
|
|
276
|
+
function assertCloudExportsPath(envConfig) {
|
|
277
|
+
if (envConfig.exportsPath && !envConfig.exportsPath.startsWith("gs://")) {
|
|
278
|
+
consola.error(`exportsPath "${envConfig.exportsPath}" is a local path but this is a cloud command.\nUse a gs:// URI for cloud environments (e.g. gs://my-bucket/exports).`);
|
|
279
|
+
process.exit(1);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
263
282
|
async function handleCloudRun(options) {
|
|
264
283
|
const { config, envConfig, jobsDirectory, jobNameFromArgs, jobFlags, isInteractive, isAsync, tasks, parallelism } = options;
|
|
265
284
|
const cloud = config.cloud;
|
|
@@ -267,6 +286,7 @@ async function handleCloudRun(options) {
|
|
|
267
286
|
consola.error("No cloud configuration found in runner config.\nAdd a `cloud` section to your job-runner.config.ts");
|
|
268
287
|
process.exit(1);
|
|
269
288
|
}
|
|
289
|
+
assertCloudExportsPath(envConfig);
|
|
270
290
|
/** Override parallelism from CLI flag */
|
|
271
291
|
if (parallelism !== void 0) cloud.resources = {
|
|
272
292
|
...cloud.resources,
|
package/dist/cli.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.mjs","names":[],"sources":["../src/cli.ts"],"sourcesContent":["#!/usr/bin/env node\n\nimport { execSync } from \"node:child_process\";\nimport path from \"node:path\";\nimport process from \"node:process\";\nimport { consola } from \"consola\";\nimport type { ZodObject, ZodRawShape } from \"zod\";\nimport type { RunnerConfig } from \"./config\";\nimport {\n createOrUpdateJob,\n deployIfChanged,\n prepareImage,\n} from \"./cloud/deploy\";\nimport { execute } from \"./cloud/execute\";\nimport { deriveJobResourceName } from \"./cloud/job-name\";\nimport { discoverJobs } from \"./discover-jobs\";\nimport { promptForArgs, selectJob } from \"./interactive\";\nimport { runJob } from \"./run-job\";\nimport { getSecrets } from \"./secrets\";\nimport { parseEnvFiles } from \"./env-file\";\nimport type { JobFunction } from \"./types\";\n\nconst BIN_NAME = \"job\";\nconst CONFIG_FILE = \"job-runner.config.ts\";\nconst DEFAULT_BUILD_COMMAND = \"turbo build\";\nconst DEFAULT_JOBS_DIRECTORY = \"dist/jobs\";\n\nconst USAGE = `Usage: ${BIN_NAME} local run <env> <job-name> [options]\n ${BIN_NAME} cloud run <env> <job-name> [options]\n ${BIN_NAME} cloud deploy <env>\n ${BIN_NAME} --list\n\nCloud run options:\n --tasks <n> Number of parallel tasks for this execution\n --parallelism <n> Max concurrent tasks (sets job resource default)`;\n\nfunction validateInteger(value: number, flagName: string): number {\n if (Number.isNaN(value) || !Number.isInteger(value) || value < 0) {\n consola.error(\n `Invalid value for ${flagName}: expected a non-negative integer`,\n );\n process.exit(1);\n }\n return value;\n}\n\n/**\n * Extract a numeric flag value from args.\n * Supports both `--flag N` and `--flag=N` syntax.\n * Returns undefined if the flag is not present.\n */\nfunction extractNumberFlag(\n args: string[],\n flagName: string,\n): number | undefined {\n for (let i = 0; i < args.length; i++) {\n const arg = args[i]!;\n\n if (arg === flagName) {\n const next = args[i + 1];\n if (next === undefined || next.startsWith(\"-\")) {\n consola.error(`${flagName} requires a value`);\n process.exit(1);\n }\n return validateInteger(Number(next), flagName);\n }\n\n if (arg.startsWith(`${flagName}=`)) {\n return validateInteger(Number(arg.slice(flagName.length + 1)), flagName);\n }\n }\n\n return undefined;\n}\n\nasync function main(): Promise<void> {\n const args = process.argv.slice(2);\n\n /** Extract flags from anywhere in args */\n const noBuild = args.includes(\"--no-build\");\n const isInteractive = args.includes(\"--interactive\") || args.includes(\"-i\");\n const isAsync = args.includes(\"--async\");\n\n /** Determine mode early so cloud-only flags are only parsed in cloud mode */\n const mode = args.find((arg) => !arg.startsWith(\"-\"));\n const isCloudMode = mode === \"cloud\";\n\n const tasks = isCloudMode ? extractNumberFlag(args, \"--tasks\") : undefined;\n if (tasks === 0) {\n consola.error(\"--tasks must be at least 1\");\n process.exit(1);\n }\n const parallelism = isCloudMode\n ? extractNumberFlag(args, \"--parallelism\")\n : undefined;\n\n const configPath = path.resolve(process.cwd(), CONFIG_FILE);\n\n /** Load config directly (config should only depend on gcp-job-runner) */\n let config: RunnerConfig;\n try {\n const module = (await import(configPath)) as { default: RunnerConfig };\n config = module.default;\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n consola.error(\n `Failed to load runner config from ${configPath}\\n${message}`,\n );\n process.exit(1);\n }\n\n /** Resolve jobs directory with default */\n const jobsDirectory =\n config.jobsDirectory ?? path.resolve(process.cwd(), DEFAULT_JOBS_DIRECTORY);\n\n const envNames = Object.keys(config.environments);\n\n /** Handle --list: discover and print all available jobs */\n if (args.includes(\"--list\")) {\n if (!noBuild && config.buildCommand !== false) {\n const buildCommand = config.buildCommand ?? DEFAULT_BUILD_COMMAND;\n runBuild(buildCommand);\n }\n\n const jobs = await discoverJobs(jobsDirectory);\n if (jobs.length === 0) {\n consola.info(\"No jobs found.\");\n } else {\n consola.info(\"Available jobs:\");\n for (const job of jobs) {\n consola.log(` ${job.name}`);\n }\n }\n return;\n }\n\n /**\n * Parse positional arguments. In cloud mode, skip values consumed by\n * numeric flags like `--tasks 5` so that `5` is not treated as a positional.\n */\n const consumedIndices = new Set<number>();\n if (isCloudMode) {\n for (let i = 0; i < args.length; i++) {\n const arg = args[i]!;\n if (\n (arg === \"--tasks\" || arg === \"--parallelism\") &&\n i + 1 < args.length\n ) {\n consumedIndices.add(i + 1);\n }\n }\n }\n\n const positionals = args.filter(\n (arg, index) => !arg.startsWith(\"-\") && !consumedIndices.has(index),\n );\n\n if (mode !== \"local\" && mode !== \"cloud\") {\n consola.error(\n `Unknown or missing mode \"${mode ?? \"\"}\".\\n\\n` +\n `${USAGE}\\n\\n` +\n `Environments: ${envNames.join(\", \")}`,\n );\n process.exit(1);\n }\n\n const action = positionals[1];\n if (mode === \"cloud\" && action !== \"run\" && action !== \"deploy\") {\n consola.error(\n `Unknown or missing action \"${action ?? \"\"}\" for cloud mode.\\n\\n` + USAGE,\n );\n process.exit(1);\n }\n if (mode === \"local\" && action !== \"run\") {\n consola.error(\n `Unknown or missing action \"${action ?? \"\"}\" for local mode.\\n\\n` + USAGE,\n );\n process.exit(1);\n }\n\n const envName = positionals[2];\n if (!envName) {\n consola.error(\n `No environment specified.\\n\\n` +\n `${USAGE}\\n\\n` +\n `Environments: ${envNames.join(\", \")}`,\n );\n process.exit(1);\n }\n\n if (!envNames.includes(envName)) {\n consola.error(\n `Unknown environment \"${envName}\".\\n\\n` +\n `Available environments: ${envNames.join(\", \")}`,\n );\n process.exit(1);\n }\n\n const envConfig = config.environments[envName]!;\n\n /**\n * Remaining positionals after env become the job name/path. For example\n * `job cloud run stag test/countdown` -> positionals[3] = \"test/countdown\".\n */\n const jobNameFromArgs = positionals[3];\n\n /**\n * Collect job flags: everything after `<env>` in the original args that\n * isn't a consumed positional or a known global flag.\n */\n const consumedPositionals = new Set(\n [mode, action, envName, jobNameFromArgs].filter(Boolean),\n );\n const globalFlags = new Set([\n \"--no-build\",\n \"--interactive\",\n \"-i\",\n \"--async\",\n \"--list\",\n ]);\n\n /** --tasks and --parallelism are cloud-only; don't strip them in local mode */\n if (mode === \"cloud\") {\n globalFlags.add(\"--tasks\");\n globalFlags.add(\"--parallelism\");\n }\n\n const envIndex = args.indexOf(envName);\n const jobFlags = args.slice(envIndex + 1).filter((arg, index, arr) => {\n if (consumedPositionals.has(arg)) return false;\n if (globalFlags.has(arg)) return false;\n if (mode === \"cloud\") {\n /** Filter out `--flag=value` forms of number flags */\n if (arg.startsWith(\"--tasks=\") || arg.startsWith(\"--parallelism=\"))\n return false;\n /** Filter out values that follow --tasks or --parallelism */\n const previous = arr[index - 1];\n if (previous === \"--tasks\" || previous === \"--parallelism\") return false;\n }\n return true;\n });\n\n /** Build unless skipped */\n if (!noBuild && config.buildCommand !== false) {\n const buildCommand = config.buildCommand ?? DEFAULT_BUILD_COMMAND;\n runBuild(buildCommand);\n }\n\n if (mode === \"local\") {\n await handleLocalRun({\n config,\n envName,\n envConfig,\n jobsDirectory,\n jobNameFromArgs,\n jobFlags,\n isInteractive,\n });\n } else if (action === \"deploy\") {\n await handleCloudDeploy({ config, envConfig });\n } else {\n await handleCloudRun({\n config,\n envConfig,\n jobsDirectory,\n jobNameFromArgs,\n jobFlags,\n isInteractive,\n isAsync,\n tasks,\n parallelism,\n });\n }\n}\n\ninterface LocalRunOptions {\n config: RunnerConfig;\n envName: string;\n envConfig: RunnerConfig[\"environments\"][string];\n jobsDirectory: string;\n jobNameFromArgs: string | undefined;\n jobFlags: string[];\n isInteractive: boolean;\n}\n\nasync function handleLocalRun(options: LocalRunOptions): Promise<void> {\n const {\n config,\n envName,\n envConfig,\n jobsDirectory,\n jobNameFromArgs,\n jobFlags,\n isInteractive,\n } = options;\n\n /** Set environment variables for local execution */\n process.env.NODE_ENV ??= \"development\";\n process.env.USE_CONSOLE_LOG ??= \"true\";\n process.env.LOG_COLORIZE ??= \"true\";\n\n /** Load envFile variables (don't overwrite existing process.env values) */\n const envFileVars = parseEnvFiles(envConfig.envFile);\n for (const [key, value] of Object.entries(envFileVars)) {\n process.env[key] ??= value;\n }\n\n /** Explicit env values override envFile values */\n if (envConfig.env) {\n for (const [key, value] of Object.entries(envConfig.env)) {\n process.env[key] = value;\n }\n }\n\n /** Project always takes highest precedence */\n process.env.GOOGLE_CLOUD_PROJECT = envConfig.project;\n\n if (envConfig.secrets && envConfig.secrets.length > 0) {\n const secrets = await getSecrets(envConfig.secrets);\n for (const [key, value] of Object.entries(secrets)) {\n process.env[key] = value;\n }\n }\n\n if (isInteractive) {\n const { jobArgv } = await resolveInteractiveJob(jobsDirectory);\n\n process.argv = [process.argv[0]!, process.argv[1]!, ...jobArgv];\n\n const commandPrefix = `${BIN_NAME} local run ${envName}`;\n\n await runJob({\n jobsDirectory,\n initialize: config.initialize,\n logger: config.logger,\n commandPrefix,\n });\n return;\n }\n\n if (!jobNameFromArgs) {\n consola.error(\n `No job name specified.\\n\\n` +\n `Usage: ${BIN_NAME} local run ${envName} <job-name> [options]\\n` +\n ` ${BIN_NAME} local run ${envName} -i`,\n );\n process.exit(1);\n }\n\n /**\n * Rewrite process.argv so runJob sees the job name as the first positional\n * argument, followed by any job-specific flags.\n */\n process.argv = [\n process.argv[0]!,\n process.argv[1]!,\n jobNameFromArgs,\n ...jobFlags,\n ];\n\n const commandPrefix = `${BIN_NAME} local run ${envName}`;\n\n await runJob({\n jobsDirectory,\n initialize: config.initialize,\n logger: config.logger,\n commandPrefix,\n });\n}\n\ninterface CloudDeployOptions {\n config: RunnerConfig;\n envConfig: RunnerConfig[\"environments\"][string];\n}\n\nasync function handleCloudDeploy(options: CloudDeployOptions): Promise<void> {\n const { config, envConfig } = options;\n const cloud = config.cloud;\n\n if (!cloud) {\n consola.error(\n \"No cloud configuration found in runner config.\\n\" +\n \"Add a `cloud` section to your job-runner.config.ts\",\n );\n process.exit(1);\n }\n\n const serviceDirectory = process.cwd();\n\n const { imageUri } = await prepareImage({\n cloud,\n envConfig,\n serviceDirectory,\n });\n\n consola.info(`Image: ${imageUri}`);\n consola.success(\"Deploy complete\");\n}\n\ninterface CloudRunOptions {\n config: RunnerConfig;\n envConfig: RunnerConfig[\"environments\"][string];\n jobsDirectory: string;\n jobNameFromArgs: string | undefined;\n jobFlags: string[];\n isInteractive: boolean;\n isAsync: boolean;\n tasks?: number;\n parallelism?: number;\n}\n\nasync function handleCloudRun(options: CloudRunOptions): Promise<void> {\n const {\n config,\n envConfig,\n jobsDirectory,\n jobNameFromArgs,\n jobFlags,\n isInteractive,\n isAsync,\n tasks,\n parallelism,\n } = options;\n\n const cloud = config.cloud;\n\n if (!cloud) {\n consola.error(\n \"No cloud configuration found in runner config.\\n\" +\n \"Add a `cloud` section to your job-runner.config.ts\",\n );\n process.exit(1);\n }\n\n /** Override parallelism from CLI flag */\n if (parallelism !== undefined) {\n cloud.resources = { ...cloud.resources, parallelism };\n }\n\n const serviceDirectory = process.cwd();\n\n /** Determine job name and argv */\n let jobArgv: string[];\n\n if (isInteractive) {\n const result = await resolveInteractiveJob(jobsDirectory);\n jobArgv = result.jobArgv;\n } else {\n if (!jobNameFromArgs) {\n consola.error(\n `No job name specified.\\n\\n` +\n `Usage: ${BIN_NAME} cloud run <env> <job-name> [options]\\n` +\n ` ${BIN_NAME} cloud run <env> -i`,\n );\n process.exit(1);\n }\n jobArgv = [jobNameFromArgs, ...jobFlags];\n }\n\n /** Build and push image if changed */\n const { imageUri } = await deployIfChanged({\n cloud,\n envConfig,\n serviceDirectory,\n });\n\n consola.info(`Image: ${imageUri}`);\n\n /** Derive per-script Cloud Run Job name */\n const jobScript = jobArgv[0]!;\n const jobResourceName = deriveJobResourceName(jobScript);\n const region = cloud.region ?? \"us-central1\";\n\n /** Ensure per-script Cloud Run Job exists with current image */\n await createOrUpdateJob({\n cloud,\n envConfig,\n jobName: jobResourceName,\n imageUri,\n region,\n project: envConfig.project,\n });\n\n /** Execute the per-script Cloud Run Job */\n await execute({\n jobResourceName,\n region,\n project: envConfig.project,\n jobArgv,\n async: isAsync,\n tasks,\n });\n}\n\n/**\n * Interactively select a job and prompt for arguments.\n * Returns the job name and the complete jobArgv array.\n */\nasync function resolveInteractiveJob(\n jobsDirectory: string,\n): Promise<{ jobName: string; jobArgv: string[] }> {\n const jobName = await selectJob(jobsDirectory);\n\n consola.info(`Selected job: ${jobName}`);\n\n /**\n * Set console-friendly env vars before importing the module, since\n * importing may initialize a structured logger like pino.\n */\n process.env.USE_CONSOLE_LOG ??= \"true\";\n process.env.LOG_COLORIZE ??= \"true\";\n\n const parts = jobName.split(\"/\");\n const fileName = parts.pop() ?? \"\";\n const subDirectories = parts;\n const fileLocation = path.join(jobsDirectory, ...subDirectories);\n const modulePath = path.resolve(fileLocation, `${fileName}.mjs`);\n\n let schema: ZodObject<ZodRawShape> | undefined;\n try {\n const moduleObject = (await import(modulePath)) as Record<string, unknown>;\n const fn = moduleObject.default as JobFunction | undefined;\n schema = fn?.__metadata?.schema;\n } catch {\n /** Module might not exist yet or have errors - proceed without schema */\n }\n\n /** Prompt for arguments if schema exists */\n let args: Record<string, unknown> = {};\n if (schema && Object.keys(schema.shape).length > 0) {\n consola.info(\"Enter arguments for the job:\");\n args = await promptForArgs(schema);\n }\n\n const jobArgv = [jobName];\n if (Object.keys(args).length > 0) {\n jobArgv.push(\"--args\", JSON.stringify(args));\n }\n\n return { jobName, jobArgv };\n}\n\n/**\n * Run the build command to compile workspace dependencies.\n * Shows a spinner and hides output unless the build fails.\n */\nfunction runBuild(command: string): void {\n consola.start(\"Building jobs source code...\");\n\n try {\n execSync(command, {\n stdio: \"pipe\",\n encoding: \"utf-8\",\n });\n consola.success(\"Build complete\");\n } catch (error) {\n consola.fail(\"Build failed\");\n\n /** Show the build output on failure */\n if (error && typeof error === \"object\" && \"stdout\" in error) {\n const stdout = (error as { stdout?: string }).stdout;\n if (stdout) {\n consola.log(stdout);\n }\n }\n if (error && typeof error === \"object\" && \"stderr\" in error) {\n const stderr = (error as { stderr?: string }).stderr;\n if (stderr) {\n consola.log(stderr);\n }\n }\n\n process.exit(1);\n }\n}\n\nawait main();\n"],"mappings":";;;;;;;;;;;;;;;AAsBA,MAAM,WAAW;AACjB,MAAM,cAAc;AACpB,MAAM,wBAAwB;AAC9B,MAAM,yBAAyB;AAE/B,MAAM,QAAQ,UAAU,SAAS;SACxB,SAAS;SACT,SAAS;SACT,SAAS;;;;;AAMlB,SAAS,gBAAgB,OAAe,UAA0B;AAChE,KAAI,OAAO,MAAM,MAAM,IAAI,CAAC,OAAO,UAAU,MAAM,IAAI,QAAQ,GAAG;AAChE,UAAQ,MACN,qBAAqB,SAAS,mCAC/B;AACD,UAAQ,KAAK,EAAE;;AAEjB,QAAO;;;;;;;AAQ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AM,IAAI;CAChC,MAAM,WAAW,MAAM,KAAK,IAAI;CAChC,MAAM,iBAAiB;CACvB,MAAM,eAAe,KAAK,KAAK,eAAe,GAAG,eAAe;CAChE,MAAM,aAAa,KAAK,QAAQ,cAAc,GAAG,SAAS,MAAM;CAEhE,IAAI;AACJ,KAAI;AAGF,YAFsB,MAAM,OAAO,aACX,SACX,YAAY;SACnB;;CAKR,IAAI,OAAgC,EAAE;AACtC,KAAI,UAAU,OAAO,KAAK,OAAO,MAAM,CAAC,SAAS,GAAG;AAClD,UAAQ,KAAK,+BAA+B;AAC5C,SAAO,MAAM,cAAc,OAAO;;CAGpC,MAAM,UAAU,CAAC,QAAQ;AACzB,KAAI,OAAO,KAAK,KAAK,CAAC,SAAS,EAC7B,SAAQ,KAAK,UAAU,KAAK,UAAU,KAAK,CAAC;AAG9C,QAAO;EAAE;EAAS;EAAS;;;;;;AAO7B,SAAS,SAAS,SAAuB;AACvC,SAAQ,MAAM,+BAA+B;AAE7C,KAAI;AACF,WAAS,SAAS;GAChB,OAAO;GACP,UAAU;GACX,CAAC;AACF,UAAQ,QAAQ,iBAAiB;UAC1B,OAAO;AACd,UAAQ,KAAK,eAAe;;AAG5B,MAAI,SAAS,OAAO,UAAU,YAAY,YAAY,OAAO;GAC3D,MAAM,SAAU,MAA8B;AAC9C,OAAI,OACF,SAAQ,IAAI,OAAO;;AAGvB,MAAI,SAAS,OAAO,UAAU,YAAY,YAAY,OAAO;GAC3D,MAAM,SAAU,MAA8B;AAC9C,OAAI,OACF,SAAQ,IAAI,OAAO;;AAIvB,UAAQ,KAAK,EAAE;;;AAInB,MAAM,MAAM"}
|
|
1
|
+
{"version":3,"file":"cli.mjs","names":[],"sources":["../src/cli.ts"],"sourcesContent":["#!/usr/bin/env node\n\nimport { execSync } from \"node:child_process\";\nimport path from \"node:path\";\nimport process from \"node:process\";\nimport { consola } from \"consola\";\nimport type { ZodObject, ZodRawShape } from \"zod\";\nimport { resolveLocalExportsPath, type RunnerConfig } from \"./config\";\nimport {\n createOrUpdateJob,\n deployIfChanged,\n prepareImage,\n} from \"./cloud/deploy\";\nimport { execute } from \"./cloud/execute\";\nimport { deriveJobResourceName } from \"./cloud/job-name\";\nimport { discoverJobs } from \"./discover-jobs\";\nimport { promptForArgs, selectJob } from \"./interactive\";\nimport { runJob } from \"./run-job\";\nimport { getSecrets } from \"./secrets\";\nimport { parseEnvFiles } from \"./env-file\";\nimport type { JobFunction } from \"./types\";\n\nconst BIN_NAME = \"job\";\nconst CONFIG_FILE = \"job-runner.config.ts\";\nconst DEFAULT_BUILD_COMMAND = \"turbo build\";\nconst DEFAULT_JOBS_DIRECTORY = \"dist/jobs\";\n\nconst USAGE = `Usage: ${BIN_NAME} local run <env> <job-name> [options]\n ${BIN_NAME} cloud run <env> <job-name> [options]\n ${BIN_NAME} cloud deploy <env>\n ${BIN_NAME} --list\n\nCloud run options:\n --tasks <n> Number of parallel tasks for this execution\n --parallelism <n> Max concurrent tasks (sets job resource default)`;\n\nfunction validateInteger(value: number, flagName: string): number {\n if (Number.isNaN(value) || !Number.isInteger(value) || value < 0) {\n consola.error(\n `Invalid value for ${flagName}: expected a non-negative integer`,\n );\n process.exit(1);\n }\n return value;\n}\n\n/**\n * Extract a numeric flag value from args.\n * Supports both `--flag N` and `--flag=N` syntax.\n * Returns undefined if the flag is not present.\n */\nfunction extractNumberFlag(\n args: string[],\n flagName: string,\n): number | undefined {\n for (let i = 0; i < args.length; i++) {\n const arg = args[i]!;\n\n if (arg === flagName) {\n const next = args[i + 1];\n if (next === undefined || next.startsWith(\"-\")) {\n consola.error(`${flagName} requires a value`);\n process.exit(1);\n }\n return validateInteger(Number(next), flagName);\n }\n\n if (arg.startsWith(`${flagName}=`)) {\n return validateInteger(Number(arg.slice(flagName.length + 1)), flagName);\n }\n }\n\n return undefined;\n}\n\nasync function main(): Promise<void> {\n const args = process.argv.slice(2);\n\n /** Extract flags from anywhere in args */\n const noBuild = args.includes(\"--no-build\");\n const isInteractive = args.includes(\"--interactive\") || args.includes(\"-i\");\n const isAsync = args.includes(\"--async\");\n\n /** Determine mode early so cloud-only flags are only parsed in cloud mode */\n const mode = args.find((arg) => !arg.startsWith(\"-\"));\n const isCloudMode = mode === \"cloud\";\n\n const tasks = isCloudMode ? extractNumberFlag(args, \"--tasks\") : undefined;\n if (tasks === 0) {\n consola.error(\"--tasks must be at least 1\");\n process.exit(1);\n }\n const parallelism = isCloudMode\n ? extractNumberFlag(args, \"--parallelism\")\n : undefined;\n\n const configPath = path.resolve(process.cwd(), CONFIG_FILE);\n\n /** Load config directly (config should only depend on gcp-job-runner) */\n let config: RunnerConfig;\n try {\n const module = (await import(configPath)) as { default: RunnerConfig };\n config = module.default;\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n consola.error(\n `Failed to load runner config from ${configPath}\\n${message}`,\n );\n process.exit(1);\n }\n\n /** Resolve jobs directory with default */\n const jobsDirectory =\n config.jobsDirectory ?? path.resolve(process.cwd(), DEFAULT_JOBS_DIRECTORY);\n\n const envNames = Object.keys(config.environments);\n\n /** Handle --list: discover and print all available jobs */\n if (args.includes(\"--list\")) {\n if (!noBuild && config.buildCommand !== false) {\n const buildCommand = config.buildCommand ?? DEFAULT_BUILD_COMMAND;\n runBuild(buildCommand);\n }\n\n const jobs = await discoverJobs(jobsDirectory);\n if (jobs.length === 0) {\n consola.info(\"No jobs found.\");\n } else {\n consola.info(\"Available jobs:\");\n for (const job of jobs) {\n consola.log(` ${job.name}`);\n }\n }\n return;\n }\n\n /**\n * Parse positional arguments. In cloud mode, skip values consumed by\n * numeric flags like `--tasks 5` so that `5` is not treated as a positional.\n */\n const consumedIndices = new Set<number>();\n if (isCloudMode) {\n for (let i = 0; i < args.length; i++) {\n const arg = args[i]!;\n if (\n (arg === \"--tasks\" || arg === \"--parallelism\") &&\n i + 1 < args.length\n ) {\n consumedIndices.add(i + 1);\n }\n }\n }\n\n const positionals = args.filter(\n (arg, index) => !arg.startsWith(\"-\") && !consumedIndices.has(index),\n );\n\n if (mode !== \"local\" && mode !== \"cloud\") {\n consola.error(\n `Unknown or missing mode \"${mode ?? \"\"}\".\\n\\n` +\n `${USAGE}\\n\\n` +\n `Environments: ${envNames.join(\", \")}`,\n );\n process.exit(1);\n }\n\n const action = positionals[1];\n if (mode === \"cloud\" && action !== \"run\" && action !== \"deploy\") {\n consola.error(\n `Unknown or missing action \"${action ?? \"\"}\" for cloud mode.\\n\\n` + USAGE,\n );\n process.exit(1);\n }\n if (mode === \"local\" && action !== \"run\") {\n consola.error(\n `Unknown or missing action \"${action ?? \"\"}\" for local mode.\\n\\n` + USAGE,\n );\n process.exit(1);\n }\n\n const envName = positionals[2];\n if (!envName) {\n consola.error(\n `No environment specified.\\n\\n` +\n `${USAGE}\\n\\n` +\n `Environments: ${envNames.join(\", \")}`,\n );\n process.exit(1);\n }\n\n if (!envNames.includes(envName)) {\n consola.error(\n `Unknown environment \"${envName}\".\\n\\n` +\n `Available environments: ${envNames.join(\", \")}`,\n );\n process.exit(1);\n }\n\n const envConfig = config.environments[envName]!;\n\n /**\n * Remaining positionals after env become the job name/path. For example\n * `job cloud run stag test/countdown` -> positionals[3] = \"test/countdown\".\n */\n const jobNameFromArgs = positionals[3];\n\n /**\n * Collect job flags: everything after `<env>` in the original args that\n * isn't a consumed positional or a known global flag.\n */\n const consumedPositionals = new Set(\n [mode, action, envName, jobNameFromArgs].filter(Boolean),\n );\n const globalFlags = new Set([\n \"--no-build\",\n \"--interactive\",\n \"-i\",\n \"--async\",\n \"--list\",\n ]);\n\n /** --tasks and --parallelism are cloud-only; don't strip them in local mode */\n if (mode === \"cloud\") {\n globalFlags.add(\"--tasks\");\n globalFlags.add(\"--parallelism\");\n }\n\n const envIndex = args.indexOf(envName);\n const jobFlags = args.slice(envIndex + 1).filter((arg, index, arr) => {\n if (consumedPositionals.has(arg)) return false;\n if (globalFlags.has(arg)) return false;\n if (mode === \"cloud\") {\n /** Filter out `--flag=value` forms of number flags */\n if (arg.startsWith(\"--tasks=\") || arg.startsWith(\"--parallelism=\"))\n return false;\n /** Filter out values that follow --tasks or --parallelism */\n const previous = arr[index - 1];\n if (previous === \"--tasks\" || previous === \"--parallelism\") return false;\n }\n return true;\n });\n\n /** Build unless skipped */\n if (!noBuild && config.buildCommand !== false) {\n const buildCommand = config.buildCommand ?? DEFAULT_BUILD_COMMAND;\n runBuild(buildCommand);\n }\n\n if (mode === \"local\") {\n await handleLocalRun({\n config,\n envName,\n envConfig,\n jobsDirectory,\n jobNameFromArgs,\n jobFlags,\n isInteractive,\n });\n } else if (action === \"deploy\") {\n await handleCloudDeploy({ config, envConfig });\n } else {\n await handleCloudRun({\n config,\n envConfig,\n jobsDirectory,\n jobNameFromArgs,\n jobFlags,\n isInteractive,\n isAsync,\n tasks,\n parallelism,\n });\n }\n}\n\ninterface LocalRunOptions {\n config: RunnerConfig;\n envName: string;\n envConfig: RunnerConfig[\"environments\"][string];\n jobsDirectory: string;\n jobNameFromArgs: string | undefined;\n jobFlags: string[];\n isInteractive: boolean;\n}\n\nasync function handleLocalRun(options: LocalRunOptions): Promise<void> {\n const {\n config,\n envName,\n envConfig,\n jobsDirectory,\n jobNameFromArgs,\n jobFlags,\n isInteractive,\n } = options;\n\n /** Set environment variables for local execution */\n process.env.NODE_ENV ??= \"development\";\n process.env.USE_CONSOLE_LOG ??= \"true\";\n process.env.LOG_COLORIZE ??= \"true\";\n\n /** Load envFile variables (don't overwrite existing process.env values) */\n const envFileVars = parseEnvFiles(envConfig.envFile);\n for (const [key, value] of Object.entries(envFileVars)) {\n process.env[key] ??= value;\n }\n\n /** Explicit env values override envFile values */\n if (envConfig.env) {\n for (const [key, value] of Object.entries(envConfig.env)) {\n process.env[key] = value;\n }\n }\n\n /** Project always takes highest precedence */\n process.env.GOOGLE_CLOUD_PROJECT = envConfig.project;\n\n /**\n * `localExportsPath` wins over the environment's `exportsPath` for local\n * runs so developers can use one destination regardless of whether they\n * point at stag or prod data.\n */\n const localExportsPath = resolveLocalExportsPath(\n config,\n envConfig,\n process.cwd(),\n );\n if (localExportsPath !== undefined) {\n process.env.JOB_EXPORTS_PATH = localExportsPath;\n }\n\n if (envConfig.secrets && envConfig.secrets.length > 0) {\n const secrets = await getSecrets(envConfig.secrets);\n for (const [key, value] of Object.entries(secrets)) {\n process.env[key] = value;\n }\n }\n\n if (isInteractive) {\n const { jobArgv } = await resolveInteractiveJob(jobsDirectory);\n\n process.argv = [process.argv[0]!, process.argv[1]!, ...jobArgv];\n\n const commandPrefix = `${BIN_NAME} local run ${envName}`;\n\n await runJob({\n jobsDirectory,\n initialize: config.initialize,\n logger: config.logger,\n commandPrefix,\n });\n return;\n }\n\n if (!jobNameFromArgs) {\n consola.error(\n `No job name specified.\\n\\n` +\n `Usage: ${BIN_NAME} local run ${envName} <job-name> [options]\\n` +\n ` ${BIN_NAME} local run ${envName} -i`,\n );\n process.exit(1);\n }\n\n /**\n * Rewrite process.argv so runJob sees the job name as the first positional\n * argument, followed by any job-specific flags.\n */\n process.argv = [\n process.argv[0]!,\n process.argv[1]!,\n jobNameFromArgs,\n ...jobFlags,\n ];\n\n const commandPrefix = `${BIN_NAME} local run ${envName}`;\n\n await runJob({\n jobsDirectory,\n initialize: config.initialize,\n logger: config.logger,\n commandPrefix,\n });\n}\n\ninterface CloudDeployOptions {\n config: RunnerConfig;\n envConfig: RunnerConfig[\"environments\"][string];\n}\n\nasync function handleCloudDeploy(options: CloudDeployOptions): Promise<void> {\n const { config, envConfig } = options;\n const cloud = config.cloud;\n\n if (!cloud) {\n consola.error(\n \"No cloud configuration found in runner config.\\n\" +\n \"Add a `cloud` section to your job-runner.config.ts\",\n );\n process.exit(1);\n }\n\n assertCloudExportsPath(envConfig);\n\n const serviceDirectory = process.cwd();\n\n const { imageUri } = await prepareImage({\n cloud,\n envConfig,\n serviceDirectory,\n });\n\n consola.info(`Image: ${imageUri}`);\n consola.success(\"Deploy complete\");\n}\n\n/**\n * Reject local exportsPath values for cloud commands — they would silently\n * write to a container filesystem that is discarded on task exit.\n */\nfunction assertCloudExportsPath(\n envConfig: RunnerConfig[\"environments\"][string],\n): void {\n if (envConfig.exportsPath && !envConfig.exportsPath.startsWith(\"gs://\")) {\n consola.error(\n `exportsPath \"${envConfig.exportsPath}\" is a local path but this is a cloud command.\\n` +\n \"Use a gs:// URI for cloud environments (e.g. gs://my-bucket/exports).\",\n );\n process.exit(1);\n }\n}\n\ninterface CloudRunOptions {\n config: RunnerConfig;\n envConfig: RunnerConfig[\"environments\"][string];\n jobsDirectory: string;\n jobNameFromArgs: string | undefined;\n jobFlags: string[];\n isInteractive: boolean;\n isAsync: boolean;\n tasks?: number;\n parallelism?: number;\n}\n\nasync function handleCloudRun(options: CloudRunOptions): Promise<void> {\n const {\n config,\n envConfig,\n jobsDirectory,\n jobNameFromArgs,\n jobFlags,\n isInteractive,\n isAsync,\n tasks,\n parallelism,\n } = options;\n\n const cloud = config.cloud;\n\n if (!cloud) {\n consola.error(\n \"No cloud configuration found in runner config.\\n\" +\n \"Add a `cloud` section to your job-runner.config.ts\",\n );\n process.exit(1);\n }\n\n assertCloudExportsPath(envConfig);\n\n /** Override parallelism from CLI flag */\n if (parallelism !== undefined) {\n cloud.resources = { ...cloud.resources, parallelism };\n }\n\n const serviceDirectory = process.cwd();\n\n /** Determine job name and argv */\n let jobArgv: string[];\n\n if (isInteractive) {\n const result = await resolveInteractiveJob(jobsDirectory);\n jobArgv = result.jobArgv;\n } else {\n if (!jobNameFromArgs) {\n consola.error(\n `No job name specified.\\n\\n` +\n `Usage: ${BIN_NAME} cloud run <env> <job-name> [options]\\n` +\n ` ${BIN_NAME} cloud run <env> -i`,\n );\n process.exit(1);\n }\n jobArgv = [jobNameFromArgs, ...jobFlags];\n }\n\n /** Build and push image if changed */\n const { imageUri } = await deployIfChanged({\n cloud,\n envConfig,\n serviceDirectory,\n });\n\n consola.info(`Image: ${imageUri}`);\n\n /** Derive per-script Cloud Run Job name */\n const jobScript = jobArgv[0]!;\n const jobResourceName = deriveJobResourceName(jobScript);\n const region = cloud.region ?? \"us-central1\";\n\n /** Ensure per-script Cloud Run Job exists with current image */\n await createOrUpdateJob({\n cloud,\n envConfig,\n jobName: jobResourceName,\n imageUri,\n region,\n project: envConfig.project,\n });\n\n /** Execute the per-script Cloud Run Job */\n await execute({\n jobResourceName,\n region,\n project: envConfig.project,\n jobArgv,\n async: isAsync,\n tasks,\n });\n}\n\n/**\n * Interactively select a job and prompt for arguments.\n * Returns the job name and the complete jobArgv array.\n */\nasync function resolveInteractiveJob(\n jobsDirectory: string,\n): Promise<{ jobName: string; jobArgv: string[] }> {\n const jobName = await selectJob(jobsDirectory);\n\n consola.info(`Selected job: ${jobName}`);\n\n /**\n * Set console-friendly env vars before importing the module, since\n * importing may initialize a structured logger like pino.\n */\n process.env.USE_CONSOLE_LOG ??= \"true\";\n process.env.LOG_COLORIZE ??= \"true\";\n\n const parts = jobName.split(\"/\");\n const fileName = parts.pop() ?? \"\";\n const subDirectories = parts;\n const fileLocation = path.join(jobsDirectory, ...subDirectories);\n const modulePath = path.resolve(fileLocation, `${fileName}.mjs`);\n\n let schema: ZodObject<ZodRawShape> | undefined;\n try {\n const moduleObject = (await import(modulePath)) as Record<string, unknown>;\n const fn = moduleObject.default as JobFunction | undefined;\n schema = fn?.__metadata?.schema;\n } catch {\n /** Module might not exist yet or have errors - proceed without schema */\n }\n\n /** Prompt for arguments if schema exists */\n let args: Record<string, unknown> = {};\n if (schema && Object.keys(schema.shape).length > 0) {\n consola.info(\"Enter arguments for the job:\");\n args = await promptForArgs(schema);\n }\n\n const jobArgv = [jobName];\n if (Object.keys(args).length > 0) {\n jobArgv.push(\"--args\", JSON.stringify(args));\n }\n\n return { jobName, jobArgv };\n}\n\n/**\n * Run the build command to compile workspace dependencies.\n * Shows a spinner and hides output unless the build fails.\n */\nfunction runBuild(command: string): void {\n consola.start(\"Building jobs source code...\");\n\n try {\n execSync(command, {\n stdio: \"pipe\",\n encoding: \"utf-8\",\n });\n consola.success(\"Build complete\");\n } catch (error) {\n consola.fail(\"Build failed\");\n\n /** Show the build output on failure */\n if (error && typeof error === \"object\" && \"stdout\" in error) {\n const stdout = (error as { stdout?: string }).stdout;\n if (stdout) {\n consola.log(stdout);\n }\n }\n if (error && typeof error === \"object\" && \"stderr\" in error) {\n const stderr = (error as { stderr?: string }).stderr;\n if (stderr) {\n consola.log(stderr);\n }\n }\n\n process.exit(1);\n }\n}\n\nawait main();\n"],"mappings":";;;;;;;;;;;;;;;;AAsBA,MAAM,WAAW;AACjB,MAAM,cAAc;AACpB,MAAM,wBAAwB;AAC9B,MAAM,yBAAyB;AAE/B,MAAM,QAAQ,UAAU,SAAS;SACxB,SAAS;SACT,SAAS;SACT,SAAS;;;;;AAMlB,SAAS,gBAAgB,OAAe,UAA0B;AAChE,KAAI,OAAO,MAAM,MAAM,IAAI,CAAC,OAAO,UAAU,MAAM,IAAI,QAAQ,GAAG;AAChE,UAAQ,MACN,qBAAqB,SAAS,mCAC/B;AACD,UAAQ,KAAK,EAAE;;AAEjB,QAAO;;;;;;;AAQT,SAAS,kBACP,MACA,UACoB;AACpB,MAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;EACpC,MAAM,MAAM,KAAK;AAEjB,MAAI,QAAQ,UAAU;GACpB,MAAM,OAAO,KAAK,IAAI;AACtB,OAAI,SAAS,UAAa,KAAK,WAAW,IAAI,EAAE;AAC9C,YAAQ,MAAM,GAAG,SAAS,mBAAmB;AAC7C,YAAQ,KAAK,EAAE;;AAEjB,UAAO,gBAAgB,OAAO,KAAK,EAAE,SAAS;;AAGhD,MAAI,IAAI,WAAW,GAAG,SAAS,GAAG,CAChC,QAAO,gBAAgB,OAAO,IAAI,MAAM,SAAS,SAAS,EAAE,CAAC,EAAE,SAAS;;;AAO9E,eAAe,OAAsB;CACnC,MAAM,OAAO,QAAQ,KAAK,MAAM,EAAE;;CAGlC,MAAM,UAAU,KAAK,SAAS,aAAa;CAC3C,MAAM,gBAAgB,KAAK,SAAS,gBAAgB,IAAI,KAAK,SAAS,KA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KAAK,OAAO,MAAM,CAAC,SAAS,GAAG;AAClD,UAAQ,KAAK,+BAA+B;AAC5C,SAAO,MAAM,cAAc,OAAO;;CAGpC,MAAM,UAAU,CAAC,QAAQ;AACzB,KAAI,OAAO,KAAK,KAAK,CAAC,SAAS,EAC7B,SAAQ,KAAK,UAAU,KAAK,UAAU,KAAK,CAAC;AAG9C,QAAO;EAAE;EAAS;EAAS;;;;;;AAO7B,SAAS,SAAS,SAAuB;AACvC,SAAQ,MAAM,+BAA+B;AAE7C,KAAI;AACF,WAAS,SAAS;GAChB,OAAO;GACP,UAAU;GACX,CAAC;AACF,UAAQ,QAAQ,iBAAiB;UAC1B,OAAO;AACd,UAAQ,KAAK,eAAe;;AAG5B,MAAI,SAAS,OAAO,UAAU,YAAY,YAAY,OAAO;GAC3D,MAAM,SAAU,MAA8B;AAC9C,OAAI,OACF,SAAQ,IAAI,OAAO;;AAGvB,MAAI,SAAS,OAAO,UAAU,YAAY,YAAY,OAAO;GAC3D,MAAM,SAAU,MAA8B;AAC9C,OAAI,OACF,SAAQ,IAAI,OAAO;;AAIvB,UAAQ,KAAK,EAAE;;;AAInB,MAAM,MAAM"}
|
package/dist/cloud/deploy.mjs
CHANGED
|
@@ -2,8 +2,8 @@ import { parseEnvFiles } from "../env-file.mjs";
|
|
|
2
2
|
import { generateDockerfile } from "./dockerfile.mjs";
|
|
3
3
|
import { checkGcloudAvailable, gcloudExecCapture, gcloudJson, isDockerDaemonRunning, isDockerInstalled, shellExecCapture, startDockerDaemon, waitForDockerDaemon } from "./gcloud.mjs";
|
|
4
4
|
import { hashDirectory } from "./hash.mjs";
|
|
5
|
-
import { consola } from "consola";
|
|
6
5
|
import path from "node:path";
|
|
6
|
+
import { consola } from "consola";
|
|
7
7
|
import { existsSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
8
8
|
|
|
9
9
|
//#region src/cloud/deploy.ts
|
|
@@ -211,6 +211,11 @@ async function createOrUpdateJob(options) {
|
|
|
211
211
|
...envConfig.env,
|
|
212
212
|
GOOGLE_CLOUD_PROJECT: project
|
|
213
213
|
};
|
|
214
|
+
/**
|
|
215
|
+
* Forward the exports destination to the container. Local paths are
|
|
216
|
+
* rejected earlier by assertCloudExportsPath() in the CLI handlers.
|
|
217
|
+
*/
|
|
218
|
+
if (envConfig.exportsPath) envVars.JOB_EXPORTS_PATH = envConfig.exportsPath;
|
|
214
219
|
const envVarsString = Object.entries(envVars).map(([key, value]) => `${key}=${value}`).join(",");
|
|
215
220
|
const secretsString = (envConfig.secrets ?? []).map((name) => `${name}=${name}:latest`).join(",");
|
|
216
221
|
if (existingJob) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deploy.mjs","names":[],"sources":["../../src/cloud/deploy.ts"],"sourcesContent":["import { existsSync, readFileSync, writeFileSync, unlinkSync } from \"node:fs\";\nimport path from \"node:path\";\nimport { consola } from \"consola\";\nimport type { CloudConfig, RunnerEnvOptions } from \"../config\";\nimport { parseEnvFiles } from \"../env-file\";\nimport { generateDockerfile } from \"./dockerfile\";\nimport {\n checkGcloudAvailable,\n isDockerInstalled,\n isDockerDaemonRunning,\n startDockerDaemon,\n waitForDockerDaemon,\n gcloudExecCapture,\n gcloudJson,\n shellExecCapture,\n} from \"./gcloud\";\nimport { hashDirectory } from \"./hash\";\n\nexport interface DeployOptions {\n /** Cloud configuration from the runner config */\n cloud: CloudConfig;\n /** Environment configuration (project, env vars, secrets) */\n envConfig: RunnerEnvOptions;\n /** Working directory (service root where isolate.config.json lives) */\n serviceDirectory: string;\n}\n\nexport interface DeployResult {\n /** Full image URI including tag */\n imageUri: string;\n /** Whether a new image was built */\n imageBuilt: boolean;\n}\n\n/**\n * Filter out noisy gcloud hints from captured output.\n * Removes \"To execute this job\" and \"Updates are available\" blocks.\n */\nfunction filterGcloudOutput(output: string): string {\n return output\n .replace(/To execute this job.*?gcloud run jobs execute \\S+\\n?/gs, \"\")\n .replace(/Updates are available.*?\\$ gcloud components update\\n?/gs, \"\")\n .trim();\n}\n\nconst DEFAULT_REGION = \"us-central1\";\nconst DEFAULT_ARTIFACT_REGISTRY = \"cloud-run\";\nconst DEFAULT_ISOLATE_PATH = \"isolate\";\nconst GENERATED_DOCKERFILE = \"Dockerfile\";\nconst REGISTRY = \"docker.pkg.dev\";\n\ninterface IsolateConfig {\n targetPackagePath?: string;\n}\n\n/**\n * Resolve the isolate output directory path.\n * Reads isolate.config.json if present, otherwise uses default \"./isolate\".\n */\nfunction resolveIsolateDirectory(serviceDirectory: string): string {\n const configPath = path.join(serviceDirectory, \"isolate.config.json\");\n\n if (existsSync(configPath)) {\n try {\n const configContent = readFileSync(configPath, \"utf-8\");\n const config = JSON.parse(configContent) as IsolateConfig;\n if (config.targetPackagePath) {\n return path.join(serviceDirectory, config.targetPackagePath);\n }\n } catch {\n /** Ignore parse errors, use default */\n }\n }\n\n return path.join(serviceDirectory, DEFAULT_ISOLATE_PATH);\n}\n\nexport interface PrepareResult {\n imageUri: string;\n imageBuilt: boolean;\n region: string;\n project: string;\n}\n\n/**\n * Shared preparation logic: isolate, hash, check image, build if needed.\n * Used by both deploy() and deployIfChanged().\n */\nexport async function prepareImage(\n options: DeployOptions,\n): Promise<PrepareResult> {\n const { cloud, envConfig, serviceDirectory } = options;\n const region = cloud.region ?? DEFAULT_REGION;\n const artifactRegistry = cloud.artifactRegistry ?? DEFAULT_ARTIFACT_REGISTRY;\n const project = envConfig.project;\n let buildLocal = cloud.buildLocal !== false;\n\n checkGcloudAvailable();\n\n if (buildLocal) {\n if (!isDockerInstalled()) {\n consola.warn(\n \"Docker is not installed, falling back to Cloud Build. \" +\n \"Install Docker for faster local builds: https://docs.docker.com/get-docker/\",\n );\n buildLocal = false;\n } else if (!isDockerDaemonRunning()) {\n if (!process.stdin.isTTY) {\n /** Non-interactive environment (CI) — fall back automatically */\n consola.warn(\n \"Docker daemon is not running, falling back to Cloud Build.\",\n );\n buildLocal = false;\n } else {\n const choice = await consola.prompt(\n \"Docker is installed but the daemon is not running.\",\n {\n type: \"select\",\n options: [\n {\n label: \"Start Docker\",\n value: \"start\",\n hint: \"attempt to start the daemon\",\n },\n {\n label: \"Use Cloud Build\",\n value: \"cloud-build\",\n hint: \"build remotely instead\",\n },\n ],\n },\n );\n\n if (typeof choice === \"symbol\") {\n process.exit(0);\n }\n\n if (choice === \"start\") {\n const started = startDockerDaemon();\n\n if (!started) {\n consola.warn(\n \"Could not start Docker automatically, falling back to Cloud Build.\",\n );\n buildLocal = false;\n } else {\n const ready = await waitForDockerDaemon();\n\n if (!ready) {\n consola.warn(\n \"Docker daemon did not become ready in time, falling back to Cloud Build.\",\n );\n buildLocal = false;\n }\n }\n } else {\n buildLocal = false;\n }\n }\n }\n }\n\n /** Step 1: Run isolate to bundle workspace dependencies */\n const isolateDirectory = resolveIsolateDirectory(serviceDirectory);\n\n consola.start(\"Isolating package...\");\n\n try {\n const { isolate: runIsolate } = await import(\"isolate-package\");\n\n const configPath = path.join(serviceDirectory, \"isolate.config.json\");\n const fileConfig = existsSync(configPath)\n ? JSON.parse(readFileSync(configPath, \"utf-8\"))\n : {};\n\n await runIsolate({\n ...fileConfig,\n includeDevDependencies: true,\n });\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n consola.error(`Failed to isolate package: ${message}`);\n process.exit(1);\n }\n\n consola.success(\"Package isolated\");\n\n /** Step 2: Hash the isolate directory */\n const tag = await hashDirectory(isolateDirectory);\n consola.info(`Content hash: ${tag}`);\n\n /** Step 3: Check if image already exists */\n const imageUri = `${region}-${REGISTRY}/${project}/${artifactRegistry}/${cloud.name}:${tag}`;\n\n const imageExists = checkImageExists(imageUri, project);\n let imageBuilt = false;\n\n if (imageExists) {\n consola.success(`Image already exists: ${cloud.name}:${tag}`);\n } else if (buildLocal) {\n /** Step 4a: Generate Dockerfile and build locally with Docker */\n consola.start(\"Building image locally with Docker...\");\n\n const dockerfilePath = path.join(serviceDirectory, GENERATED_DOCKERFILE);\n writeFileSync(dockerfilePath, generateDockerfile());\n\n try {\n const buildResult = shellExecCapture(\n `docker build --platform linux/amd64 -t ${imageUri} .`,\n { cwd: serviceDirectory },\n );\n\n if (!buildResult.success) {\n consola.error(\"Docker build failed. Output:\\n\" + buildResult.output);\n process.exit(1);\n }\n\n consola.success(`Image built: ${cloud.name}:${tag}`);\n\n /** Configure Docker authentication for Artifact Registry */\n gcloudExecCapture([\n \"auth\",\n \"configure-docker\",\n `${region}-${REGISTRY}`,\n \"--quiet\",\n ]);\n\n consola.start(\"Pushing image to Artifact Registry...\");\n\n const pushResult = shellExecCapture(`docker push ${imageUri}`);\n\n if (!pushResult.success) {\n consola.error(\"Docker push failed. Output:\\n\" + pushResult.output);\n process.exit(1);\n }\n\n consola.success(`Image pushed: ${cloud.name}:${tag}`);\n imageBuilt = true;\n } finally {\n /** Clean up generated Dockerfile */\n try {\n unlinkSync(dockerfilePath);\n } catch {\n /** Ignore cleanup errors */\n }\n }\n } else {\n /** Step 4b: Generate Dockerfile and build with Cloud Build */\n consola.start(\"Building image with Cloud Build...\");\n\n const dockerfilePath = path.join(serviceDirectory, GENERATED_DOCKERFILE);\n writeFileSync(dockerfilePath, generateDockerfile());\n\n try {\n const buildResult = gcloudExecCapture(\n [\n \"builds\",\n \"submit\",\n \"--project\",\n project,\n \"--region\",\n region,\n `--tag=${imageUri}`,\n \".\",\n ],\n { cwd: serviceDirectory },\n );\n\n /** Extract and show the Cloud Build logs URL if available */\n const logsUrlMatch = buildResult.output.match(\n /Logs are available at \\[(.+?)]/,\n );\n if (logsUrlMatch) {\n consola.info(`Cloud Build logs: ${logsUrlMatch[1]}`);\n }\n\n if (!buildResult.success) {\n consola.error(\"Cloud Build failed. Output:\\n\" + buildResult.output);\n process.exit(1);\n }\n\n consola.success(`Image built: ${cloud.name}:${tag}`);\n imageBuilt = true;\n } finally {\n /** Clean up generated Dockerfile */\n try {\n unlinkSync(dockerfilePath);\n } catch {\n /** Ignore cleanup errors */\n }\n }\n }\n\n return { imageUri, imageBuilt, region, project };\n}\n\n/**\n * Build and push a Cloud Run Job image.\n *\n * This is image-only: it does not create or update Cloud Run Job resources.\n * Use createOrUpdateJob() separately to manage job resources.\n */\nexport async function deploy(options: DeployOptions): Promise<DeployResult> {\n const { imageUri, imageBuilt } = await prepareImage(options);\n return { imageUri, imageBuilt };\n}\n\n/**\n * Build and push a Cloud Run Job image only if it has changed.\n *\n * This is image-only: it does not create or update Cloud Run Job resources.\n * Use createOrUpdateJob() separately to manage job resources.\n */\nexport async function deployIfChanged(\n options: DeployOptions,\n): Promise<DeployResult> {\n const { imageUri, imageBuilt } = await prepareImage(options);\n\n if (!imageBuilt) {\n consola.info(\"No changes detected, skipping image build\");\n }\n\n return { imageUri, imageBuilt };\n}\n\nexport interface CreateOrUpdateJobOptions {\n cloud: CloudConfig;\n envConfig: RunnerEnvOptions;\n /** The Cloud Run Job resource name (e.g., \"admin-create-user\") */\n jobName: string;\n imageUri: string;\n region: string;\n project: string;\n}\n\n/**\n * Create or update a Cloud Run Job resource.\n * Returns true if the job was newly created, false if updated.\n */\nexport async function createOrUpdateJob(\n options: CreateOrUpdateJobOptions,\n): Promise<boolean> {\n const { cloud, envConfig, jobName, imageUri, region, project } = options;\n const memory = cloud.resources?.memory ?? \"512Mi\";\n const cpu = cloud.resources?.cpu ?? \"1\";\n const timeout = cloud.resources?.timeout ?? 86400;\n\n /** Check if job already exists */\n const existingJob = gcloudJson(\n [\n \"run\",\n \"jobs\",\n \"describe\",\n jobName,\n \"--project\",\n project,\n \"--region\",\n region,\n ],\n { ignoreErrors: true },\n );\n\n /** Build environment variables (envFile < env < project) */\n const envFileVars = parseEnvFiles(envConfig.envFile);\n const envVars: Record<string, string> = {\n ...envFileVars,\n ...envConfig.env,\n GOOGLE_CLOUD_PROJECT: project,\n };\n\n const envVarsString = Object.entries(envVars)\n .map(([key, value]) => `${key}=${value}`)\n .join(\",\");\n\n /** Build secret references */\n const secretNames = envConfig.secrets ?? [];\n const secretsString = secretNames\n .map((name) => `${name}=${name}:latest`)\n .join(\",\");\n\n if (existingJob) {\n consola.start(\"Updating Cloud Run Job...\");\n\n const updateArgs = [\n \"run\",\n \"jobs\",\n \"update\",\n jobName,\n \"--project\",\n project,\n \"--region\",\n region,\n `--image=${imageUri}`,\n `--set-env-vars=${envVarsString}`,\n `--memory=${memory}`,\n `--cpu=${cpu}`,\n `--task-timeout=${timeout}s`,\n \"--max-retries=0\",\n ];\n\n /**\n * Always pass --parallelism on update so that removing it from config\n * resets the deployed value. Default 0 means no concurrency limit.\n */\n updateArgs.push(`--parallelism=${cloud.resources?.parallelism ?? 0}`);\n\n if (secretsString) {\n updateArgs.push(`--set-secrets=${secretsString}`);\n }\n\n if (cloud.serviceAccount) {\n updateArgs.push(`--service-account=${cloud.serviceAccount}`);\n }\n\n if (cloud.network) {\n updateArgs.push(`--network=${cloud.network.name}`);\n if (cloud.network.subnet) {\n updateArgs.push(`--subnet=${cloud.network.subnet}`);\n }\n updateArgs.push(\n `--vpc-egress=${cloud.network.egress ?? \"private-ranges-only\"}`,\n );\n }\n\n const result = gcloudExecCapture(updateArgs);\n\n if (!result.success) {\n consola.error(\"Failed to update Cloud Run Job\");\n process.exit(1);\n }\n\n const filtered = filterGcloudOutput(result.stderr);\n if (filtered) {\n process.stderr.write(filtered + \"\\n\");\n }\n\n consola.success(`Cloud Run Job updated: ${jobName}`);\n return false;\n }\n\n consola.start(\"Creating Cloud Run Job...\");\n\n const createArgs = [\n \"run\",\n \"jobs\",\n \"create\",\n jobName,\n \"--project\",\n project,\n \"--region\",\n region,\n `--image=${imageUri}`,\n `--set-env-vars=${envVarsString}`,\n `--memory=${memory}`,\n `--cpu=${cpu}`,\n `--task-timeout=${timeout}s`,\n \"--max-retries=0\",\n ];\n\n if (cloud.resources?.parallelism !== undefined) {\n createArgs.push(`--parallelism=${cloud.resources.parallelism}`);\n }\n\n if (secretsString) {\n createArgs.push(`--set-secrets=${secretsString}`);\n }\n\n if (cloud.serviceAccount) {\n createArgs.push(`--service-account=${cloud.serviceAccount}`);\n }\n\n if (cloud.network) {\n createArgs.push(`--network=${cloud.network.name}`);\n if (cloud.network.subnet) {\n createArgs.push(`--subnet=${cloud.network.subnet}`);\n }\n createArgs.push(\n `--vpc-egress=${cloud.network.egress ?? \"private-ranges-only\"}`,\n );\n }\n\n const result = gcloudExecCapture(createArgs);\n\n if (!result.success) {\n consola.error(\"Failed to create Cloud Run Job\");\n process.exit(1);\n }\n\n const filtered = filterGcloudOutput(result.stderr);\n if (filtered) {\n process.stderr.write(filtered + \"\\n\");\n }\n\n consola.success(`Cloud Run Job created: ${jobName}`);\n return true;\n}\n\n/**\n * Check if a Docker image exists in Artifact Registry.\n */\nfunction checkImageExists(imageUri: string, project: string): boolean {\n const result = gcloudJson(\n [\n \"artifacts\",\n \"docker\",\n \"images\",\n \"describe\",\n imageUri,\n \"--project\",\n project,\n ],\n { ignoreErrors: true },\n );\n\n return result !== undefined;\n}\n"],"mappings":";;;;;;;;;;;;;AAsCA,SAAS,mBAAmB,QAAwB;AAClD,QAAO,OACJ,QAAQ,0DAA0D,GAAG,CACrE,QAAQ,4DAA4D,GAAG,CACvE,MAAM;;AAGX,MAAM,iBAAiB;AACvB,MAAM,4BAA4B;AAClC,MAAM,uBAAuB;AAC7B,MAAM,uBAAuB;AAC7B,MAAM,WAAW;;;;;AAUjB,SAAS,wBAAwB,kBAAkC;CACjE,MAAM,aAAa,KAAK,KAAK,kBAAkB,sBAAsB;AAErE,KAAI,WAAW,WAAW,CACxB,KAAI;EACF,MAAM,gBAAgB,aAAa,YAAY,QAAQ;EACvD,MAAM,SAAS,KAAK,MAAM,cAAc;AACxC,MAAI,OAAO,kBACT,QAAO,KAAK,KAAK,kBAAkB,OAAO,kBAAkB;SAExD;AAKV,QAAO,KAAK,KAAK,kBAAkB,qBAAqB;;;;;;AAc1D,eAAsB,aACpB,SACwB;CACxB,MAAM,EAAE,OAAO,WAAW,qBAAqB;CAC/C,MAAM,SAAS,MAAM,UAAU;CAC/B,MAAM,mBAAmB,MAAM,oBAAoB;C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AClD,KAAI,SACF,SAAQ,OAAO,MAAM,WAAW,KAAK;AAGvC,SAAQ,QAAQ,0BAA0B,UAAU;AACpD,QAAO;;;;;AAMT,SAAS,iBAAiB,UAAkB,SAA0B;AAcpE,QAbe,WACb;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACD,EACD,EAAE,cAAc,MAAM,CACvB,KAEiB"}
|
|
1
|
+
{"version":3,"file":"deploy.mjs","names":[],"sources":["../../src/cloud/deploy.ts"],"sourcesContent":["import { existsSync, readFileSync, writeFileSync, unlinkSync } from \"node:fs\";\nimport path from \"node:path\";\nimport { consola } from \"consola\";\nimport type { CloudConfig, RunnerEnvOptions } from \"../config\";\nimport { parseEnvFiles } from \"../env-file\";\nimport { generateDockerfile } from \"./dockerfile\";\nimport {\n checkGcloudAvailable,\n isDockerInstalled,\n isDockerDaemonRunning,\n startDockerDaemon,\n waitForDockerDaemon,\n gcloudExecCapture,\n gcloudJson,\n shellExecCapture,\n} from \"./gcloud\";\nimport { hashDirectory } from \"./hash\";\n\nexport interface DeployOptions {\n /** Cloud configuration from the runner config */\n cloud: CloudConfig;\n /** Environment configuration (project, env vars, secrets) */\n envConfig: RunnerEnvOptions;\n /** Working directory (service root where isolate.config.json lives) */\n serviceDirectory: string;\n}\n\nexport interface DeployResult {\n /** Full image URI including tag */\n imageUri: string;\n /** Whether a new image was built */\n imageBuilt: boolean;\n}\n\n/**\n * Filter out noisy gcloud hints from captured output.\n * Removes \"To execute this job\" and \"Updates are available\" blocks.\n */\nfunction filterGcloudOutput(output: string): string {\n return output\n .replace(/To execute this job.*?gcloud run jobs execute \\S+\\n?/gs, \"\")\n .replace(/Updates are available.*?\\$ gcloud components update\\n?/gs, \"\")\n .trim();\n}\n\nconst DEFAULT_REGION = \"us-central1\";\nconst DEFAULT_ARTIFACT_REGISTRY = \"cloud-run\";\nconst DEFAULT_ISOLATE_PATH = \"isolate\";\nconst GENERATED_DOCKERFILE = \"Dockerfile\";\nconst REGISTRY = \"docker.pkg.dev\";\n\ninterface IsolateConfig {\n targetPackagePath?: string;\n}\n\n/**\n * Resolve the isolate output directory path.\n * Reads isolate.config.json if present, otherwise uses default \"./isolate\".\n */\nfunction resolveIsolateDirectory(serviceDirectory: string): string {\n const configPath = path.join(serviceDirectory, \"isolate.config.json\");\n\n if (existsSync(configPath)) {\n try {\n const configContent = readFileSync(configPath, \"utf-8\");\n const config = JSON.parse(configContent) as IsolateConfig;\n if (config.targetPackagePath) {\n return path.join(serviceDirectory, config.targetPackagePath);\n }\n } catch {\n /** Ignore parse errors, use default */\n }\n }\n\n return path.join(serviceDirectory, DEFAULT_ISOLATE_PATH);\n}\n\nexport interface PrepareResult {\n imageUri: string;\n imageBuilt: boolean;\n region: string;\n project: string;\n}\n\n/**\n * Shared preparation logic: isolate, hash, check image, build if needed.\n * Used by both deploy() and deployIfChanged().\n */\nexport async function prepareImage(\n options: DeployOptions,\n): Promise<PrepareResult> {\n const { cloud, envConfig, serviceDirectory } = options;\n const region = cloud.region ?? DEFAULT_REGION;\n const artifactRegistry = cloud.artifactRegistry ?? DEFAULT_ARTIFACT_REGISTRY;\n const project = envConfig.project;\n let buildLocal = cloud.buildLocal !== false;\n\n checkGcloudAvailable();\n\n if (buildLocal) {\n if (!isDockerInstalled()) {\n consola.warn(\n \"Docker is not installed, falling back to Cloud Build. \" +\n \"Install Docker for faster local builds: https://docs.docker.com/get-docker/\",\n );\n buildLocal = false;\n } else if (!isDockerDaemonRunning()) {\n if (!process.stdin.isTTY) {\n /** Non-interactive environment (CI) — fall back automatically */\n consola.warn(\n \"Docker daemon is not running, falling back to Cloud Build.\",\n );\n buildLocal = false;\n } else {\n const choice = await consola.prompt(\n \"Docker is installed but the daemon is not running.\",\n {\n type: \"select\",\n options: [\n {\n label: \"Start Docker\",\n value: \"start\",\n hint: \"attempt to start the daemon\",\n },\n {\n label: \"Use Cloud Build\",\n value: \"cloud-build\",\n hint: \"build remotely instead\",\n },\n ],\n },\n );\n\n if (typeof choice === \"symbol\") {\n process.exit(0);\n }\n\n if (choice === \"start\") {\n const started = startDockerDaemon();\n\n if (!started) {\n consola.warn(\n \"Could not start Docker automatically, falling back to Cloud Build.\",\n );\n buildLocal = false;\n } else {\n const ready = await waitForDockerDaemon();\n\n if (!ready) {\n consola.warn(\n \"Docker daemon did not become ready in time, falling back to Cloud Build.\",\n );\n buildLocal = false;\n }\n }\n } else {\n buildLocal = false;\n }\n }\n }\n }\n\n /** Step 1: Run isolate to bundle workspace dependencies */\n const isolateDirectory = resolveIsolateDirectory(serviceDirectory);\n\n consola.start(\"Isolating package...\");\n\n try {\n const { isolate: runIsolate } = await import(\"isolate-package\");\n\n const configPath = path.join(serviceDirectory, \"isolate.config.json\");\n const fileConfig = existsSync(configPath)\n ? JSON.parse(readFileSync(configPath, \"utf-8\"))\n : {};\n\n await runIsolate({\n ...fileConfig,\n includeDevDependencies: true,\n });\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n consola.error(`Failed to isolate package: ${message}`);\n process.exit(1);\n }\n\n consola.success(\"Package isolated\");\n\n /** Step 2: Hash the isolate directory */\n const tag = await hashDirectory(isolateDirectory);\n consola.info(`Content hash: ${tag}`);\n\n /** Step 3: Check if image already exists */\n const imageUri = `${region}-${REGISTRY}/${project}/${artifactRegistry}/${cloud.name}:${tag}`;\n\n const imageExists = checkImageExists(imageUri, project);\n let imageBuilt = false;\n\n if (imageExists) {\n consola.success(`Image already exists: ${cloud.name}:${tag}`);\n } else if (buildLocal) {\n /** Step 4a: Generate Dockerfile and build locally with Docker */\n consola.start(\"Building image locally with Docker...\");\n\n const dockerfilePath = path.join(serviceDirectory, GENERATED_DOCKERFILE);\n writeFileSync(dockerfilePath, generateDockerfile());\n\n try {\n const buildResult = shellExecCapture(\n `docker build --platform linux/amd64 -t ${imageUri} .`,\n { cwd: serviceDirectory },\n );\n\n if (!buildResult.success) {\n consola.error(\"Docker build failed. Output:\\n\" + buildResult.output);\n process.exit(1);\n }\n\n consola.success(`Image built: ${cloud.name}:${tag}`);\n\n /** Configure Docker authentication for Artifact Registry */\n gcloudExecCapture([\n \"auth\",\n \"configure-docker\",\n `${region}-${REGISTRY}`,\n \"--quiet\",\n ]);\n\n consola.start(\"Pushing image to Artifact Registry...\");\n\n const pushResult = shellExecCapture(`docker push ${imageUri}`);\n\n if (!pushResult.success) {\n consola.error(\"Docker push failed. Output:\\n\" + pushResult.output);\n process.exit(1);\n }\n\n consola.success(`Image pushed: ${cloud.name}:${tag}`);\n imageBuilt = true;\n } finally {\n /** Clean up generated Dockerfile */\n try {\n unlinkSync(dockerfilePath);\n } catch {\n /** Ignore cleanup errors */\n }\n }\n } else {\n /** Step 4b: Generate Dockerfile and build with Cloud Build */\n consola.start(\"Building image with Cloud Build...\");\n\n const dockerfilePath = path.join(serviceDirectory, GENERATED_DOCKERFILE);\n writeFileSync(dockerfilePath, generateDockerfile());\n\n try {\n const buildResult = gcloudExecCapture(\n [\n \"builds\",\n \"submit\",\n \"--project\",\n project,\n \"--region\",\n region,\n `--tag=${imageUri}`,\n \".\",\n ],\n { cwd: serviceDirectory },\n );\n\n /** Extract and show the Cloud Build logs URL if available */\n const logsUrlMatch = buildResult.output.match(\n /Logs are available at \\[(.+?)]/,\n );\n if (logsUrlMatch) {\n consola.info(`Cloud Build logs: ${logsUrlMatch[1]}`);\n }\n\n if (!buildResult.success) {\n consola.error(\"Cloud Build failed. Output:\\n\" + buildResult.output);\n process.exit(1);\n }\n\n consola.success(`Image built: ${cloud.name}:${tag}`);\n imageBuilt = true;\n } finally {\n /** Clean up generated Dockerfile */\n try {\n unlinkSync(dockerfilePath);\n } catch {\n /** Ignore cleanup errors */\n }\n }\n }\n\n return { imageUri, imageBuilt, region, project };\n}\n\n/**\n * Build and push a Cloud Run Job image.\n *\n * This is image-only: it does not create or update Cloud Run Job resources.\n * Use createOrUpdateJob() separately to manage job resources.\n */\nexport async function deploy(options: DeployOptions): Promise<DeployResult> {\n const { imageUri, imageBuilt } = await prepareImage(options);\n return { imageUri, imageBuilt };\n}\n\n/**\n * Build and push a Cloud Run Job image only if it has changed.\n *\n * This is image-only: it does not create or update Cloud Run Job resources.\n * Use createOrUpdateJob() separately to manage job resources.\n */\nexport async function deployIfChanged(\n options: DeployOptions,\n): Promise<DeployResult> {\n const { imageUri, imageBuilt } = await prepareImage(options);\n\n if (!imageBuilt) {\n consola.info(\"No changes detected, skipping image build\");\n }\n\n return { imageUri, imageBuilt };\n}\n\nexport interface CreateOrUpdateJobOptions {\n cloud: CloudConfig;\n envConfig: RunnerEnvOptions;\n /** The Cloud Run Job resource name (e.g., \"admin-create-user\") */\n jobName: string;\n imageUri: string;\n region: string;\n project: string;\n}\n\n/**\n * Create or update a Cloud Run Job resource.\n * Returns true if the job was newly created, false if updated.\n */\nexport async function createOrUpdateJob(\n options: CreateOrUpdateJobOptions,\n): Promise<boolean> {\n const { cloud, envConfig, jobName, imageUri, region, project } = options;\n const memory = cloud.resources?.memory ?? \"512Mi\";\n const cpu = cloud.resources?.cpu ?? \"1\";\n const timeout = cloud.resources?.timeout ?? 86400;\n\n /** Check if job already exists */\n const existingJob = gcloudJson(\n [\n \"run\",\n \"jobs\",\n \"describe\",\n jobName,\n \"--project\",\n project,\n \"--region\",\n region,\n ],\n { ignoreErrors: true },\n );\n\n /** Build environment variables (envFile < env < project) */\n const envFileVars = parseEnvFiles(envConfig.envFile);\n const envVars: Record<string, string> = {\n ...envFileVars,\n ...envConfig.env,\n GOOGLE_CLOUD_PROJECT: project,\n };\n\n /**\n * Forward the exports destination to the container. Local paths are\n * rejected earlier by assertCloudExportsPath() in the CLI handlers.\n */\n if (envConfig.exportsPath) {\n envVars.JOB_EXPORTS_PATH = envConfig.exportsPath;\n }\n\n const envVarsString = Object.entries(envVars)\n .map(([key, value]) => `${key}=${value}`)\n .join(\",\");\n\n /** Build secret references */\n const secretNames = envConfig.secrets ?? [];\n const secretsString = secretNames\n .map((name) => `${name}=${name}:latest`)\n .join(\",\");\n\n if (existingJob) {\n consola.start(\"Updating Cloud Run Job...\");\n\n const updateArgs = [\n \"run\",\n \"jobs\",\n \"update\",\n jobName,\n \"--project\",\n project,\n \"--region\",\n region,\n `--image=${imageUri}`,\n `--set-env-vars=${envVarsString}`,\n `--memory=${memory}`,\n `--cpu=${cpu}`,\n `--task-timeout=${timeout}s`,\n \"--max-retries=0\",\n ];\n\n /**\n * Always pass --parallelism on update so that removing it from config\n * resets the deployed value. Default 0 means no concurrency limit.\n */\n updateArgs.push(`--parallelism=${cloud.resources?.parallelism ?? 0}`);\n\n if (secretsString) {\n updateArgs.push(`--set-secrets=${secretsString}`);\n }\n\n if (cloud.serviceAccount) {\n updateArgs.push(`--service-account=${cloud.serviceAccount}`);\n }\n\n if (cloud.network) {\n updateArgs.push(`--network=${cloud.network.name}`);\n if (cloud.network.subnet) {\n updateArgs.push(`--subnet=${cloud.network.subnet}`);\n }\n updateArgs.push(\n `--vpc-egress=${cloud.network.egress ?? \"private-ranges-only\"}`,\n );\n }\n\n const result = gcloudExecCapture(updateArgs);\n\n if (!result.success) {\n consola.error(\"Failed to update Cloud Run Job\");\n process.exit(1);\n }\n\n const filtered = filterGcloudOutput(result.stderr);\n if (filtered) {\n process.stderr.write(filtered + \"\\n\");\n }\n\n consola.success(`Cloud Run Job updated: ${jobName}`);\n return false;\n }\n\n consola.start(\"Creating Cloud Run Job...\");\n\n const createArgs = [\n \"run\",\n \"jobs\",\n \"create\",\n jobName,\n \"--project\",\n project,\n \"--region\",\n region,\n `--image=${imageUri}`,\n `--set-env-vars=${envVarsString}`,\n `--memory=${memory}`,\n `--cpu=${cpu}`,\n `--task-timeout=${timeout}s`,\n \"--max-retries=0\",\n ];\n\n if (cloud.resources?.parallelism !== undefined) {\n createArgs.push(`--parallelism=${cloud.resources.parallelism}`);\n }\n\n if (secretsString) {\n createArgs.push(`--set-secrets=${secretsString}`);\n }\n\n if (cloud.serviceAccount) {\n createArgs.push(`--service-account=${cloud.serviceAccount}`);\n }\n\n if (cloud.network) {\n createArgs.push(`--network=${cloud.network.name}`);\n if (cloud.network.subnet) {\n createArgs.push(`--subnet=${cloud.network.subnet}`);\n }\n createArgs.push(\n `--vpc-egress=${cloud.network.egress ?? \"private-ranges-only\"}`,\n );\n }\n\n const result = gcloudExecCapture(createArgs);\n\n if (!result.success) {\n consola.error(\"Failed to create Cloud Run Job\");\n process.exit(1);\n }\n\n const filtered = filterGcloudOutput(result.stderr);\n if (filtered) {\n process.stderr.write(filtered + \"\\n\");\n }\n\n consola.success(`Cloud Run Job created: ${jobName}`);\n return true;\n}\n\n/**\n * Check if a Docker image exists in Artifact Registry.\n */\nfunction checkImageExists(imageUri: string, project: string): boolean {\n const result = gcloudJson(\n [\n \"artifacts\",\n \"docker\",\n \"images\",\n \"describe\",\n imageUri,\n \"--project\",\n project,\n ],\n { ignoreErrors: true },\n );\n\n return result !== undefined;\n}\n"],"mappings":";;;;;;;;;;;;;AAsCA,SAAS,mBAAmB,QAAwB;AAClD,QAAO,OACJ,QAAQ,0DAA0D,GAAG,CACrE,QAAQ,4DAA4D,GAAG,CACvE,MAAM;;AAGX,MAAM,iBAAiB;AACvB,MAAM,4BAA4B;AAClC,MAAM,uBAAuB;AAC7B,MAAM,uBAAuB;AAC7B,MAAM,WAAW;;;;;AAUjB,SAAS,wBAAwB,kBAAkC;CACjE,MAAM,aAAa,KAAK,KAAK,kBAAkB,sBAAsB;AAErE,KAAI,WAAW,WAAW,CACxB,KAAI;EACF,MAAM,gBAAgB,aAAa,YAAY,QAAQ;EACvD,MAAM,SAAS,KAAK,MAAM,cAAc;AACxC,MAAI,OAAO,kBACT,QAAO,KAAK,KAAK,kBAAkB,OAAO,kBAAkB;SAExD;AAKV,QAAO,KAAK,KAAK,kBAAkB,qBAAqB;;;;;;AAc1D,eAAsB,aACpB,SACwB;CACxB,MAAM,EAAE,OAAO,WAAW,qBAAqB;CAC/C,MAAM,SAAS,MAAM,UAAU;CAC/B,MAAM,mBAAmB,MAAM,oBAAoB;CACnD,MAAM,UAAU,UAAU;CAC1B,IAAI,aAAa,MAAM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AAO;AAClD,KAAI,SACF,SAAQ,OAAO,MAAM,WAAW,KAAK;AAGvC,SAAQ,QAAQ,0BAA0B,UAAU;AACpD,QAAO;;;;;AAMT,SAAS,iBAAiB,UAAkB,SAA0B;AAcpE,QAbe,WACb;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACD,EACD,EAAE,cAAc,MAAM,CACvB,KAEiB"}
|
package/dist/cloud/hash.mjs
CHANGED
package/dist/config.d.mts
CHANGED
|
@@ -13,6 +13,20 @@ interface RunnerEnvOptions {
|
|
|
13
13
|
env?: Record<string, string>;
|
|
14
14
|
/** Secret names to load from GCP Secret Manager */
|
|
15
15
|
secrets?: string[];
|
|
16
|
+
/**
|
|
17
|
+
* Destination for artifacts written via `getExportsWriter()`.
|
|
18
|
+
*
|
|
19
|
+
* Either a local path (resolved relative to the service directory) or a
|
|
20
|
+
* `gs://bucket[/prefix]` URI. For most setups this is a `gs://` URI — one
|
|
21
|
+
* per environment — paired with a top-level `localExportsPath` that
|
|
22
|
+
* catches every local run regardless of which environment is selected.
|
|
23
|
+
*
|
|
24
|
+
* When unset, `getExportsWriter()` throws (unless `localExportsPath` is
|
|
25
|
+
* set and the run is local). Cloud deployments require a `gs://` URI.
|
|
26
|
+
*
|
|
27
|
+
* @see RunnerConfig.localExportsPath
|
|
28
|
+
*/
|
|
29
|
+
exportsPath?: string;
|
|
16
30
|
}
|
|
17
31
|
/** Container resource limits for a Cloud Run Job */
|
|
18
32
|
interface CloudResources {
|
|
@@ -70,6 +84,17 @@ interface RunnerConfig {
|
|
|
70
84
|
};
|
|
71
85
|
/** Named environments (e.g., stag, prod) */
|
|
72
86
|
environments: Record<string, RunnerEnvOptions>;
|
|
87
|
+
/**
|
|
88
|
+
* Destination used by `getExportsWriter()` for every local run, regardless
|
|
89
|
+
* of which environment is selected. Resolved relative to the service
|
|
90
|
+
* directory; `gs://bucket[/prefix]` URIs are also accepted.
|
|
91
|
+
*
|
|
92
|
+
* When set, local runs ignore the environment's `exportsPath`. When unset,
|
|
93
|
+
* local runs fall back to the environment's `exportsPath`.
|
|
94
|
+
*
|
|
95
|
+
* Cloud runs always use the environment's `exportsPath`.
|
|
96
|
+
*/
|
|
97
|
+
localExportsPath?: string;
|
|
73
98
|
/** Cloud Run Jobs configuration (required for `job cloud run/deploy` commands) */
|
|
74
99
|
cloud?: CloudConfig;
|
|
75
100
|
/**
|
package/dist/config.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
1
3
|
//#region src/config.ts
|
|
2
4
|
/** Identity function for type-safe runner config definition */
|
|
3
5
|
function defineRunnerConfig(config) {
|
|
@@ -7,7 +9,18 @@ function defineRunnerConfig(config) {
|
|
|
7
9
|
function defineRunnerEnv(options) {
|
|
8
10
|
return options;
|
|
9
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Resolve the exports destination for a local run. Prefers the top-level
|
|
14
|
+
* `localExportsPath` over the environment's `exportsPath`. Local paths are
|
|
15
|
+
* resolved against the service directory; `gs://` URIs pass through
|
|
16
|
+
* unchanged.
|
|
17
|
+
*/
|
|
18
|
+
function resolveLocalExportsPath(config, envConfig, serviceDirectory) {
|
|
19
|
+
const raw = config.localExportsPath ?? envConfig.exportsPath;
|
|
20
|
+
if (!raw) return void 0;
|
|
21
|
+
return raw.startsWith("gs://") ? raw : path.resolve(serviceDirectory, raw);
|
|
22
|
+
}
|
|
10
23
|
|
|
11
24
|
//#endregion
|
|
12
|
-
export { defineRunnerConfig, defineRunnerEnv };
|
|
25
|
+
export { defineRunnerConfig, defineRunnerEnv, resolveLocalExportsPath };
|
|
13
26
|
//# sourceMappingURL=config.mjs.map
|
package/dist/config.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.mjs","names":[],"sources":["../src/config.ts"],"sourcesContent":["/** Environment configuration for a specific deployment target */\nexport interface RunnerEnvOptions {\n /** GCP project ID — sets GOOGLE_CLOUD_PROJECT automatically */\n project: string;\n /**\n * Path(s) to .env files to load, resolved relative to the service directory.\n * Variables from these files have lower precedence than explicit `env` values.\n * When multiple files are specified, earlier files take precedence over later ones.\n */\n envFile?: string | string[];\n /** Additional environment variables to set before the job runs */\n env?: Record<string, string>;\n /** Secret names to load from GCP Secret Manager */\n secrets?: string[];\n}\n\n/** Container resource limits for a Cloud Run Job */\nexport interface CloudResources {\n /** Memory limit (e.g., \"512Mi\", \"1Gi\"). Default: \"512Mi\" */\n memory?: string;\n /** CPU limit (e.g., \"1\", \"2\"). Default: \"1\" */\n cpu?: string;\n /** Job timeout in seconds. Default: 86400 (24 hours) */\n timeout?: number;\n /** Maximum number of tasks that can run in parallel. Default: unset (no limit) */\n parallelism?: number;\n}\n\n/** Direct VPC egress configuration for private network access (e.g., Redis) */\nexport interface CloudNetworkConfig {\n /** VPC network name (e.g., \"default\") */\n name: string;\n /** VPC subnet name (e.g., \"default\") */\n subnet?: string;\n /** VPC egress mode. Default: \"private-ranges-only\" */\n egress?: \"all-traffic\" | \"private-ranges-only\";\n}\n\n/** Configuration for Cloud Run Jobs execution */\nexport interface CloudConfig {\n /** Cloud Run Job name (e.g., \"loads-predictions-jobs\") */\n name: string;\n /** GCP region. Default: \"us-central1\" */\n region?: string;\n /** Artifact Registry repository name. Default: \"cloud-run\" */\n artifactRegistry?: string;\n /** Container resource limits */\n resources?: CloudResources;\n /** Service account email for the Cloud Run Job */\n serviceAccount?: string;\n /**\n * Build Docker images locally instead of using Cloud Build.\n * Requires Docker to be installed and running. Default: true.\n */\n buildLocal?: boolean;\n /** Direct VPC egress configuration for private network access */\n network?: CloudNetworkConfig;\n}\n\n/** Full runner configuration provided by each service */\nexport interface RunnerConfig {\n /**\n * Absolute path to the directory containing job scripts.\n * Default: `dist/jobs` relative to cwd.\n */\n jobsDirectory?: string;\n /** Optional initialization function called before the job runs (skipped for --help) */\n initialize?: () => void | Promise<void>;\n /** Optional custom logger (defaults to console) */\n logger?: {\n info: (message: string) => void;\n error: (message: string) => void;\n };\n /** Named environments (e.g., stag, prod) */\n environments: Record<string, RunnerEnvOptions>;\n /** Cloud Run Jobs configuration (required for `job cloud run/deploy` commands) */\n cloud?: CloudConfig;\n /**\n * Command to build workspace dependencies before running jobs.\n * Set to `false` to skip the build step entirely.\n * Default: \"turbo build\"\n */\n buildCommand?: string | false;\n}\n\n/** Identity function for type-safe runner config definition */\nexport function defineRunnerConfig(config: RunnerConfig): RunnerConfig {\n return config;\n}\n\n/** Identity function for type-safe environment definition */\nexport function defineRunnerEnv(options: RunnerEnvOptions): RunnerEnvOptions {\n return options;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"config.mjs","names":[],"sources":["../src/config.ts"],"sourcesContent":["import path from \"node:path\";\n\n/** Environment configuration for a specific deployment target */\nexport interface RunnerEnvOptions {\n /** GCP project ID — sets GOOGLE_CLOUD_PROJECT automatically */\n project: string;\n /**\n * Path(s) to .env files to load, resolved relative to the service directory.\n * Variables from these files have lower precedence than explicit `env` values.\n * When multiple files are specified, earlier files take precedence over later ones.\n */\n envFile?: string | string[];\n /** Additional environment variables to set before the job runs */\n env?: Record<string, string>;\n /** Secret names to load from GCP Secret Manager */\n secrets?: string[];\n /**\n * Destination for artifacts written via `getExportsWriter()`.\n *\n * Either a local path (resolved relative to the service directory) or a\n * `gs://bucket[/prefix]` URI. For most setups this is a `gs://` URI — one\n * per environment — paired with a top-level `localExportsPath` that\n * catches every local run regardless of which environment is selected.\n *\n * When unset, `getExportsWriter()` throws (unless `localExportsPath` is\n * set and the run is local). Cloud deployments require a `gs://` URI.\n *\n * @see RunnerConfig.localExportsPath\n */\n exportsPath?: string;\n}\n\n/** Container resource limits for a Cloud Run Job */\nexport interface CloudResources {\n /** Memory limit (e.g., \"512Mi\", \"1Gi\"). Default: \"512Mi\" */\n memory?: string;\n /** CPU limit (e.g., \"1\", \"2\"). Default: \"1\" */\n cpu?: string;\n /** Job timeout in seconds. Default: 86400 (24 hours) */\n timeout?: number;\n /** Maximum number of tasks that can run in parallel. Default: unset (no limit) */\n parallelism?: number;\n}\n\n/** Direct VPC egress configuration for private network access (e.g., Redis) */\nexport interface CloudNetworkConfig {\n /** VPC network name (e.g., \"default\") */\n name: string;\n /** VPC subnet name (e.g., \"default\") */\n subnet?: string;\n /** VPC egress mode. Default: \"private-ranges-only\" */\n egress?: \"all-traffic\" | \"private-ranges-only\";\n}\n\n/** Configuration for Cloud Run Jobs execution */\nexport interface CloudConfig {\n /** Cloud Run Job name (e.g., \"loads-predictions-jobs\") */\n name: string;\n /** GCP region. Default: \"us-central1\" */\n region?: string;\n /** Artifact Registry repository name. Default: \"cloud-run\" */\n artifactRegistry?: string;\n /** Container resource limits */\n resources?: CloudResources;\n /** Service account email for the Cloud Run Job */\n serviceAccount?: string;\n /**\n * Build Docker images locally instead of using Cloud Build.\n * Requires Docker to be installed and running. Default: true.\n */\n buildLocal?: boolean;\n /** Direct VPC egress configuration for private network access */\n network?: CloudNetworkConfig;\n}\n\n/** Full runner configuration provided by each service */\nexport interface RunnerConfig {\n /**\n * Absolute path to the directory containing job scripts.\n * Default: `dist/jobs` relative to cwd.\n */\n jobsDirectory?: string;\n /** Optional initialization function called before the job runs (skipped for --help) */\n initialize?: () => void | Promise<void>;\n /** Optional custom logger (defaults to console) */\n logger?: {\n info: (message: string) => void;\n error: (message: string) => void;\n };\n /** Named environments (e.g., stag, prod) */\n environments: Record<string, RunnerEnvOptions>;\n /**\n * Destination used by `getExportsWriter()` for every local run, regardless\n * of which environment is selected. Resolved relative to the service\n * directory; `gs://bucket[/prefix]` URIs are also accepted.\n *\n * When set, local runs ignore the environment's `exportsPath`. When unset,\n * local runs fall back to the environment's `exportsPath`.\n *\n * Cloud runs always use the environment's `exportsPath`.\n */\n localExportsPath?: string;\n /** Cloud Run Jobs configuration (required for `job cloud run/deploy` commands) */\n cloud?: CloudConfig;\n /**\n * Command to build workspace dependencies before running jobs.\n * Set to `false` to skip the build step entirely.\n * Default: \"turbo build\"\n */\n buildCommand?: string | false;\n}\n\n/** Identity function for type-safe runner config definition */\nexport function defineRunnerConfig(config: RunnerConfig): RunnerConfig {\n return config;\n}\n\n/** Identity function for type-safe environment definition */\nexport function defineRunnerEnv(options: RunnerEnvOptions): RunnerEnvOptions {\n return options;\n}\n\n/**\n * Resolve the exports destination for a local run. Prefers the top-level\n * `localExportsPath` over the environment's `exportsPath`. Local paths are\n * resolved against the service directory; `gs://` URIs pass through\n * unchanged.\n */\nexport function resolveLocalExportsPath(\n config: Pick<RunnerConfig, \"localExportsPath\">,\n envConfig: Pick<RunnerEnvOptions, \"exportsPath\">,\n serviceDirectory: string,\n): string | undefined {\n const raw = config.localExportsPath ?? envConfig.exportsPath;\n if (!raw) return undefined;\n return raw.startsWith(\"gs://\") ? raw : path.resolve(serviceDirectory, raw);\n}\n"],"mappings":";;;;AAiHA,SAAgB,mBAAmB,QAAoC;AACrE,QAAO;;;AAIT,SAAgB,gBAAgB,SAA6C;AAC3E,QAAO;;;;;;;;AAST,SAAgB,wBACd,QACA,WACA,kBACoB;CACpB,MAAM,MAAM,OAAO,oBAAoB,UAAU;AACjD,KAAI,CAAC,IAAK,QAAO;AACjB,QAAO,IAAI,WAAW,QAAQ,GAAG,MAAM,KAAK,QAAQ,kBAAkB,IAAI"}
|
package/dist/discover-jobs.mjs
CHANGED
package/dist/env-file.mjs
CHANGED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region src/exports.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Writer for job artifacts, produced by `getExportsWriter()`. A single writer
|
|
4
|
+
* instance targets a destination configured via the `exportsPath` runner
|
|
5
|
+
* option (local directory or `gs://` URI). All methods return the resolved
|
|
6
|
+
* absolute path or `gs://` URI of the written artifact.
|
|
7
|
+
*/
|
|
8
|
+
interface ExportsWriter {
|
|
9
|
+
/**
|
|
10
|
+
* Write a value as pretty-printed JSON (2-space indent, trailing newline).
|
|
11
|
+
* The `.json` extension is added if missing.
|
|
12
|
+
*/
|
|
13
|
+
writeJson(relativePath: string, data: unknown): Promise<string>;
|
|
14
|
+
/** Write a UTF-8 string (e.g. CSV, SVG, plain text). */
|
|
15
|
+
writeText(relativePath: string, content: string): Promise<string>;
|
|
16
|
+
/** Write a binary buffer. */
|
|
17
|
+
writeBuffer(relativePath: string, content: Buffer | Uint8Array): Promise<string>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Return a writer that persists artifacts to the destination configured via
|
|
21
|
+
* the `exportsPath` runner option. Local paths are used for local execution;
|
|
22
|
+
* `gs://bucket[/prefix]` URIs are used for Cloud Run deployments.
|
|
23
|
+
*
|
|
24
|
+
* Throws when no destination is configured.
|
|
25
|
+
*/
|
|
26
|
+
declare function getExportsWriter(): ExportsWriter;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { ExportsWriter, getExportsWriter };
|
|
29
|
+
//# sourceMappingURL=exports.d.mts.map
|
package/dist/exports.mjs
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { consola } from "consola";
|
|
3
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
4
|
+
|
|
5
|
+
//#region src/exports.ts
|
|
6
|
+
const GCS_PREFIX = "gs://";
|
|
7
|
+
/**
|
|
8
|
+
* Return a writer that persists artifacts to the destination configured via
|
|
9
|
+
* the `exportsPath` runner option. Local paths are used for local execution;
|
|
10
|
+
* `gs://bucket[/prefix]` URIs are used for Cloud Run deployments.
|
|
11
|
+
*
|
|
12
|
+
* Throws when no destination is configured.
|
|
13
|
+
*/
|
|
14
|
+
function getExportsWriter() {
|
|
15
|
+
const destination = process.env.JOB_EXPORTS_PATH;
|
|
16
|
+
if (!destination) throw new Error("No exports destination configured.\nSet `localExportsPath` or the current environment's `exportsPath` in your job-runner.config.ts, or set JOB_EXPORTS_PATH directly in the environment.");
|
|
17
|
+
if (destination.startsWith(GCS_PREFIX)) return createGcsWriter(destination);
|
|
18
|
+
return createLocalWriter(destination);
|
|
19
|
+
}
|
|
20
|
+
function createLocalWriter(basePath) {
|
|
21
|
+
const resolvedBase = path.resolve(basePath);
|
|
22
|
+
/** Takes an already-sanitized relative path and writes the content. */
|
|
23
|
+
async function write(safeRelative, content) {
|
|
24
|
+
const fullPath = path.join(resolvedBase, safeRelative);
|
|
25
|
+
await mkdir(path.dirname(fullPath), { recursive: true });
|
|
26
|
+
await writeFile(fullPath, content);
|
|
27
|
+
consola.info(`Export written: ${fullPath}`);
|
|
28
|
+
return fullPath;
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
async writeJson(relativePath, data) {
|
|
32
|
+
return write(ensureExtension(sanitizeRelativePath(relativePath), ".json"), formatJson(data));
|
|
33
|
+
},
|
|
34
|
+
async writeText(relativePath, content) {
|
|
35
|
+
return write(sanitizeRelativePath(relativePath), content);
|
|
36
|
+
},
|
|
37
|
+
async writeBuffer(relativePath, content) {
|
|
38
|
+
return write(sanitizeRelativePath(relativePath), content);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function createGcsWriter(uri) {
|
|
43
|
+
const target = parseGcsUri(uri);
|
|
44
|
+
/** Takes an already-sanitized relative path and uploads the content. */
|
|
45
|
+
async function write(safeRelative, content, contentType) {
|
|
46
|
+
const objectName = joinGcsPath(target.prefix, safeRelative);
|
|
47
|
+
const fullUri = `${GCS_PREFIX}${target.bucket}/${objectName}`;
|
|
48
|
+
const file = (await getStorageClient()).bucket(target.bucket).file(objectName);
|
|
49
|
+
const body = typeof content === "string" || Buffer.isBuffer(content) ? content : Buffer.from(content);
|
|
50
|
+
await file.save(body, {
|
|
51
|
+
contentType,
|
|
52
|
+
resumable: false
|
|
53
|
+
});
|
|
54
|
+
consola.info(`Export written: ${fullUri}`);
|
|
55
|
+
return fullUri;
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
async writeJson(relativePath, data) {
|
|
59
|
+
return write(ensureExtension(sanitizeRelativePath(relativePath), ".json"), formatJson(data), "application/json");
|
|
60
|
+
},
|
|
61
|
+
async writeText(relativePath, content) {
|
|
62
|
+
const safe = sanitizeRelativePath(relativePath);
|
|
63
|
+
return write(safe, content, contentTypeFor(safe));
|
|
64
|
+
},
|
|
65
|
+
async writeBuffer(relativePath, content) {
|
|
66
|
+
return write(sanitizeRelativePath(relativePath), content, "application/octet-stream");
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Lazy Storage client singleton. The `@google-cloud/storage` module is only
|
|
72
|
+
* loaded when a `gs://` destination is actually used for a write.
|
|
73
|
+
*/
|
|
74
|
+
let storageClient = null;
|
|
75
|
+
async function getStorageClient() {
|
|
76
|
+
if (storageClient) return storageClient;
|
|
77
|
+
const { Storage: StorageCtor } = await import("@google-cloud/storage");
|
|
78
|
+
storageClient = new StorageCtor();
|
|
79
|
+
return storageClient;
|
|
80
|
+
}
|
|
81
|
+
function parseGcsUri(uri) {
|
|
82
|
+
const withoutScheme = uri.slice(5);
|
|
83
|
+
const slashIndex = withoutScheme.indexOf("/");
|
|
84
|
+
if (slashIndex === -1) {
|
|
85
|
+
if (!withoutScheme) throw new Error(`Invalid GCS URI: "${uri}" (missing bucket name)`);
|
|
86
|
+
return {
|
|
87
|
+
bucket: withoutScheme,
|
|
88
|
+
prefix: ""
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
const bucket = withoutScheme.slice(0, slashIndex);
|
|
92
|
+
const prefix = withoutScheme.slice(slashIndex + 1).replace(/\/+$/, "");
|
|
93
|
+
if (!bucket) throw new Error(`Invalid GCS URI: "${uri}" (missing bucket name)`);
|
|
94
|
+
return {
|
|
95
|
+
bucket,
|
|
96
|
+
prefix
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function joinGcsPath(prefix, relative) {
|
|
100
|
+
const normalizedRelative = relative.replace(/^\/+/, "");
|
|
101
|
+
return prefix ? `${prefix}/${normalizedRelative}` : normalizedRelative;
|
|
102
|
+
}
|
|
103
|
+
function sanitizeRelativePath(relativePath) {
|
|
104
|
+
if (!relativePath || relativePath.trim() === "") throw new Error("Export path is empty");
|
|
105
|
+
if (path.isAbsolute(relativePath) || relativePath.startsWith("/")) throw new Error(`Export path must be relative, got "${relativePath}". Absolute paths are not allowed.`);
|
|
106
|
+
const normalized = path.posix.normalize(relativePath.replace(/\\/g, "/"));
|
|
107
|
+
/**
|
|
108
|
+
* Reject `..` only as a distinct path segment, not as a prefix. A filename
|
|
109
|
+
* like "..hidden" is a legitimate dotfile variant, while ".." / "../x" /
|
|
110
|
+
* "a/../../x" all produce a `..` segment after normalization.
|
|
111
|
+
*/
|
|
112
|
+
if (normalized.split("/").includes("..")) throw new Error(`Export path must not traverse upward, got "${relativePath}".`);
|
|
113
|
+
return normalized;
|
|
114
|
+
}
|
|
115
|
+
function ensureExtension(relativePath, extension) {
|
|
116
|
+
return relativePath.endsWith(extension) ? relativePath : `${relativePath}${extension}`;
|
|
117
|
+
}
|
|
118
|
+
function formatJson(data) {
|
|
119
|
+
return `${JSON.stringify(data, null, 2)}\n`;
|
|
120
|
+
}
|
|
121
|
+
const TEXT_CONTENT_TYPES = {
|
|
122
|
+
".csv": "text/csv; charset=utf-8",
|
|
123
|
+
".json": "application/json",
|
|
124
|
+
".svg": "image/svg+xml",
|
|
125
|
+
".txt": "text/plain; charset=utf-8",
|
|
126
|
+
".html": "text/html; charset=utf-8",
|
|
127
|
+
".xml": "application/xml",
|
|
128
|
+
".yaml": "application/yaml",
|
|
129
|
+
".yml": "application/yaml",
|
|
130
|
+
".md": "text/markdown; charset=utf-8"
|
|
131
|
+
};
|
|
132
|
+
function contentTypeFor(relativePath) {
|
|
133
|
+
return TEXT_CONTENT_TYPES[path.posix.extname(relativePath).toLowerCase()] ?? "text/plain; charset=utf-8";
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
//#endregion
|
|
137
|
+
export { getExportsWriter };
|
|
138
|
+
//# sourceMappingURL=exports.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exports.mjs","names":[],"sources":["../src/exports.ts"],"sourcesContent":["import { mkdir, writeFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { consola } from \"consola\";\nimport type { Storage } from \"@google-cloud/storage\";\n\n/**\n * Writer for job artifacts, produced by `getExportsWriter()`. A single writer\n * instance targets a destination configured via the `exportsPath` runner\n * option (local directory or `gs://` URI). All methods return the resolved\n * absolute path or `gs://` URI of the written artifact.\n */\nexport interface ExportsWriter {\n /**\n * Write a value as pretty-printed JSON (2-space indent, trailing newline).\n * The `.json` extension is added if missing.\n */\n writeJson(relativePath: string, data: unknown): Promise<string>;\n /** Write a UTF-8 string (e.g. CSV, SVG, plain text). */\n writeText(relativePath: string, content: string): Promise<string>;\n /** Write a binary buffer. */\n writeBuffer(\n relativePath: string,\n content: Buffer | Uint8Array,\n ): Promise<string>;\n}\n\nconst GCS_PREFIX = \"gs://\";\n\n/**\n * Return a writer that persists artifacts to the destination configured via\n * the `exportsPath` runner option. Local paths are used for local execution;\n * `gs://bucket[/prefix]` URIs are used for Cloud Run deployments.\n *\n * Throws when no destination is configured.\n */\nexport function getExportsWriter(): ExportsWriter {\n const destination = process.env.JOB_EXPORTS_PATH;\n\n if (!destination) {\n throw new Error(\n \"No exports destination configured.\\n\" +\n \"Set `localExportsPath` or the current environment's `exportsPath` \" +\n \"in your job-runner.config.ts, or set JOB_EXPORTS_PATH directly in \" +\n \"the environment.\",\n );\n }\n\n if (destination.startsWith(GCS_PREFIX)) {\n return createGcsWriter(destination);\n }\n\n return createLocalWriter(destination);\n}\n\nfunction createLocalWriter(basePath: string): ExportsWriter {\n const resolvedBase = path.resolve(basePath);\n\n /** Takes an already-sanitized relative path and writes the content. */\n async function write(\n safeRelative: string,\n content: string | Uint8Array,\n ): Promise<string> {\n const fullPath = path.join(resolvedBase, safeRelative);\n\n await mkdir(path.dirname(fullPath), { recursive: true });\n await writeFile(fullPath, content);\n\n consola.info(`Export written: ${fullPath}`);\n return fullPath;\n }\n\n return {\n async writeJson(relativePath, data) {\n /** Sanitize before appending the extension so \"\" doesn't become \".json\". */\n const withExtension = ensureExtension(\n sanitizeRelativePath(relativePath),\n \".json\",\n );\n return write(withExtension, formatJson(data));\n },\n async writeText(relativePath, content) {\n return write(sanitizeRelativePath(relativePath), content);\n },\n async writeBuffer(relativePath, content) {\n return write(sanitizeRelativePath(relativePath), content);\n },\n };\n}\n\ninterface GcsTarget {\n bucket: string;\n prefix: string;\n}\n\nfunction createGcsWriter(uri: string): ExportsWriter {\n const target = parseGcsUri(uri);\n\n /** Takes an already-sanitized relative path and uploads the content. */\n async function write(\n safeRelative: string,\n content: string | Buffer | Uint8Array,\n contentType: string,\n ): Promise<string> {\n const objectName = joinGcsPath(target.prefix, safeRelative);\n const fullUri = `${GCS_PREFIX}${target.bucket}/${objectName}`;\n\n const storage = await getStorageClient();\n const file = storage.bucket(target.bucket).file(objectName);\n\n const body =\n typeof content === \"string\" || Buffer.isBuffer(content)\n ? content\n : Buffer.from(content);\n\n await file.save(body, {\n contentType,\n resumable: false,\n });\n\n consola.info(`Export written: ${fullUri}`);\n return fullUri;\n }\n\n return {\n async writeJson(relativePath, data) {\n /** Sanitize before appending the extension so \"\" doesn't become \".json\". */\n const withExtension = ensureExtension(\n sanitizeRelativePath(relativePath),\n \".json\",\n );\n return write(withExtension, formatJson(data), \"application/json\");\n },\n async writeText(relativePath, content) {\n const safe = sanitizeRelativePath(relativePath);\n return write(safe, content, contentTypeFor(safe));\n },\n async writeBuffer(relativePath, content) {\n return write(\n sanitizeRelativePath(relativePath),\n content,\n \"application/octet-stream\",\n );\n },\n };\n}\n\n/**\n * Lazy Storage client singleton. The `@google-cloud/storage` module is only\n * loaded when a `gs://` destination is actually used for a write.\n */\nlet storageClient: Storage | null = null;\nasync function getStorageClient(): Promise<Storage> {\n if (storageClient) return storageClient;\n const { Storage: StorageCtor } = await import(\"@google-cloud/storage\");\n storageClient = new StorageCtor();\n return storageClient;\n}\n\nfunction parseGcsUri(uri: string): GcsTarget {\n const withoutScheme = uri.slice(GCS_PREFIX.length);\n const slashIndex = withoutScheme.indexOf(\"/\");\n\n if (slashIndex === -1) {\n if (!withoutScheme) {\n throw new Error(`Invalid GCS URI: \"${uri}\" (missing bucket name)`);\n }\n return { bucket: withoutScheme, prefix: \"\" };\n }\n\n const bucket = withoutScheme.slice(0, slashIndex);\n const prefix = withoutScheme.slice(slashIndex + 1).replace(/\\/+$/, \"\");\n\n if (!bucket) {\n throw new Error(`Invalid GCS URI: \"${uri}\" (missing bucket name)`);\n }\n\n return { bucket, prefix };\n}\n\nfunction joinGcsPath(prefix: string, relative: string): string {\n const normalizedRelative = relative.replace(/^\\/+/, \"\");\n return prefix ? `${prefix}/${normalizedRelative}` : normalizedRelative;\n}\n\nfunction sanitizeRelativePath(relativePath: string): string {\n if (!relativePath || relativePath.trim() === \"\") {\n throw new Error(\"Export path is empty\");\n }\n\n if (path.isAbsolute(relativePath) || relativePath.startsWith(\"/\")) {\n throw new Error(\n `Export path must be relative, got \"${relativePath}\". ` +\n \"Absolute paths are not allowed.\",\n );\n }\n\n const normalized = path.posix.normalize(relativePath.replace(/\\\\/g, \"/\"));\n\n /**\n * Reject `..` only as a distinct path segment, not as a prefix. A filename\n * like \"..hidden\" is a legitimate dotfile variant, while \"..\" / \"../x\" /\n * \"a/../../x\" all produce a `..` segment after normalization.\n */\n if (normalized.split(\"/\").includes(\"..\")) {\n throw new Error(\n `Export path must not traverse upward, got \"${relativePath}\".`,\n );\n }\n\n return normalized;\n}\n\nfunction ensureExtension(relativePath: string, extension: string): string {\n return relativePath.endsWith(extension)\n ? relativePath\n : `${relativePath}${extension}`;\n}\n\nfunction formatJson(data: unknown): string {\n return `${JSON.stringify(data, null, 2)}\\n`;\n}\n\nconst TEXT_CONTENT_TYPES: Record<string, string> = {\n \".csv\": \"text/csv; charset=utf-8\",\n \".json\": \"application/json\",\n \".svg\": \"image/svg+xml\",\n \".txt\": \"text/plain; charset=utf-8\",\n \".html\": \"text/html; charset=utf-8\",\n \".xml\": \"application/xml\",\n \".yaml\": \"application/yaml\",\n \".yml\": \"application/yaml\",\n \".md\": \"text/markdown; charset=utf-8\",\n};\n\nfunction contentTypeFor(relativePath: string): string {\n const ext = path.posix.extname(relativePath).toLowerCase();\n return TEXT_CONTENT_TYPES[ext] ?? \"text/plain; charset=utf-8\";\n}\n"],"mappings":";;;;;AA0BA,MAAM,aAAa;;;;;;;;AASnB,SAAgB,mBAAkC;CAChD,MAAM,cAAc,QAAQ,IAAI;AAEhC,KAAI,CAAC,YACH,OAAM,IAAI,MACR,2LAID;AAGH,KAAI,YAAY,WAAW,WAAW,CACpC,QAAO,gBAAgB,YAAY;AAGrC,QAAO,kBAAkB,YAAY;;AAGvC,SAAS,kBAAkB,UAAiC;CAC1D,MAAM,eAAe,KAAK,QAAQ,SAAS;;CAG3C,eAAe,MACb,cACA,SACiB;EACjB,MAAM,WAAW,KAAK,KAAK,cAAc,aAAa;AAEtD,QAAM,MAAM,KAAK,QAAQ,SAAS,EAAE,EAAE,WAAW,MAAM,CAAC;AACxD,QAAM,UAAU,UAAU,QAAQ;AAElC,UAAQ,KAAK,mBAAmB,WAAW;AAC3C,SAAO;;AAGT,QAAO;EACL,MAAM,UAAU,cAAc,MAAM;AAMlC,UAAO,MAJe,gBACpB,qBAAqB,aAAa,EAClC,QACD,EAC2B,WAAW,KAAK,CAAC;;EAE/C,MAAM,UAAU,cAAc,SAAS;AACrC,UAAO,MAAM,qBAAqB,aAAa,EAAE,QAAQ;;EAE3D,MAAM,YAAY,cAAc,SAAS;AACvC,UAAO,MAAM,qBAAqB,aAAa,EAAE,QAAQ;;EAE5D;;AAQH,SAAS,gBAAgB,KAA4B;CACnD,MAAM,SAAS,YAAY,IAAI;;CAG/B,eAAe,MACb,cACA,SACA,aACiB;EACjB,MAAM,aAAa,YAAY,OAAO,QAAQ,aAAa;EAC3D,MAAM,UAAU,GAAG,aAAa,OAAO,OAAO,GAAG;EAGjD,MAAM,QADU,MAAM,kBAAkB,EACnB,OAAO,OAAO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