Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cdk-assets): context path not honored by Docker asset build #6957

Merged
merged 3 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/cdk-assets/lib/private/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ export class Docker {
'--tag', options.tag,
...options.target ? ['--target', options.target] : [],
...options.file ? ['--file', options.file] : [],
options.directory,
'.'
];
await this.execute(buildCommand);
await this.execute(buildCommand, { cwd: options.directory });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm fwiw. The below alternative patch also worked for me if for some reason the reviewers wanted to avoid setting cwd (I don't know why that would be an issue):

--- cdk-assets/lib/private/handlers/container-images.js.orig    2020-03-24 06:03:56.670862836 +0000
+++ cdk-assets/lib/private/handlers/container-images.js 2020-03-24 05:58:01.971071483 +0000
@@ -51,7 +51,7 @@
             tag: this.localTagName,
             buildArgs: source.dockerBuildArgs,
             target: source.dockerBuildTarget,
-            file: source.dockerFile,
+            file: path.resolve(fullPath, source.dockerFile),
         });
     }
 }

}

/**
Expand Down Expand Up @@ -100,4 +100,4 @@ async function obtainEcrCredentials(ecr: AWS.ECR, logger?: Logger) {

function flatten(x: string[][]) {
return Array.prototype.concat([], ...x);
}
}
7 changes: 4 additions & 3 deletions packages/cdk-assets/test/docker-images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { mockAws, mockedApiFailure, mockedApiResult } from './mock-aws';
import { mockSpawn } from './mock-child_process';

let aws: ReturnType<typeof mockAws>;
const absoluteDockerPath = '/simple/cdk.out/dockerdir';
beforeEach(() => {
mockfs({
'/simple/cdk.out/assets.json': JSON.stringify({
Expand All @@ -33,7 +34,7 @@ beforeEach(() => {
dockerImages: {
theAsset: {
source: {
directory: '/simple/cdk.out/dockerdir'
directory: absoluteDockerPath
},
destinations: {
theDestination: {
Expand Down Expand Up @@ -112,7 +113,7 @@ describe('with a complete manifest', () => {
mockSpawn(
{ commandLine: ['docker', 'login', '--username', 'user', '--password-stdin', 'https://proxy.com/'] },
{ commandLine: ['docker', 'inspect', 'cdkasset-theasset'], exitCode: 1 },
{ commandLine: ['docker', 'build', '--tag', 'cdkasset-theasset', '/simple/cdk.out/dockerdir'] },
{ commandLine: ['docker', 'build', '--tag', 'cdkasset-theasset', '.'], cwd: absoluteDockerPath },
{ commandLine: ['docker', 'tag', 'cdkasset-theasset', '12345.amazonaws.com/repo:abcdef'] },
{ commandLine: ['docker', 'push', '12345.amazonaws.com/repo:abcdef'] },
);
Expand All @@ -135,7 +136,7 @@ test('correctly identify Docker directory if path is absolute', async () => {
// Only care about the 'build' command line
{ commandLine: ['docker', 'login'], prefix: true, },
{ commandLine: ['docker', 'inspect'], exitCode: 1, prefix: true },
{ commandLine: ['docker', 'build', '--tag', 'cdkasset-theasset', '/simple/cdk.out/dockerdir'] },
{ commandLine: ['docker', 'build', '--tag', 'cdkasset-theasset', '.'], cwd: absoluteDockerPath },
{ commandLine: ['docker', 'tag'], prefix: true },
{ commandLine: ['docker', 'push'], prefix: true },
);
Expand Down
9 changes: 7 additions & 2 deletions packages/cdk-assets/test/mock-child_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if (!(child_process as any).spawn.mockImplementationOnce) {

export interface Invocation {
commandLine: string[];
cwd?: string;
exitCode?: number;
stdout?: string;

Expand All @@ -20,7 +21,7 @@ export function mockSpawn(...invocations: Invocation[]) {
let mock = (child_process.spawn as any);
for (const _invocation of invocations) {
const invocation = _invocation; // Mirror into variable for closure
mock = mock.mockImplementationOnce((binary: string, args: string[], _options: any) => {
mock = mock.mockImplementationOnce((binary: string, args: string[], options: child_process.SpawnOptions) => {
if (invocation.prefix) {
// Match command line prefix
expect([binary, ...args].slice(0, invocation.commandLine.length)).toEqual(invocation.commandLine);
Expand All @@ -29,6 +30,10 @@ export function mockSpawn(...invocations: Invocation[]) {
expect([binary, ...args]).toEqual(invocation.commandLine);
}

if (invocation.cwd != null) {
expect(options.cwd).toBe(invocation.cwd);
}

const child: any = new events.EventEmitter();
child.stdin = new events.EventEmitter();
child.stdin.write = jest.fn();
Expand Down Expand Up @@ -57,4 +62,4 @@ function mockEmit(emitter: events.EventEmitter, event: string, data: any) {
setImmediate(() => {
emitter.emit(event, data);
});
}
}