UNPKG

@catladder/cli

Version:

Panter cli tool for cloud CI/CD and DevOps

71 lines (70 loc) 2.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOnDeployExecuteScript = void 0; const utils_1 = require("../../../../utils"); const createArgsString_1 = require("../../utils/createArgsString"); const getJobOrServiceArgs_1 = require("../../utils/getJobOrServiceArgs"); const jobName_1 = require("../../utils/jobName"); const cloudRunJobs_1 = require("../cloudRunJobs"); const common_1 = require("../common"); const getOnDeployExecuteScript = (context, when) => { const executes = getExecutes(context); return executes.filter(config => config.when === when).flatMap(execute => { return getJobRunScriptForExecute(context, execute); }); }; exports.getOnDeployExecuteScript = getOnDeployExecuteScript; const getExecutes = context => { var _a; const deployConfig = (0, common_1.getCloudRunDeployConfig)(context); return [...getLegacyExecutes(context), ...Object.entries((_a = deployConfig.execute) !== null && _a !== void 0 ? _a : {}).flatMap(([key, value]) => { // remove all schedule executes if (!value) { return []; } return [value]; })]; }; const getLegacyExecutes = context => { const jobsWithNames = (0, cloudRunJobs_1.getCloudRunJobsWithNames)(context); return jobsWithNames.flatMap(({ jobName, job }) => { if (!job.when || !["preDeploy", "postDeploy", "preStop", "postStop"].includes(job.when)) { return []; } return [{ job: jobName, type: "job", when: job.when, ...(job.when === "preDeploy" || job.when === "postDeploy" ? { waitForCompletion: job.waitForCompletion } : {}) }]; }); }; const getJobRunScriptForExecute = (context, config) => { var _a; const type = config.type; if (type === "script") { return (0, utils_1.ensureArray)(config.script); } else if (type === "job") { const commonArgs = (0, common_1.getCommonCloudRunArgs)(context); // always wait for completion for preStop and postStop jobs // since stop will delete the jobs afterwards, so they will fail const waitForCompletion = ["preStop", "postStop"].includes(config.when) ? true // always : "waitForCompletion" in config ? (_a = config.waitForCompletion) !== null && _a !== void 0 ? _a : false // depends on config : false; const argString = (0, createArgsString_1.createArgsString)({ ...commonArgs, wait: waitForCompletion === true ? true : undefined, args: (0, getJobOrServiceArgs_1.getCloudRunServiceOrJobArgsArg)(config.args) }); const fullJobName = (0, jobName_1.getFullJobName)(context, config.job); return [`${(0, common_1.gcloudRunCmd)()} jobs execute ${fullJobName.toString()} ${argString}`]; } throw new Error(`unsupported execute type: ${type}`); };