@catladder/cli
Version:
Panter cli tool for cloud CI/CD and DevOps
162 lines • 7.89 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasDockerfile = exports.getDockerBuildDefaultScript = exports.getDockerBuildScriptWithBuiltInDockerFile = exports.gitlabDockerLogin = exports.createDockerBuildJobBase = exports.getDockerJobBaseProps = exports.DOCKER_BUILD_JOB_NAME = exports.getDockerBuildVariables = exports.requiresDockerBuild = exports.getDockerImageVariables = void 0;
const fs_1 = require("fs");
const lodash_1 = require("lodash");
const path_1 = __importDefault(require("path"));
const deploy_1 = require("../deploy");
const artifactsRegistry_1 = require("../deploy/cloudRun/artifactsRegistry");
const gcloudServiceAccountLoginCommands_1 = require("../deploy/cloudRun/utils/gcloudServiceAccountLoginCommands");
const runner_1 = require("../runner");
const gitlab_1 = require("../utils/gitlab");
const createJobCache_1 = require("./cache/createJobCache");
const DOCKER_BUILD_RUNNER_REQUESTS = {
KUBERNETES_CPU_REQUEST: "0.45",
KUBERNETES_MEMORY_REQUEST: "1Gi",
KUBERNETES_MEMORY_LIMIT: "2Gi",
};
const getDockerImageVariables = (context) => {
var _a;
const deployConfig = (_a = context.deploy) === null || _a === void 0 ? void 0 : _a.config;
return {
...((0, deploy_1.isOfDeployType)(deployConfig, "google-cloudrun")
? {
DOCKER_REGISTRY: (0, artifactsRegistry_1.getArtifactsRegistryHost)(context),
DOCKER_IMAGE: (0, artifactsRegistry_1.getArtifactsRegistryImageName)(context),
DOCKER_CACHE_IMAGE: (0, artifactsRegistry_1.getArtifactsRegistryBuildCacheImage)(context),
}
: // gitlab registry:
{
DOCKER_REGISTRY: "$CI_REGISTRY",
DOCKER_CACHE_IMAGE: "$CI_REGISTRY_IMAGE/caches/" + context.name,
// ONLY USED IN KUBERNETES
DOCKER_IMAGE_NAME: context.env + "/" + context.name,
DOCKER_IMAGE: "$CI_REGISTRY_IMAGE/$DOCKER_IMAGE_NAME",
}),
DOCKER_IMAGE_TAG: "$CI_COMMIT_SHA",
};
};
exports.getDockerImageVariables = getDockerImageVariables;
/**
* Weather the context requires a docker build
*/
const requiresDockerBuild = (context) => {
var _a;
const deployConfig = (_a = context.deploy) === null || _a === void 0 ? void 0 : _a.config;
return ((0, deploy_1.isOfDeployType)(deployConfig, "kubernetes", "google-cloudrun", "dockerTag") ||
((0, deploy_1.isOfDeployType)(deployConfig, "custom") && deployConfig.requiresDocker));
};
exports.requiresDockerBuild = requiresDockerBuild;
// those need to be runner variables
const getDockerBuildRunnerVariables = () => ({
DOCKER_HOST: "tcp://docker:2375",
DOCKER_TLS_CERTDIR: "",
DOCKER_DRIVER: "overlay2",
DOCKER_BUILDKIT: "1", // see https://docs.docker.com/develop/develop-images/build_enhancements/
});
const getDockerAdditions = (build) => {
var _a, _b;
if (!("docker" in build))
return {};
if (!build.docker)
return {};
return {
DOCKERFILE_ADDITIONS: "additionsBegin" in build.docker
? (_a = build.docker.additionsBegin) === null || _a === void 0 ? void 0 : _a.join("\n")
: undefined,
DOCKERFILE_ADDITIONS_END: "additionsEnd" in build.docker
? (_b = build.docker.additionsEnd) === null || _b === void 0 ? void 0 : _b.join("\n")
: undefined,
};
};
const getDockerBuildVariables = (context) => {
var _a;
return {
...getDockerAdditions(context.build.config),
APP_DIR: context.build.dir,
DOCKER_BUILD_CONTEXT: "docker" in context.build.config &&
context.build.config.docker &&
"buildContextLocation" in context.build.config.docker &&
((_a = context.build.config.docker) === null || _a === void 0 ? void 0 : _a.buildContextLocation) === "component"
? context.build.dir
: ".",
...(0, exports.getDockerImageVariables)(context),
};
};
exports.getDockerBuildVariables = getDockerBuildVariables;
exports.DOCKER_BUILD_JOB_NAME = "🔨 docker";
const getDockerJobBaseProps = () => {
return {
image: (0, runner_1.getRunnerImage)("docker-build"),
services: [
{
name: "docker:24.0.6-dind", // see see https://gitlab.com/gitlab-org/gitlab-runner/-/issues/27300#note_466755332
command: ["--tls=false", "--registry-mirror=https://mirror.gcr.io"],
},
],
variables: {},
runnerVariables: getDockerBuildRunnerVariables(),
};
};
exports.getDockerJobBaseProps = getDockerJobBaseProps;
const createDockerBuildJobBase = (context, { script, cache, ...def }) => {
return (0, lodash_1.merge)({
name: exports.DOCKER_BUILD_JOB_NAME,
envMode: "jobPerEnv",
stage: "build",
cache: cache ? (0, createJobCache_1.createJobCacheFromCacheConfigs)(context, cache) : undefined,
...(0, exports.getDockerJobBaseProps)(),
script: script || [],
}, {
variables: (0, exports.getDockerBuildVariables)(context),
runnerVariables: {
...DOCKER_BUILD_RUNNER_REQUESTS,
...getDockerBuildRunnerVariables(),
},
}, def);
};
exports.createDockerBuildJobBase = createDockerBuildJobBase;
const gitlabDockerLogin = (context) => context.deploy && (0, deploy_1.isOfDeployType)(context.deploy.config, "google-cloudrun")
? [
...(0, gcloudServiceAccountLoginCommands_1.gcloudServiceAccountLoginCommands)(context),
`gcloud auth configure-docker ${(0, artifactsRegistry_1.getArtifactsRegistryHost)(context)}`,
]
: [
"docker login --username gitlab-ci-token --password $CI_JOB_TOKEN $CI_REGISTRY",
];
exports.gitlabDockerLogin = gitlabDockerLogin;
const BUILT_IN_ENSURE_DOCKERFILE_SCRIPTS = {
meteor: "ensureMeteorDockerfile",
node: "ensureNodeDockerfile",
nginx: "ensureNginxDockerfile",
custom: null,
};
const getDockerBuildScriptWithBuiltInDockerFile = (context, defaultType) => {
var _a;
const type = "docker" in context.build.config &&
context.build.config.docker &&
"type" in context.build.config.docker
? (_a = context.build.config.docker) === null || _a === void 0 ? void 0 : _a.type
: defaultType;
return (0, exports.getDockerBuildDefaultScript)(context, type ? BUILT_IN_ENSURE_DOCKERFILE_SCRIPTS[type] : null);
};
exports.getDockerBuildScriptWithBuiltInDockerFile = getDockerBuildScriptWithBuiltInDockerFile;
const getDockerBuildDefaultScript = (context, ensureDockerFileScript) => [
ensureDockerFileScript !== null && ensureDockerFileScript !== void 0 ? ensureDockerFileScript : undefined,
...(0, gitlab_1.collapseableSection)("docker-login", "Docker Login")((0, exports.gitlabDockerLogin)(context)),
...(0, gitlab_1.collapseableSection)("docker-build", "Docker build")([
"docker build --network host --cache-from $DOCKER_CACHE_IMAGE --tag $DOCKER_IMAGE:$DOCKER_IMAGE_TAG -f $APP_DIR/Dockerfile $DOCKER_BUILD_CONTEXT --build-arg BUILDKIT_INLINE_CACHE=1", //BUILDKIT_INLINE_CACHE, see https://testdriven.io/blog/faster-ci-builds-with-docker-cache/
]),
...(0, gitlab_1.collapseableSection)("docker-push", "Docker push and tag")([
"docker push $DOCKER_IMAGE:$DOCKER_IMAGE_TAG",
"docker tag $DOCKER_IMAGE:$DOCKER_IMAGE_TAG $DOCKER_CACHE_IMAGE",
"docker push $DOCKER_CACHE_IMAGE",
]),
].filter(Boolean);
exports.getDockerBuildDefaultScript = getDockerBuildDefaultScript;
const hasDockerfile = (context) => (0, fs_1.existsSync)(path_1.default.join(context.build.dir, "Dockerfile"));
exports.hasDockerfile = hasDockerfile;
//# sourceMappingURL=docker.js.map