@magda/docker-utils
Version:
MAGDA Docker Utilities
106 lines (102 loc) • 3.58 kB
JavaScript
#!/usr/bin/env node
// ../../scripts/retag-and-push.js
import yargs from "yargs";
import * as childProcess from "child_process";
// ../../scripts/docker-util.js
function getVersions(local, version) {
return version || [
!local && process.env.npm_package_version ? process.env.npm_package_version : "latest"
];
}
function getName(name) {
if (name && typeof name === "string") {
return name;
}
return process.env.npm_package_config_docker_name ? process.env.npm_package_config_docker_name : process.env.npm_package_name ? "magda-" + process.env.npm_package_name.split("/")[1] : "UnnamedImage";
}
// ../../scripts/retag-and-push.js
var env = Object.assign({}, process.env);
var extraParameters = [];
if (env.ConEmuANSI === "ON") {
env.ConEmuANSI = "OFF";
extraParameters.push("-cur_console:i");
}
var argv = yargs.config().help().option("fromPrefix", {
describe: "The prefix of the image that will be given a new tag",
type: "string",
example: "registry.gitlab.com/magda-data/",
default: ""
}).option("fromName", {
describe: "The package name that used to generate the fromTag. Used to optionally override the docker nanme config in package.json during the auto tagging.",
type: "string",
example: "magda-ckan-connector",
default: ""
}).option("fromVersion", {
describe: "The version of the existing image that will be given a new tag",
type: "string",
example: "0.0.38-RC1",
default: getVersions()[0]
}).option("toPrefix", {
describe: "The prefix for the tag to push to",
type: "string",
example: "registry.gitlab.com/magda-data/",
default: ""
}).option("toName", {
describe: "The package name that used to generate the toTag. Used to optionally override the docker nanme config in package.json during the auto tagging.",
type: "string",
example: "magda-ckan-connector",
default: ""
}).option("copyFromRegistry", {
describe: `When \`copyFromRegistry\`=true, [regctl](https://github.com/regclient/regclient) will be used.
The image will be copied directly from remote registry to the destination registry rather than from a local image.
This allows copying multi-arch image from one registry to another registry.`,
type: "boolean",
default: false
}).option("toVersion", {
describe: "The version for the tag to push to",
type: "string",
example: "0.0.39",
default: getVersions()[0]
}).argv;
var fromTag = argv.fromPrefix + getName(argv.fromName) + ":" + argv.fromVersion;
var toTag = argv.toPrefix + getName(argv.toName) + ":" + argv.toVersion;
if (argv.copyFromRegistry) {
console.log(`Copying from \`${fromTag}\` to \`${toTag}\`...`);
const copyProcess = childProcess.spawn(
"regctl",
["image", "copy", fromTag, toTag],
{
stdio: ["pipe", "pipe", "pipe"],
env
}
);
copyProcess.stderr.pipe(process.stderr);
copyProcess.stdout.pipe(process.stdout);
copyProcess.on("close", (code) => {
process.exit(code);
});
} else {
const pullProcess = childProcess.spawnSync("docker", ["pull", fromTag], {
stdio: ["pipe", "inherit", "inherit"],
env
});
if (pullProcess.status !== 0) {
process.exit(pullProcess.status);
}
const tagProcess = childProcess.spawnSync(
"docker",
["tag", fromTag, toTag],
{
stdio: ["pipe", "inherit", "inherit"],
env
}
);
if (tagProcess.status !== 0) {
process.exit(tagProcess.status);
}
const pushProcess = childProcess.spawnSync("docker", ["push", toTag], {
stdio: ["pipe", "inherit", "inherit"],
env
});
process.exit(pushProcess.status);
}