Skip to content

Commit

Permalink
fix(core): partial wildcards don't work with selective bundling (#10767)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
quantumew committed Oct 8, 2020
1 parent a3790be commit f7ce079
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/core/lib/asset-staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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, () => {
Expand Down Expand Up @@ -392,4 +393,3 @@ function sortObject(object: { [key: string]: any }): { [key: string]: any } {
}
return ret;
}

52 changes: 52 additions & 0 deletions packages/@aws-cdk/core/test/staging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit f7ce079

Please sign in to comment.