Skip to content

Commit

Permalink
feat: Support passing AssetOptions (#7099)
Browse files Browse the repository at this point in the history
feat: Support passing AssetOptions
  • Loading branch information
christophgysin authored Apr 2, 2020
1 parent 6358698 commit 3925d9a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/@aws-cdk/aws-s3-deployment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ The following source types are supported for bucket deployments:
- Local directory: `s3deploy.Source.asset('/path/to/local/directory')`
- Another bucket: `s3deploy.Source.bucket(bucket, zipObjectKey)`

To create a source from a single file, you can pass `AssetOptions` to exclude
all but a single file:

- Single file: `s3deploy.Source.asset('/path/to/local/directory', { exclude: ['**', '!onlyThisFile.txt'] })`

## Retain on Delete

By default, the contents of the destination bucket will be deleted when the
Expand Down
7 changes: 5 additions & 2 deletions packages/@aws-cdk/aws-s3-deployment/lib/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,17 @@ export class Source {
* Uses a local asset as the deployment source.
* @param path The path to a local .zip file or a directory
*/
public static asset(path: string): ISource {
public static asset(path: string, options?: s3_assets.AssetOptions): ISource {
return {
bind(context: cdk.Construct): SourceConfig {
let id = 1;
while (context.node.tryFindChild(`Asset${id}`)) {
id++;
}
const asset = new s3_assets.Asset(context, `Asset${id}`, { path });
const asset = new s3_assets.Asset(context, `Asset${id}`, {
path,
...options,
});
if (!asset.isZipArchive) {
throw new Error(`Asset path must be either a .zip file or a directory`);
}
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-s3-deployment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
},
"homepage": "https://github.com/aws/aws-cdk",
"peerDependencies": {
"@aws-cdk/assets": "0.0.0",
"@aws-cdk/aws-cloudformation": "0.0.0",
"@aws-cdk/aws-cloudfront": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
Expand Down
63 changes: 63 additions & 0 deletions packages/@aws-cdk/aws-s3-deployment/test/test.bucket-deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,69 @@ export = {
test.done();
},

'honors passed asset options'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'Dest');

// WHEN
new s3deploy.BucketDeployment(stack, 'Deploy', {
sources: [s3deploy.Source.asset(path.join(__dirname, 'my-website'), {
exclude: ['*', '!index.html'],
})],
destinationBucket: bucket,
});

// THEN
expect(stack).to(haveResource('Custom::CDKBucketDeployment', {
"ServiceToken": {
"Fn::GetAtt": [
"CustomCDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C81C01536",
"Arn"
]
},
"SourceBucketNames": [{
"Ref": "AssetParameterse9b696b2a8a1f93ea8b8a9ce1e4dd4727f9243eba984e50411ca95c6b03d26b6S3Bucket1A1EC3E9"
}],
"SourceObjectKeys": [{
"Fn::Join": [
"",
[
{
"Fn::Select": [
0,
{
"Fn::Split": [
"||",
{
"Ref": "AssetParameterse9b696b2a8a1f93ea8b8a9ce1e4dd4727f9243eba984e50411ca95c6b03d26b6S3VersionKeyE46A4824"
}
]
}
]
},
{
"Fn::Select": [
1,
{
"Fn::Split": [
"||",
{
"Ref": "AssetParameterse9b696b2a8a1f93ea8b8a9ce1e4dd4727f9243eba984e50411ca95c6b03d26b6S3VersionKeyE46A4824"
}
]
}
]
}
]
]
}],
"DestinationBucketName": {
"Ref": "DestC383B82A"
}
}));
test.done();
},
'retainOnDelete can be used to retain files when resource is deleted'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit 3925d9a

Please sign in to comment.