Skip to content

Commit

Permalink
feat(codepipeline): add CannedACL and CacheControl to S3 deploy action (
Browse files Browse the repository at this point in the history
#5698)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
skinny85 and mergify[bot] committed Jan 8, 2020
1 parent a4e2353 commit b3e7978
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,47 @@
import * as codepipeline from '@aws-cdk/aws-codepipeline';
import * as s3 from '@aws-cdk/aws-s3';
import { Construct } from '@aws-cdk/core';
import { Construct, Duration } from '@aws-cdk/core';
import { Action } from '../action';
import { deployArtifactBounds } from '../common';

// Class copied verbatim from the aws-s3-deployment module.
// Yes, it sucks that we didn't abstract this properly in a common class,
// but having 2 different CacheControl classes that behave differently would be worse I think.
// Something to do when CDK 2.0.0 comes out.
/**
* Used for HTTP cache-control header, which influences downstream caches.
* Use the provided static factory methods to construct instances of this class.
* Used in the {@link S3DeployActionProps.cacheControl} property.
*
* @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
*/
export class CacheControl {
/** The 'must-revalidate' cache control directive. */
public static mustRevalidate() { return new CacheControl('must-revalidate'); }
/** The 'no-cache' cache control directive. */
public static noCache() { return new CacheControl('no-cache'); }
/** The 'no-transform' cache control directive. */
public static noTransform() { return new CacheControl('no-transform'); }
/** The 'public' cache control directive. */
public static setPublic() { return new CacheControl('public'); }
/** The 'private' cache control directive. */
public static setPrivate() { return new CacheControl('private'); }
/** The 'proxy-revalidate' cache control directive. */
public static proxyRevalidate() { return new CacheControl('proxy-revalidate'); }
/** The 'max-age' cache control directive. */
public static maxAge(t: Duration) { return new CacheControl(`max-age: ${t.toSeconds()}`); }
/** The 's-max-age' cache control directive. */
public static sMaxAge(t: Duration) { return new CacheControl(`s-max-age: ${t.toSeconds()}`); }
/**
* Allows you to create an arbitrary cache control directive,
* in case our support is missing a method for a particular directive.
*/
public static fromString(s: string) { return new CacheControl(s); }

/** @param value the actual text value of the created directive */
private constructor(public value: string) {}
}

/**
* Construction properties of the {@link S3DeployAction S3 deploy Action}.
*/
Expand All @@ -29,6 +67,23 @@ export interface S3DeployActionProps extends codepipeline.CommonAwsActionProps {
* The Amazon S3 bucket that is the deploy target.
*/
readonly bucket: s3.IBucket;

/**
* The specified canned ACL to objects deployed to Amazon S3.
* This overwrites any existing ACL that was applied to the object.
*
* @default - the original object ACL
*/
readonly accessControl?: s3.BucketAccessControl;

/**
* The caching behavior for requests/responses for objects in the bucket.
* The final cache control property will be the result of joining all of the provided array elements with a comma
* (plus a space after the comma).
*
* @default - none, decided by the HTTP client
*/
readonly cacheControl?: CacheControl[];
}

/**
Expand Down Expand Up @@ -63,6 +118,8 @@ export class S3DeployAction extends Action {
BucketName: this.props.bucket.bucketName,
Extract: this.props.extract === false ? 'false' : 'true',
ObjectKey: this.props.objectKey,
CannedACL: this.props.accessControl,
CacheControl: this.props.cacheControl && this.props.cacheControl.map(ac => ac.value).join(', '),
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@
"BucketName": {
"Ref": "DeployBucket67E2C076"
},
"Extract": "true"
"Extract": "true",
"CannedACL": "PublicRead",
"CacheControl": "public, max-age: 43200"
},
"InputArtifacts": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ new codepipeline.Pipeline(stack, 'Pipeline', {
actionName: 'DeployAction',
input: sourceOutput,
bucket: deployBucket,
accessControl: s3.BucketAccessControl.PUBLIC_READ,
cacheControl: [
cpactions.CacheControl.setPublic(),
cpactions.CacheControl.maxAge(cdk.Duration.hours(12)),
],
})
],
},
Expand Down

0 comments on commit b3e7978

Please sign in to comment.