@catladder/cli
Version:
Panter cli tool for cloud CI/CD and DevOps
98 lines • 5.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDeleteUnusedImagesCommands = exports.getArtifactsRegistryImage = exports.getArtifactsRegistryBuildCacheImage = exports.getArtifactsRegistryImageName = exports.getArtifactsRegistryDockerUrl = exports.getArtifactsRegistryHost = void 0;
const BashExpression_1 = require("../../bash/BashExpression");
const gitlab_1 = require("../../utils/gitlab");
const types_1 = require("../types");
const removeFirstLinesFromCommandOutput_1 = require("./utils/removeFirstLinesFromCommandOutput");
const getArtifactsRegistryHost = (context) => {
var _a;
if (!(0, types_1.isOfDeployType)((_a = context.deploy) === null || _a === void 0 ? void 0 : _a.config, "google-cloudrun")) {
// should not happen
throw new Error("deploy config is wrong");
}
return `${context.deploy.config.region}-docker.pkg.dev`;
};
exports.getArtifactsRegistryHost = getArtifactsRegistryHost;
const getArtifactsRegistryDockerUrl = (context) => {
var _a;
const deployConfig = (_a = context.deploy) === null || _a === void 0 ? void 0 : _a.config;
if (!(0, types_1.isOfDeployType)(deployConfig, "google-cloudrun")) {
// should not happen
throw new Error("deploy config is wrong");
}
const fullAppName = `${context.fullConfig.customerName}-${context.fullConfig.appName}`;
return `${(0, exports.getArtifactsRegistryHost)(context)}/${deployConfig.projectId}/catladder-deploy/${fullAppName}`;
};
exports.getArtifactsRegistryDockerUrl = getArtifactsRegistryDockerUrl;
/**
*
* lecacyReviewImageName is only temporary. In old versions the images had no reviewslug in review apps, which makes cleanup harder. We delete all those images now, but need the path
*/
const getArtifactsRegistryImageName = (context, lecacyReviewImageName = false) => {
if (lecacyReviewImageName && context.environment.envType !== "review") {
throw new Error("lecacyReviewImageName is only allowed for review app");
}
const dockerUrl = (0, exports.getArtifactsRegistryDockerUrl)(context);
const gcloudImagePath = [
dockerUrl,
context.env,
context.name,
...(context.environment.reviewSlug && !lecacyReviewImageName
? [context.environment.reviewSlug]
: []),
];
return (0, BashExpression_1.joinBashExpressions)(gcloudImagePath, "/");
};
exports.getArtifactsRegistryImageName = getArtifactsRegistryImageName;
const getArtifactsRegistryBuildCacheImage = (context) => {
const dockerUrl = (0, exports.getArtifactsRegistryDockerUrl)(context);
// does not include env, so that after merge, you might get more cache hits (review-->dev)
const gcloudImagePath = [dockerUrl, "caches", context.name];
return gcloudImagePath.join("/");
};
exports.getArtifactsRegistryBuildCacheImage = getArtifactsRegistryBuildCacheImage;
const getArtifactsRegistryImage = (context) => `${(0, exports.getArtifactsRegistryImageName)(context)}:$DOCKER_IMAGE_TAG`;
exports.getArtifactsRegistryImage = getArtifactsRegistryImage;
const getDeleteImageCommands = (fullImageName, keepNewest = 0) => {
if (keepNewest === 0) {
// no need to list tags, we delete the whole thing
return [
`gcloud artifacts docker images delete ${fullImageName} --quiet --delete-tags`,
];
}
// delete unused tags
const listAllImagesCommand = `gcloud artifacts docker images list ${fullImageName} --sort-by=~CREATE_TIME --format="value(version)"`;
const listImagesToDeletecommand = (0, removeFirstLinesFromCommandOutput_1.removeFirstLinesFromCommandOutput)(listAllImagesCommand, keepNewest);
const deleteImageCommand = `gcloud artifacts docker images delete ${fullImageName}@$version --quiet --delete-tags`;
const deleteImagesCommand = `${listImagesToDeletecommand} | while read -r version; do ${deleteImageCommand}; done`;
return [deleteImagesCommand];
};
/**
* commands to delete unused images.
*
* @param context
* @param keep how many of the newest images to keep
* @returns
*/
const getDeleteUnusedImagesCommands = (context, keep = 0) => {
var _a;
const deployConfig = (_a = context.deploy) === null || _a === void 0 ? void 0 : _a.config;
if (!(0, types_1.isOfDeployType)(deployConfig, "google-cloudrun")) {
// should not happen
throw new Error("deploy config is wrong");
}
const fullImageName = (0, exports.getArtifactsRegistryImageName)(context);
const buildCacheImageName = (0, exports.getArtifactsRegistryBuildCacheImage)(context);
return [
...getDeleteImageCommands(fullImageName, keep),
...getDeleteImageCommands(buildCacheImageName, 1),
// because a recent version of catladder had no review-slug in the image name, we have to delete those as well
// we can later remove this line in some months
...(context.environment.envType === "review"
? (0, gitlab_1.allowFailureInScripts)(getDeleteImageCommands((0, exports.getArtifactsRegistryImageName)(context, true), 0))
: []),
];
};
exports.getDeleteUnusedImagesCommands = getDeleteUnusedImagesCommands;
//# sourceMappingURL=artifactsRegistry.js.map