-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathsource-action.ts
109 lines (95 loc) · 3.16 KB
/
source-action.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import codepipeline = require('@aws-cdk/aws-codepipeline');
import targets = require('@aws-cdk/aws-events-targets');
import s3 = require('@aws-cdk/aws-s3');
import { Construct } from '@aws-cdk/core';
import { Action } from '../action';
import { sourceArtifactBounds } from '../common';
/**
* How should the S3 Action detect changes.
* This is the type of the {@link S3SourceAction.trigger} property.
*/
export enum S3Trigger {
/**
* The Action will never detect changes -
* the Pipeline it's part of will only begin a run when explicitly started.
*/
NONE = 'None',
/**
* CodePipeline will poll S3 to detect changes.
* This is the default method of detecting changes.
*/
POLL = 'Poll',
/**
* CodePipeline will use CloudWatch Events to be notified of changes.
* Note that the Bucket that the Action uses needs to be part of a CloudTrail Trail
* for the events to be delivered.
*/
EVENTS = 'Events',
}
/**
* Construction properties of the {@link S3SourceAction S3 source Action}.
*/
export interface S3SourceActionProps extends codepipeline.CommonAwsActionProps {
/**
*
*/
readonly output: codepipeline.Artifact;
/**
* The key within the S3 bucket that stores the source code.
*
* @example 'path/to/file.zip'
*/
readonly bucketKey: string;
/**
* How should CodePipeline detect source changes for this Action.
* Note that if this is S3Trigger.EVENTS, you need to make sure to include the source Bucket in a CloudTrail Trail,
* as otherwise the CloudWatch Events will not be emitted.
*
* @default S3Trigger.POLL
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/log-s3-data-events.html
*/
readonly trigger?: S3Trigger;
/**
* The Amazon S3 bucket that stores the source code
*/
readonly bucket: s3.IBucket;
}
/**
* Source that is provided by a specific Amazon S3 object.
*
* Will trigger the pipeline as soon as the S3 object changes, but only if there is
* a CloudTrail Trail in the account that captures the S3 event.
*/
export class S3SourceAction extends Action {
private readonly props: S3SourceActionProps;
constructor(props: S3SourceActionProps) {
super({
...props,
category: codepipeline.ActionCategory.SOURCE,
provider: 'S3',
artifactBounds: sourceArtifactBounds(),
outputs: [props.output],
});
this.props = props;
}
protected bound(_scope: Construct, stage: codepipeline.IStage, options: codepipeline.ActionBindOptions):
codepipeline.ActionConfig {
if (this.props.trigger === S3Trigger.EVENTS) {
this.props.bucket.onCloudTrailPutObject(stage.pipeline.node.uniqueId + 'SourceEventRule', {
target: new targets.CodePipeline(stage.pipeline),
paths: [this.props.bucketKey]
});
}
// we need to read from the source bucket...
this.props.bucket.grantRead(options.role);
// ...and write to the Pipeline bucket
options.bucket.grantWrite(options.role);
return {
configuration: {
S3Bucket: this.props.bucket.bucketName,
S3ObjectKey: this.props.bucketKey,
PollForSourceChanges: this.props.trigger && this.props.trigger === S3Trigger.POLL,
},
};
}
}