From f7ce0796eae621e6974658959cdbf11822d39eb2 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 8 Oct 2020 03:52:14 -0500 Subject: [PATCH] fix(core): partial wildcards don't work with selective bundling (#10767) Use `minimatch` to match stack names when deciding whether to bundle assets or not. This will ensure that when `cdk deploy -e '*-stack'` is run and there is a stack with assets named `'my-stack`, the assets are bundled. fixes #10732 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/core/lib/asset-staging.ts | 4 +- packages/@aws-cdk/core/test/staging.test.ts | 52 +++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/core/lib/asset-staging.ts b/packages/@aws-cdk/core/lib/asset-staging.ts index 70444124de25a..09596eccc3abf 100644 --- a/packages/@aws-cdk/core/lib/asset-staging.ts +++ b/packages/@aws-cdk/core/lib/asset-staging.ts @@ -4,6 +4,7 @@ import * as path from 'path'; import * as cxapi from '@aws-cdk/cx-api'; import { Construct } from 'constructs'; import * as fs from 'fs-extra'; +import * as minimatch from 'minimatch'; import { AssetHashType, AssetOptions } from './assets'; import { BundlingOptions } from './bundling'; import { FileSystem, FingerprintOptions } from './fs'; @@ -137,7 +138,7 @@ export class AssetStaging extends CoreConstruct { if (props.bundling) { // Check if we actually have to bundle for this stack const bundlingStacks: string[] = this.node.tryGetContext(cxapi.BUNDLING_STACKS) ?? ['*']; - const runBundling = bundlingStacks.includes(Stack.of(this).stackName) || bundlingStacks.includes('*'); + const runBundling = !!bundlingStacks.find(pattern => minimatch(Stack.of(this).stackName, pattern)); if (runBundling) { const bundling = props.bundling; this.assetHash = AssetStaging.getOrCalcAssetHash(cacheKey, () => { @@ -392,4 +393,3 @@ function sortObject(object: { [key: string]: any }): { [key: string]: any } { } return ret; } - diff --git a/packages/@aws-cdk/core/test/staging.test.ts b/packages/@aws-cdk/core/test/staging.test.ts index a40e67edbfd96..c85108794d8db 100644 --- a/packages/@aws-cdk/core/test/staging.test.ts +++ b/packages/@aws-cdk/core/test/staging.test.ts @@ -706,6 +706,58 @@ nodeunitShim({ test.done(); }, + + 'bundling still occurs with partial wildcard'(test: Test) { + // GIVEN + const app = new App(); + const stack = new Stack(app, 'MyStack'); + stack.node.setContext(cxapi.BUNDLING_STACKS, ['*Stack']); + const directory = path.join(__dirname, 'fs', 'fixtures', 'test1'); + + // WHEN + const asset = new AssetStaging(stack, 'Asset', { + sourcePath: directory, + assetHashType: AssetHashType.BUNDLE, + bundling: { + image: BundlingDockerImage.fromRegistry('alpine'), + command: [DockerStubCommand.SUCCESS], + }, + }); + + test.equal( + readDockerStubInput(), + `run --rm ${USER_ARG} -v /input:/asset-input:delegated -v /output:/asset-output:delegated -w /asset-input alpine DOCKER_STUB_SUCCESS`, + ); + test.equal(asset.assetHash, '33cbf2cae5432438e0f046bc45ba8c3cef7b6afcf47b59d1c183775c1918fb1f'); // hash of MyStack/Asset + + test.done(); + }, + + 'bundling still occurs with full wildcard'(test: Test) { + // GIVEN + const app = new App(); + const stack = new Stack(app, 'MyStack'); + stack.node.setContext(cxapi.BUNDLING_STACKS, ['*']); + const directory = path.join(__dirname, 'fs', 'fixtures', 'test1'); + + // WHEN + const asset = new AssetStaging(stack, 'Asset', { + sourcePath: directory, + assetHashType: AssetHashType.BUNDLE, + bundling: { + image: BundlingDockerImage.fromRegistry('alpine'), + command: [DockerStubCommand.SUCCESS], + }, + }); + + test.equal( + readDockerStubInput(), + `run --rm ${USER_ARG} -v /input:/asset-input:delegated -v /output:/asset-output:delegated -w /asset-input alpine DOCKER_STUB_SUCCESS`, + ); + test.equal(asset.assetHash, '33cbf2cae5432438e0f046bc45ba8c3cef7b6afcf47b59d1c183775c1918fb1f'); // hash of MyStack/Asset + + test.done(); + }, }); // Reads a docker stub and cleans the volume paths out of the stub.