Skip to content

Commit 8f1a5e8

Browse files
hoegertnRomainMuller
authored andcommitted
feat(s3): Add DeployAction for codepipeline (#1596)
1 parent 58b4685 commit 8f1a5e8

File tree

6 files changed

+358
-4
lines changed

6 files changed

+358
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
{
2+
"Resources": {
3+
"PipelineArtifactsBucket22248F97": {
4+
"Type": "AWS::S3::Bucket",
5+
"DeletionPolicy": "Retain"
6+
},
7+
"PipelineRoleD68726F7": {
8+
"Type": "AWS::IAM::Role",
9+
"Properties": {
10+
"AssumeRolePolicyDocument": {
11+
"Statement": [
12+
{
13+
"Action": "sts:AssumeRole",
14+
"Effect": "Allow",
15+
"Principal": {
16+
"Service": "codepipeline.amazonaws.com"
17+
}
18+
}
19+
],
20+
"Version": "2012-10-17"
21+
}
22+
}
23+
},
24+
"PipelineRoleDefaultPolicyC7A05455": {
25+
"Type": "AWS::IAM::Policy",
26+
"Properties": {
27+
"PolicyDocument": {
28+
"Statement": [
29+
{
30+
"Action": [
31+
"s3:GetObject*",
32+
"s3:GetBucket*",
33+
"s3:List*",
34+
"s3:DeleteObject*",
35+
"s3:PutObject*",
36+
"s3:Abort*"
37+
],
38+
"Effect": "Allow",
39+
"Resource": [
40+
{
41+
"Fn::GetAtt": [
42+
"PipelineArtifactsBucket22248F97",
43+
"Arn"
44+
]
45+
},
46+
{
47+
"Fn::Join": [
48+
"",
49+
[
50+
{
51+
"Fn::GetAtt": [
52+
"PipelineArtifactsBucket22248F97",
53+
"Arn"
54+
]
55+
},
56+
"/*"
57+
]
58+
]
59+
}
60+
]
61+
},
62+
{
63+
"Action": [
64+
"s3:GetObject*",
65+
"s3:GetBucket*",
66+
"s3:List*"
67+
],
68+
"Effect": "Allow",
69+
"Resource": [
70+
{
71+
"Fn::GetAtt": [
72+
"PipelineBucketB967BD35",
73+
"Arn"
74+
]
75+
},
76+
{
77+
"Fn::Join": [
78+
"",
79+
[
80+
{
81+
"Fn::GetAtt": [
82+
"PipelineBucketB967BD35",
83+
"Arn"
84+
]
85+
},
86+
"/*"
87+
]
88+
]
89+
}
90+
]
91+
},
92+
{
93+
"Action": [
94+
"s3:DeleteObject*",
95+
"s3:PutObject*",
96+
"s3:Abort*"
97+
],
98+
"Effect": "Allow",
99+
"Resource": [
100+
{
101+
"Fn::GetAtt": [
102+
"DeployBucket67E2C076",
103+
"Arn"
104+
]
105+
},
106+
{
107+
"Fn::Join": [
108+
"",
109+
[
110+
{
111+
"Fn::GetAtt": [
112+
"DeployBucket67E2C076",
113+
"Arn"
114+
]
115+
},
116+
"/*"
117+
]
118+
]
119+
}
120+
]
121+
}
122+
],
123+
"Version": "2012-10-17"
124+
},
125+
"PolicyName": "PipelineRoleDefaultPolicyC7A05455",
126+
"Roles": [
127+
{
128+
"Ref": "PipelineRoleD68726F7"
129+
}
130+
]
131+
}
132+
},
133+
"PipelineC660917D": {
134+
"Type": "AWS::CodePipeline::Pipeline",
135+
"Properties": {
136+
"RoleArn": {
137+
"Fn::GetAtt": [
138+
"PipelineRoleD68726F7",
139+
"Arn"
140+
]
141+
},
142+
"Stages": [
143+
{
144+
"Actions": [
145+
{
146+
"ActionTypeId": {
147+
"Category": "Source",
148+
"Owner": "AWS",
149+
"Provider": "S3",
150+
"Version": "1"
151+
},
152+
"Configuration": {
153+
"S3Bucket": {
154+
"Ref": "PipelineBucketB967BD35"
155+
},
156+
"S3ObjectKey": "key"
157+
},
158+
"InputArtifacts": [],
159+
"Name": "Source",
160+
"OutputArtifacts": [
161+
{
162+
"Name": "SourceArtifact"
163+
}
164+
],
165+
"RunOrder": 1
166+
}
167+
],
168+
"Name": "Source"
169+
},
170+
{
171+
"Actions": [
172+
{
173+
"ActionTypeId": {
174+
"Category": "Deploy",
175+
"Owner": "AWS",
176+
"Provider": "S3",
177+
"Version": "1"
178+
},
179+
"Configuration": {
180+
"BucketName": {
181+
"Ref": "DeployBucket67E2C076"
182+
},
183+
"Extract": "true"
184+
},
185+
"InputArtifacts": [
186+
{
187+
"Name": "SourceArtifact"
188+
}
189+
],
190+
"Name": "DeployAction",
191+
"OutputArtifacts": [],
192+
"RunOrder": 1
193+
}
194+
],
195+
"Name": "Deploy"
196+
}
197+
],
198+
"ArtifactStore": {
199+
"Location": {
200+
"Ref": "PipelineArtifactsBucket22248F97"
201+
},
202+
"Type": "S3"
203+
}
204+
},
205+
"DependsOn": [
206+
"PipelineRoleD68726F7",
207+
"PipelineRoleDefaultPolicyC7A05455"
208+
]
209+
},
210+
"PipelineBucketB967BD35": {
211+
"Type": "AWS::S3::Bucket",
212+
"Properties": {
213+
"VersioningConfiguration": {
214+
"Status": "Enabled"
215+
}
216+
}
217+
},
218+
"DeployBucket67E2C076": {
219+
"Type": "AWS::S3::Bucket",
220+
"DeletionPolicy": "Retain"
221+
}
222+
}
223+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import s3 = require('@aws-cdk/aws-s3');
2+
import cdk = require('@aws-cdk/cdk');
3+
import codepipeline = require('../lib');
4+
5+
const app = new cdk.App();
6+
7+
const stack = new cdk.Stack(app, 'aws-cdk-codepipeline-s3-deploy');
8+
9+
const pipeline = new codepipeline.Pipeline(stack, 'Pipeline');
10+
11+
const sourceStage = new codepipeline.Stage(pipeline, 'Source', { pipeline });
12+
const bucket = new s3.Bucket(stack, 'PipelineBucket', {
13+
versioned: true,
14+
removalPolicy: cdk.RemovalPolicy.Destroy,
15+
});
16+
const sourceAction = new s3.PipelineSourceAction(stack, 'Source', {
17+
stage: sourceStage,
18+
outputArtifactName: 'SourceArtifact',
19+
bucket,
20+
bucketKey: 'key',
21+
});
22+
23+
const deployBucket = new s3.Bucket(stack, 'DeployBucket', {});
24+
25+
const deployStage = new codepipeline.Stage(pipeline, 'Deploy', { pipeline });
26+
deployBucket.addToPipelineAsDeploy(deployStage, 'DeployAction', {
27+
inputArtifact: sourceAction.outputArtifact,
28+
});
29+
30+
app.run();

packages/@aws-cdk/aws-s3/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,26 @@ const sourceAction = sourceBucket.addToPipeline(sourceStage, 'S3Source', {
132132
});
133133
```
134134

135+
### Buckets as deploy targets in CodePipeline
136+
137+
This package also defines an Action that allows you to use a
138+
Bucket as a deployment target in CodePipeline:
139+
140+
```ts
141+
import codepipeline = require('@aws-cdk/aws-codepipeline');
142+
import s3 = require('@aws-cdk/aws-s3');
143+
144+
const targetBucket = new s3.Bucket(this, 'MyBucket', {});
145+
146+
const pipeline = new codepipeline.Pipeline(this, 'MyPipeline');
147+
const deployStage = pipeline.addStage('Deploy');
148+
const deployAction = new s3.PipelineDeployAction(this, 'S3Deploy', {
149+
stage: deployStage,
150+
bucket: targetBucket,
151+
inputArtifact: sourceAction.outputArtifact,
152+
});
153+
```
154+
135155
### Sharing buckets between stacks
136156

137157
To use a bucket in a different stack in the same CDK application, pass the object to the other stack:

packages/@aws-cdk/aws-s3/lib/bucket.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import cdk = require('@aws-cdk/cdk');
77
import { BucketPolicy } from './bucket-policy';
88
import { BucketNotifications } from './notifications-resource';
99
import perms = require('./perms');
10-
import { CommonPipelineSourceActionProps, PipelineSourceAction } from './pipeline-action';
10+
import {
11+
CommonPipelineDeployActionProps, CommonPipelineSourceActionProps,
12+
PipelineDeployAction, PipelineSourceAction
13+
} from './pipeline-actions';
1114
import { LifecycleRule } from './rule';
1215
import { CfnBucket } from './s3.generated';
1316
import { parseBucketArn, parseBucketName } from './util';
@@ -64,6 +67,17 @@ export interface IBucket extends cdk.IConstruct {
6467
*/
6568
addToPipeline(stage: actions.IStage, name: string, props: CommonPipelineSourceActionProps): PipelineSourceAction;
6669

70+
/**
71+
* Convenience method for creating a new {@link PipelineDeployAction},
72+
* and adding it to the given Stage.
73+
*
74+
* @param stage the Pipeline Stage to add the new Action to
75+
* @param name the name of the newly created Action
76+
* @param props the optional properties of the new Action
77+
* @returns the newly created {@link PipelineDeployAction}
78+
*/
79+
addToPipelineAsDeploy(stage: actions.IStage, name: string, props?: CommonPipelineDeployActionProps): PipelineDeployAction;
80+
6781
/**
6882
* Adds a statement to the resource policy for a principal (i.e.
6983
* account/role/service) to perform actions on this bucket and/or it's
@@ -301,6 +315,14 @@ export abstract class BucketBase extends cdk.Construct implements IBucket {
301315
});
302316
}
303317

318+
public addToPipelineAsDeploy(stage: actions.IStage, name: string, props: CommonPipelineDeployActionProps = {}): PipelineDeployAction {
319+
return new PipelineDeployAction(this, name, {
320+
stage,
321+
bucket: this,
322+
...props,
323+
});
324+
}
325+
304326
public onPutObject(name: string, target?: events.IEventRuleTarget, path?: string): events.EventRule {
305327
const eventRule = new events.EventRule(this, name, {
306328
eventPattern: {

packages/@aws-cdk/aws-s3/lib/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * from './bucket';
22
export * from './bucket-policy';
3-
export * from './pipeline-action';
3+
export * from './pipeline-actions';
44
export * from './rule';
55

66
// AWS::S3 CloudFormation Resources:

0 commit comments

Comments
 (0)