@catladder/cli
Version:
Panter cli tool for cloud CI/CD and DevOps
89 lines • 3.45 kB
JavaScript
;
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 [
{
jobName: key,
config: 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 [
{
jobName,
config: {
job: jobName,
type: "job",
when: job.when,
...(job.when === "preDeploy" || job.when === "postDeploy"
? {
waitForCompletion: job.waitForCompletion,
}
: {}),
},
},
];
});
};
const getJobRunScriptForExecute = (context, { jobName, 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, jobName);
return [
`${(0, common_1.gcloudRunCmd)()} jobs execute ${fullJobName.toString()} ${argString}`,
];
}
throw new Error(`unsupported execute type: ${type}`);
};
//# sourceMappingURL=onDeploy.js.map