@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
90 lines (89 loc) • 4.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.startBuildAndRun = void 0;
const log_1 = require("diginext-utils/dist/xconsole/log");
const inquirer_1 = __importDefault(require("inquirer"));
const lodash_1 = require("lodash");
const plugins_1 = require("../../plugins");
const app_helper_1 = require("../apps/app-helper");
const ask_project_and_app_1 = require("../apps/ask-project-and-app");
const update_config_1 = require("../apps/update-config");
const update_git_config_1 = require("../apps/update-git-config");
const ask_for_domain_1 = require("./ask-for-domain");
const generate_build_tag_1 = require("./generate-build-tag");
const start_build_1 = require("./start-build");
const startBuildAndRun = async (options) => {
if (!options.targetDirectory)
options.targetDirectory = process.cwd();
const { env = "dev", targetDirectory, author } = options;
let { app } = await (0, ask_project_and_app_1.askForProjectAndApp)(options.targetDirectory, options);
if (!app.git || !app.git.provider || !app.git.repoSSH || !app.git.repoURL) {
const gitInfo = await (0, plugins_1.getCurrentGitRepoData)(options.targetDirectory);
if (!gitInfo)
throw new Error(`This app's directory doesn't have any git remote integrated.`);
app = await (0, update_git_config_1.updateAppGitInfo)(app, { provider: gitInfo.provider, repoSSH: gitInfo.repoSSH, repoURL: gitInfo.repoURL });
}
let appConfig = (0, app_helper_1.getAppConfigFromApp)(app);
const { project, slug } = appConfig;
const deployEnvironment = appConfig.deployEnvironment[env];
let domains, selectedSSL = "letsencrypt", selectedSecretName;
// try to find any "Dockerfile" to build, it it's not existed, throw error!
const dockerFile = (0, plugins_1.resolveDockerfilePath)({ targetDirectory, env });
if (!dockerFile)
return;
// ask for generated domains:
try {
domains = await (0, ask_for_domain_1.askForDomain)(env, project, slug, deployEnvironment, { user: author });
}
catch (e) {
(0, log_1.logError)(`[BUILD_AND_RUN] ${e}`);
return;
}
if ((0, lodash_1.isEmpty)(domains)) {
domains = [];
(0, log_1.logWarn)(`This app doesn't have any domains configurated & only visible to the namespace scope, you can add your own domain to app config on Diginext workspace to expose this app to the internet anytime.`);
}
appConfig.deployEnvironment[env].domains = domains;
// if they have any domains, ask if they want to use "letsencrypt":
if (appConfig.deployEnvironment[env].domains.length > 0 && !appConfig.deployEnvironment[env].ssl) {
const askSSL = await inquirer_1.default.prompt({
type: "list",
name: "selectedSSL",
message: `Which SSL certificate do you want to use for these domains?`,
default: "letsencrypt",
choices: ["letsencrypt", "custom", "none"],
});
selectedSSL = askSSL.selectedSSL;
}
selectedSecretName = `tls-secret-${selectedSSL}-${appConfig.project}-${appConfig.slug}`;
// if they select "custom" SSL certificate -> ask for secret name:
if (selectedSSL == "custom") {
const askSecretName = await inquirer_1.default.prompt({
type: "input",
name: "secretName",
message: `Name your custom SSL secret (ENTER to use default):`,
default: selectedSecretName,
});
selectedSecretName = askSecretName.secretName;
}
appConfig.deployEnvironment[env].ssl = selectedSSL;
appConfig.deployEnvironment[env].tlsSecret = selectedSecretName;
// save domains & SSL configs
appConfig = await (0, update_config_1.updateAppConfig)(app, env, appConfig.deployEnvironment[env]);
/**
* Generate build number as docker image tag
*/
const { imageURL, namespace } = appConfig.deployEnvironment[env];
const tagInfo = await (0, generate_build_tag_1.generateBuildTagBySourceDir)(options.targetDirectory, { branch: options.gitBranch });
options.slug = appConfig.slug; // ! required
options.projectSlug = appConfig.project; // ! required
options.namespace = namespace; // ! required
options.buildTag = tagInfo.tag; // ! required
options.buildImage = `${imageURL}:${options.buildTag}`; // ! required
const buildStatus = await (0, start_build_1.startBuildV1)(options, { shouldRollout: true });
return buildStatus;
};
exports.startBuildAndRun = startBuildAndRun;