Skip to content

Commit

Permalink
feat(docker-build): Add "--copy" option
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Aug 11, 2020
1 parent 4622ab2 commit c0aae93
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/docker-build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
19 changes: 19 additions & 0 deletions packages/docker-build/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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',
Expand All @@ -38,13 +42,19 @@ 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'],
[
'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',
],
],
});

Expand Down Expand Up @@ -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) {
Expand Down
35 changes: 35 additions & 0 deletions packages/docker-build/src/utils/copyAdditional.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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);
}
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit c0aae93

Please sign in to comment.