From f96b2fb100c5f97e5f791b4c5d616940d85e7da5 Mon Sep 17 00:00:00 2001 From: Hiroshi Kuwabara Date: Fri, 4 Oct 2019 04:40:00 +0900 Subject: [PATCH] fix(ecr-assets): `exclude` option (#4354) * fix(@aws-cdk/aws-ecr-assets): `exclude` option (#4353) fixed #4353 * .dockerignore itself should be included * fixed test path with image.sourceHash * modified excluded path to be tested --- .../aws-ecr-assets/lib/image-asset.ts | 2 +- .../dockerignore-image/subdirectory/baz.txt | 0 .../aws-ecr-assets/test/test.image-asset.ts | 39 +++++++++++++++---- 3 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 packages/@aws-cdk/aws-ecr-assets/test/dockerignore-image/subdirectory/baz.txt diff --git a/packages/@aws-cdk/aws-ecr-assets/lib/image-asset.ts b/packages/@aws-cdk/aws-ecr-assets/lib/image-asset.ts index 1c0746edbefe3..662883cc72bd0 100644 --- a/packages/@aws-cdk/aws-ecr-assets/lib/image-asset.ts +++ b/packages/@aws-cdk/aws-ecr-assets/lib/image-asset.ts @@ -75,7 +75,7 @@ export class DockerImageAsset extends Construct implements assets.IAsset { throw new Error(`No 'Dockerfile' found in ${dir}`); } - let exclude: string[] = ['.dockerignore']; + let exclude: string[] = props.exclude || []; const ignore = path.join(dir, '.dockerignore'); diff --git a/packages/@aws-cdk/aws-ecr-assets/test/dockerignore-image/subdirectory/baz.txt b/packages/@aws-cdk/aws-ecr-assets/test/dockerignore-image/subdirectory/baz.txt new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/packages/@aws-cdk/aws-ecr-assets/test/test.image-asset.ts b/packages/@aws-cdk/aws-ecr-assets/test/test.image-asset.ts index 62b742a6e6df5..955d0e8e3d98a 100644 --- a/packages/@aws-cdk/aws-ecr-assets/test/test.image-asset.ts +++ b/packages/@aws-cdk/aws-ecr-assets/test/test.image-asset.ts @@ -197,14 +197,14 @@ export = { const app = new App(); const stack = new Stack(app, 'stack'); - new DockerImageAsset(stack, 'MyAsset', { + const image = new DockerImageAsset(stack, 'MyAsset', { directory: path.join(__dirname, 'demo-image') }); const session = app.synth(); - test.ok(fs.existsSync(path.join(session.directory, 'asset.1a17a141505ac69144931fe263d130f4612251caa4bbbdaf68a44ed0f405439c/Dockerfile'))); - test.ok(fs.existsSync(path.join(session.directory, 'asset.1a17a141505ac69144931fe263d130f4612251caa4bbbdaf68a44ed0f405439c/index.py'))); + test.ok(fs.existsSync(path.join(session.directory, `asset.${image.sourceHash}`, 'Dockerfile'))); + test.ok(fs.existsSync(path.join(session.directory, `asset.${image.sourceHash}`, 'index.py'))); test.done(); }, @@ -212,15 +212,40 @@ export = { const app = new App(); const stack = new Stack(app, 'stack'); - new DockerImageAsset(stack, 'MyAsset', { + const image = new DockerImageAsset(stack, 'MyAsset', { directory: path.join(__dirname, 'dockerignore-image') }); const session = app.synth(); - test.ok(fs.existsSync(path.join(session.directory, `asset.1a17a141505ac69144931fe263d130f4612251caa4bbbdaf68a44ed0f405439c/Dockerfile`))); - test.ok(fs.existsSync(path.join(session.directory, 'asset.1a17a141505ac69144931fe263d130f4612251caa4bbbdaf68a44ed0f405439c/index.py'))); - test.ok(!fs.existsSync(path.join(session.directory, 'asset.1a17a141505ac69144931fe263d130f4612251caa4bbbdaf68a44ed0f405439c/foobar.txt'))); + // .dockerignore itself should be included in output to be processed during docker build + test.ok(fs.existsSync(path.join(session.directory, `asset.${image.sourceHash}`, '.dockerignore'))); + test.ok(fs.existsSync(path.join(session.directory, `asset.${image.sourceHash}`, `Dockerfile`))); + test.ok(fs.existsSync(path.join(session.directory, `asset.${image.sourceHash}`, 'index.py'))); + test.ok(!fs.existsSync(path.join(session.directory, `asset.${image.sourceHash}`, 'foobar.txt'))); + test.ok(fs.existsSync(path.join(session.directory, `asset.${image.sourceHash}`, 'subdirectory'))); + test.ok(fs.existsSync(path.join(session.directory, `asset.${image.sourceHash}`, 'subdirectory', 'baz.txt'))); + + test.done(); + }, + + 'docker directory is staged without files specified in exclude option'(test: Test) { + const app = new App(); + const stack = new Stack(app, 'stack'); + + const image = new DockerImageAsset(stack, 'MyAsset', { + directory: path.join(__dirname, 'dockerignore-image'), + exclude: ['subdirectory'] + }); + + const session = app.synth(); + + test.ok(fs.existsSync(path.join(session.directory, `asset.${image.sourceHash}`, '.dockerignore'))); + test.ok(fs.existsSync(path.join(session.directory, `asset.${image.sourceHash}`, `Dockerfile`))); + test.ok(fs.existsSync(path.join(session.directory, `asset.${image.sourceHash}`, 'index.py'))); + test.ok(!fs.existsSync(path.join(session.directory, `asset.${image.sourceHash}`, 'foobar.txt'))); + test.ok(!fs.existsSync(path.join(session.directory, `asset.${image.sourceHash}`, 'subdirectory'))); + test.ok(!fs.existsSync(path.join(session.directory, `asset.${image.sourceHash}`, 'subdirectory', 'baz.txt'))); test.done(); },