Skip to content

Commit

Permalink
feat: Add "push" option.
Browse files Browse the repository at this point in the history
  • Loading branch information
darkobits committed Aug 28, 2019
1 parent b8ee335 commit d7bb087
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ yargs.command({
conflicts: ['dockerfile']
});

command.option('push', {
group: 'Optional Arguments:',
description: 'Whether to push images to a registry.',
required: false,
type: 'boolean',
default: false
});

command.example('$0', 'Dockerize the NodeJS project in the current directory using default options.');
command.example('$0 --label="foo=bar" --label="baz=qux" --extra-args="--squash"', 'Dockerize the NodeJS project in the current directory, apply two labels, and pass the --squash argument to Docker.');

Expand Down
7 changes: 7 additions & 0 deletions src/etc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ export interface DockerizeArguments extends Arguments {
* This file will be removed from the final image.
*/
npmrc?: string;

/**
* Whether to run 'docker push' after building an image.
*
* Note: This option assumes 'docker login' has already been run.
*/
push?: boolean;
}
33 changes: 31 additions & 2 deletions src/lib/dockerize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default async function dockerize(options: DockerizeArguments) {
ow(options.extraArgs, 'extra Docker arguments', ow.any(ow.undefined, ow.string));
ow(options.dockerfile, 'custom Dockerfile', ow.any(ow.undefined, ow.string));
ow(options.npmrc, '.npmrc file', ow.any(ow.undefined, ow.string));
ow(options.push, 'push', ow.any(ow.undefined, ow.boolean));


// ----- [2] Prepare Staging Area --------------------------------------------
Expand Down Expand Up @@ -181,7 +182,7 @@ export default async function dockerize(options: DockerizeArguments) {

const buildTime = log.createTimer();
const spinner = log.createSpinner();
const endInteractive = log.beginInteractive(() => log.info(`${spinner} Building image ${log.chalk.cyan.bold(tag)}...`));
const endBuildInteractive = log.beginInteractive(() => log.info(`${spinner} Building image ${log.chalk.cyan.bold(tag)}...`));

await Promise.all([
// Copy production-relevant package files to the staging directory.
Expand Down Expand Up @@ -218,5 +219,33 @@ export default async function dockerize(options: DockerizeArguments) {
fs.remove(stagingDir)
]);

endInteractive(() => log.info(`🏁 Built image ${log.chalk.cyan.bold(tag)} ${log.chalk.dim(`(${imageSize})`)} in ${buildTime}.`));
endBuildInteractive(() => log.info(`🏁 Built image ${log.chalk.cyan.bold(tag)} ${log.chalk.dim(`(${imageSize})`)} in ${buildTime}.`));


// ----- [11] (Optional) Push Image ------------------------------------------

if (!options.push) {
return;
}

const pushTime = log.createTimer();
const endPushInteractive = log.beginInteractive(() => log.info(`${spinner} Pushing image ${log.chalk.cyan.bold(tag)}...`));

const pushProcess = execa('docker', ['push', tag], {
stdin: 'ignore',
stdout: log.isLevelAtLeast('silly') ? 'pipe' : 'ignore',
stderr: log.isLevelAtLeast('silly') ? 'pipe' : 'ignore',
});

if (pushProcess.stdout) {
pushProcess.stdout.pipe(log.createPipe('silly'));
}

if (pushProcess.stderr) {
pushProcess.stderr.pipe(log.createPipe('silly'));
}

await pushProcess;

endPushInteractive(() => log.info(`🚀 Pushed image ${log.chalk.cyan.bold(tag)} in ${pushTime}.`));
}

0 comments on commit d7bb087

Please sign in to comment.