Skip to content

Commit

Permalink
feat(s3): Add DeployAction for codepipeline (#1596)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoegertn authored and RomainMuller committed Jan 31, 2019
1 parent 58b4685 commit 8f1a5e8
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
{
"Resources": {
"PipelineArtifactsBucket22248F97": {
"Type": "AWS::S3::Bucket",
"DeletionPolicy": "Retain"
},
"PipelineRoleD68726F7": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "codepipeline.amazonaws.com"
}
}
],
"Version": "2012-10-17"
}
}
},
"PipelineRoleDefaultPolicyC7A05455": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": [
"s3:GetObject*",
"s3:GetBucket*",
"s3:List*",
"s3:DeleteObject*",
"s3:PutObject*",
"s3:Abort*"
],
"Effect": "Allow",
"Resource": [
{
"Fn::GetAtt": [
"PipelineArtifactsBucket22248F97",
"Arn"
]
},
{
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
"PipelineArtifactsBucket22248F97",
"Arn"
]
},
"/*"
]
]
}
]
},
{
"Action": [
"s3:GetObject*",
"s3:GetBucket*",
"s3:List*"
],
"Effect": "Allow",
"Resource": [
{
"Fn::GetAtt": [
"PipelineBucketB967BD35",
"Arn"
]
},
{
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
"PipelineBucketB967BD35",
"Arn"
]
},
"/*"
]
]
}
]
},
{
"Action": [
"s3:DeleteObject*",
"s3:PutObject*",
"s3:Abort*"
],
"Effect": "Allow",
"Resource": [
{
"Fn::GetAtt": [
"DeployBucket67E2C076",
"Arn"
]
},
{
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
"DeployBucket67E2C076",
"Arn"
]
},
"/*"
]
]
}
]
}
],
"Version": "2012-10-17"
},
"PolicyName": "PipelineRoleDefaultPolicyC7A05455",
"Roles": [
{
"Ref": "PipelineRoleD68726F7"
}
]
}
},
"PipelineC660917D": {
"Type": "AWS::CodePipeline::Pipeline",
"Properties": {
"RoleArn": {
"Fn::GetAtt": [
"PipelineRoleD68726F7",
"Arn"
]
},
"Stages": [
{
"Actions": [
{
"ActionTypeId": {
"Category": "Source",
"Owner": "AWS",
"Provider": "S3",
"Version": "1"
},
"Configuration": {
"S3Bucket": {
"Ref": "PipelineBucketB967BD35"
},
"S3ObjectKey": "key"
},
"InputArtifacts": [],
"Name": "Source",
"OutputArtifacts": [
{
"Name": "SourceArtifact"
}
],
"RunOrder": 1
}
],
"Name": "Source"
},
{
"Actions": [
{
"ActionTypeId": {
"Category": "Deploy",
"Owner": "AWS",
"Provider": "S3",
"Version": "1"
},
"Configuration": {
"BucketName": {
"Ref": "DeployBucket67E2C076"
},
"Extract": "true"
},
"InputArtifacts": [
{
"Name": "SourceArtifact"
}
],
"Name": "DeployAction",
"OutputArtifacts": [],
"RunOrder": 1
}
],
"Name": "Deploy"
}
],
"ArtifactStore": {
"Location": {
"Ref": "PipelineArtifactsBucket22248F97"
},
"Type": "S3"
}
},
"DependsOn": [
"PipelineRoleD68726F7",
"PipelineRoleDefaultPolicyC7A05455"
]
},
"PipelineBucketB967BD35": {
"Type": "AWS::S3::Bucket",
"Properties": {
"VersioningConfiguration": {
"Status": "Enabled"
}
}
},
"DeployBucket67E2C076": {
"Type": "AWS::S3::Bucket",
"DeletionPolicy": "Retain"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/cdk');
import codepipeline = require('../lib');

const app = new cdk.App();

const stack = new cdk.Stack(app, 'aws-cdk-codepipeline-s3-deploy');

const pipeline = new codepipeline.Pipeline(stack, 'Pipeline');

const sourceStage = new codepipeline.Stage(pipeline, 'Source', { pipeline });
const bucket = new s3.Bucket(stack, 'PipelineBucket', {
versioned: true,
removalPolicy: cdk.RemovalPolicy.Destroy,
});
const sourceAction = new s3.PipelineSourceAction(stack, 'Source', {
stage: sourceStage,
outputArtifactName: 'SourceArtifact',
bucket,
bucketKey: 'key',
});

const deployBucket = new s3.Bucket(stack, 'DeployBucket', {});

const deployStage = new codepipeline.Stage(pipeline, 'Deploy', { pipeline });
deployBucket.addToPipelineAsDeploy(deployStage, 'DeployAction', {
inputArtifact: sourceAction.outputArtifact,
});

app.run();
20 changes: 20 additions & 0 deletions packages/@aws-cdk/aws-s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@ const sourceAction = sourceBucket.addToPipeline(sourceStage, 'S3Source', {
});
```

### Buckets as deploy targets in CodePipeline

This package also defines an Action that allows you to use a
Bucket as a deployment target in CodePipeline:

```ts
import codepipeline = require('@aws-cdk/aws-codepipeline');
import s3 = require('@aws-cdk/aws-s3');

const targetBucket = new s3.Bucket(this, 'MyBucket', {});

const pipeline = new codepipeline.Pipeline(this, 'MyPipeline');
const deployStage = pipeline.addStage('Deploy');
const deployAction = new s3.PipelineDeployAction(this, 'S3Deploy', {
stage: deployStage,
bucket: targetBucket,
inputArtifact: sourceAction.outputArtifact,
});
```

### Sharing buckets between stacks

To use a bucket in a different stack in the same CDK application, pass the object to the other stack:
Expand Down
24 changes: 23 additions & 1 deletion packages/@aws-cdk/aws-s3/lib/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import cdk = require('@aws-cdk/cdk');
import { BucketPolicy } from './bucket-policy';
import { BucketNotifications } from './notifications-resource';
import perms = require('./perms');
import { CommonPipelineSourceActionProps, PipelineSourceAction } from './pipeline-action';
import {
CommonPipelineDeployActionProps, CommonPipelineSourceActionProps,
PipelineDeployAction, PipelineSourceAction
} from './pipeline-actions';
import { LifecycleRule } from './rule';
import { CfnBucket } from './s3.generated';
import { parseBucketArn, parseBucketName } from './util';
Expand Down Expand Up @@ -64,6 +67,17 @@ export interface IBucket extends cdk.IConstruct {
*/
addToPipeline(stage: actions.IStage, name: string, props: CommonPipelineSourceActionProps): PipelineSourceAction;

/**
* Convenience method for creating a new {@link PipelineDeployAction},
* and adding it to the given Stage.
*
* @param stage the Pipeline Stage to add the new Action to
* @param name the name of the newly created Action
* @param props the optional properties of the new Action
* @returns the newly created {@link PipelineDeployAction}
*/
addToPipelineAsDeploy(stage: actions.IStage, name: string, props?: CommonPipelineDeployActionProps): PipelineDeployAction;

/**
* Adds a statement to the resource policy for a principal (i.e.
* account/role/service) to perform actions on this bucket and/or it's
Expand Down Expand Up @@ -301,6 +315,14 @@ export abstract class BucketBase extends cdk.Construct implements IBucket {
});
}

public addToPipelineAsDeploy(stage: actions.IStage, name: string, props: CommonPipelineDeployActionProps = {}): PipelineDeployAction {
return new PipelineDeployAction(this, name, {
stage,
bucket: this,
...props,
});
}

public onPutObject(name: string, target?: events.IEventRuleTarget, path?: string): events.EventRule {
const eventRule = new events.EventRule(this, name, {
eventPattern: {
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-s3/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './bucket';
export * from './bucket-policy';
export * from './pipeline-action';
export * from './pipeline-actions';
export * from './rule';

// AWS::S3 CloudFormation Resources:
Expand Down
Loading

0 comments on commit 8f1a5e8

Please sign in to comment.