From c0aae933eb3dee7ebae7252e2a2230deedd1a2b2 Mon Sep 17 00:00:00 2001 From: Tommy Chen Date: Tue, 11 Aug 2020 16:00:57 +0800 Subject: [PATCH] feat(docker-build): Add "--copy" option --- packages/docker-build/README.md | 5 +++ packages/docker-build/src/commands/build.ts | 19 ++++++++++ .../docker-build/src/utils/copyAdditional.ts | 35 +++++++++++++++++++ yarn.lock | 2 +- 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 packages/docker-build/src/utils/copyAdditional.ts diff --git a/packages/docker-build/README.md b/packages/docker-build/README.md index 83aff72..f745a33 100644 --- a/packages/docker-build/README.md +++ b/packages/docker-build/README.md @@ -58,8 +58,13 @@ Example: ```sh yarn docker build @foo/bar yarn docker build @foo/bar -t image-tag +yarn docker build --copy secret.key --copy config.json @foo/bar ``` #### `-f,--file` Path to `Dockerfile`. Default to the Dockerfile in the workspace or the project. + +#### `--copy` + +Copy additional files to a Docker image. This is useful for secret keys or configuration files. The files will be copied to `manifests` folder. The path can be either a path relative to the Dockerfile or an absolute path. diff --git a/packages/docker-build/src/commands/build.ts b/packages/docker-build/src/commands/build.ts index e8f23d6..f1bf620 100644 --- a/packages/docker-build/src/commands/build.ts +++ b/packages/docker-build/src/commands/build.ts @@ -18,6 +18,7 @@ import copyManifests from '../utils/copyManifests'; import copyCacheMarkedFiles from '../utils/copyCacheMarkedFiles'; import generateLockfile from '../utils/generateLockfile'; import packWorkspace from '../utils/packWorkspace'; +import copyAdditional from '../utils/copyAdditional'; export default class DockerBuildCommand extends BaseCommand { @Command.String() @@ -29,6 +30,9 @@ export default class DockerBuildCommand extends BaseCommand { @Command.String('-f,--file') public dockerFilePath?: string; + @Command.Array('--copy') + public copyFiles?: string[]; + public static usage = Command.Usage({ category: 'Docker-related commands', description: 'Build a Docker image for a workspace', @@ -38,6 +42,8 @@ export default class DockerBuildCommand extends BaseCommand { You have to create a Dockerfile in your workspace or your project. You can also specify the path to Dockerfile using the "-f, --file" option. Additional arguments can be passed to "docker build" directly, please check the Docker docs for more info: https://docs.docker.com/engine/reference/commandline/build/ + + You can copy additional files or folders to a Docker image using the "--copy" option. This is useful for secret keys or configuration files. The files will be copied to "manifests" folder. The path can be either a path relative to the Dockerfile or an absolute path. `, examples: [ ['Build a Docker image for a workspace', 'yarn docker build @foo/bar'], @@ -45,6 +51,10 @@ export default class DockerBuildCommand extends BaseCommand { 'Pass additional arguments to docker build command', 'yarn docker build @foo/bar -t image-tag', ], + [ + 'Copy additional files to a Docker image', + 'yarn docker build --copy secret.key --copy config.json @foo/bar', + ], ], }); @@ -129,6 +139,15 @@ export default class DockerBuildCommand extends BaseCommand { project, report, }); + + if (this.copyFiles && this.copyFiles.length) { + await copyAdditional({ + destination: manifestDir, + files: this.copyFiles, + dockerFilePath, + report, + }); + } }); for (const ws of requiredWorkspaces) { diff --git a/packages/docker-build/src/utils/copyAdditional.ts b/packages/docker-build/src/utils/copyAdditional.ts new file mode 100644 index 0000000..3d3f00a --- /dev/null +++ b/packages/docker-build/src/utils/copyAdditional.ts @@ -0,0 +1,35 @@ +import { PortablePath, xfs, ppath, toFilename } from '@yarnpkg/fslib'; +import { Report } from '@yarnpkg/core'; + +function resolvePath(baseDir: PortablePath, inputPath: string): PortablePath { + const path = toFilename(inputPath); + + if (ppath.isAbsolute(path)) { + return ppath.relative(baseDir, path); + } + + return path; +} + +export default async function copyAdditional({ + destination, + files, + dockerFilePath, + report, +}: { + destination: PortablePath; + files: string[]; + dockerFilePath: PortablePath; + report: Report; +}): Promise { + const baseDir = ppath.dirname(dockerFilePath); + + for (const file of files) { + const path = resolvePath(baseDir, file); + const src = ppath.join(baseDir, path); + const dest = ppath.join(destination, path); + + report.reportInfo(null, path); + await xfs.copyPromise(dest, src); + } +} diff --git a/yarn.lock b/yarn.lock index 8c3c950..5d648c7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2623,7 +2623,7 @@ babel-plugin-lazy-import@arcanis/babel-plugin-lazy-import: "@babel/template": ^7.4.4 peerDependencies: "@babel/core": ^7 - checksum: 3/3a0b1d65c8a8814b04e92083b5498b7eba088d9750510699e2b81b4035522f54a6f367caac800ab773af7ce65e7b2e342d85e560af24d417de2c0b3274d43abe + checksum: 3/e5b5246c2b333a6110a8eff277048e944cd293904ec4bf1deeac3276d5fe17bee6f56c9e5b8ff6668c5450e7c5520a34ac2400b04e3bc41a44b5d97c6aaaee39 languageName: node linkType: hard