Skip to content

Commit

Permalink
[BREAKING] Usability improvements for the CodeBuild Construct library. (
Browse files Browse the repository at this point in the history
#412)

1. Rename 'BuildProject' to 'Project'.
2. Allow setting the physical name of a Project, to make it consistent with other L2s.
3. Introduce a BuildImage class that makes it more convenient to specify the used Docker image.
4. Introduce a convenience PipelineProject class for use in CodePipeline that defaults the source and artifacts fields.
  • Loading branch information
skinny85 authored and Elad Ben-Israel committed Jul 29, 2018
1 parent 4f39f48 commit b8ebcbe
Show file tree
Hide file tree
Showing 17 changed files with 309 additions and 107 deletions.
22 changes: 18 additions & 4 deletions packages/@aws-cdk/aws-codebuild-codepipeline/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,31 @@ This module contains an Action that allows you to use a CodeBuild Project in Cod
Example:

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

// see the @aws-cdk/aws-codebuild module for more documentation on how to create CodeBuild Projects
const project = new codebuild.BuildProject( // ...
);
const project = new codebuildPipeline.PipelineProject(this, 'MyProject', {
// ...
});

const pipeline = new codepipeline.Pipeline(this, 'MyPipeline');
const buildStage = new codepipeline.Stage(pipeline, 'Build');
new codebuildPipeline.PipelineBuildAction(buildStage, 'CodeBuild', {
project: project,
project
});
```

The `PipelineProject` utility class is a simple sugar around the `Project`
class from the `@aws-cdk/aws-codebuild` module,
it's equivalent to:

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

const project = new codebuild.Project(this, 'MyProject', {
source: new codebuild.CodePipelineSource(),
artifacts: new codebuild.CodePipelineBuildArtifacts(),
// rest of the properties from PipelineProject are passed unchanged...
}
```
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-codebuild-codepipeline/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './pipeline-actions';
export * from './pipeline-project';
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface PipelineBuildActionProps {
/**
* The build project
*/
project: codebuild.BuildProjectRef;
project: codebuild.ProjectRef;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import codebuild = require('@aws-cdk/aws-codebuild');
import cdk = require('@aws-cdk/cdk');

// tslint:disable-next-line:no-empty-interface
export interface PipelineProjectProps extends codebuild.CommonProjectProps {
}

/**
* A convenience class for CodeBuild Projects that are used in CodePipeline.
*/
export class PipelineProject extends codebuild.Project {
constructor(parent: cdk.Construct, id: string, props?: PipelineProjectProps) {
super(parent, id, {
source: new codebuild.CodePipelineSource(),
artifacts: new codebuild.CodePipelineBuildArtifacts(),
...props
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const source = new codecommitPipeline.PipelineSource(sourceStage, 'source', {
});

const buildStage = new codepipeline.Stage(pipeline, 'build');
const project = new codebuild.BuildProject(stack, 'MyBuildProject', {
const project = new codebuild.Project(stack, 'MyBuildProject', {
source: new codebuild.CodePipelineSource(),
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Use pipeline as CloudWAtch event target

import codebuild = require('@aws-cdk/aws-codebuild');
import codecommit = require('@aws-cdk/aws-codecommit');
import codecommitPipeline = require('@aws-cdk/aws-codecommit-codepipeline');
import codepipeline = require('@aws-cdk/aws-codepipeline');
Expand All @@ -17,7 +16,7 @@ const sourceStage = new codepipeline.Stage(pipeline, 'Source');
const buildStage = new codepipeline.Stage(pipeline, 'Build');

const repository = new codecommit.Repository(stack, 'CodeCommitRepo', { repositoryName: 'foo' });
const project = new codebuild.BuildProject(stack, 'BuildProject', { source: new codebuild.CodePipelineSource() });
const project = new codebuildPipeline.PipelineProject(stack, 'BuildProject');

const sourceAction = new codecommitPipeline.PipelineSource(sourceStage, 'CodeCommitSource', { artifactName: 'Source', repository });
new codebuildPipeline.PipelineBuildAction(buildStage, 'CodeBuildAction', { inputArtifact: sourceAction.artifact, project });
Expand Down
44 changes: 40 additions & 4 deletions packages/@aws-cdk/aws-codebuild-codepipeline/test/test.pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import s3 = require('@aws-cdk/aws-s3');
import sns = require('@aws-cdk/aws-sns');
import cdk = require('@aws-cdk/cdk');
import { Test } from 'nodeunit';
import { PipelineBuildAction } from '../lib';
import codebuildPipeline = require('../lib');

// tslint:disable:object-literal-key-quotes

Expand All @@ -26,11 +26,11 @@ export = {
});

const buildStage = new codepipeline.Stage(pipeline, 'build');
const project = new codebuild.BuildProject(stack, 'MyBuildProject', {
const project = new codebuild.Project(stack, 'MyBuildProject', {
source: new codebuild.CodePipelineSource()
});

new PipelineBuildAction(buildStage, 'build', {
new codebuildPipeline.PipelineBuildAction(buildStage, 'build', {
project,
inputArtifact: source.artifact
});
Expand Down Expand Up @@ -206,5 +206,41 @@ export = {

test.deepEqual([], pipeline.validate());
test.done();
}
},

'PipelineProject': {
'with a custom Project Name': {
'sets the source and artifacts to CodePipeline'(test: Test) {
const stack = new cdk.Stack();

new codebuildPipeline.PipelineProject(stack, 'MyProject', {
projectName: 'MyProject',
});

expect(stack).to(haveResource('AWS::CodeBuild::Project', {
"Name": "MyProject",
"Source": {
"Type": "CODEPIPELINE"
},
"Artifacts": {
"Type": "CODEPIPELINE"
},
"ServiceRole": {
"Fn::GetAtt": [
"MyProjectRole9BBE5233",
"Arn"
]
},
"Environment": {
"Type": "LINUX_CONTAINER",
"PrivilegedMode": false,
"Image": "aws/codebuild/ubuntu-base:14.04",
"ComputeType": "BUILD_GENERAL1_SMALL"
}
}));

test.done();
}
}
},
};
32 changes: 20 additions & 12 deletions packages/@aws-cdk/aws-codebuild/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,48 @@ Define a project. This will also create an IAM Role and IAM Policy for CodeBuild
Create a CodeBuild project with CodePipeline as the source:

```ts
new BuildProject(this, 'MyFirstProject', {
source: new CodePipelineSource()
import codebuild = require('@aws-cdk/aws-codebuild');

new codebuild.Project(this, 'MyFirstProject', {
source: new codebuild.CodePipelineSource()
});
```

Create a CodeBuild project with CodeCommit as the source:

```ts
const repo = new Repository(this, 'MyRepo', { repositoryName: 'foo' });
new BuildProject(this, 'MyFirstCodeCommitProject', {
source: new CodeCommitSource(repo)
import codebuild = require('@aws-cdk/aws-codebuild');
import codecommit = require('@aws-cdk/aws-codecommit');

const repo = new codecommit.Repository(this, 'MyRepo', { repositoryName: 'foo' });
new codebuild.Project(this, 'MyFirstCodeCommitProject', {
source: new codebuild.CodeCommitSource(repo)
});
```

Create a CodeBuild project with an S3 bucket as the source:

```ts
const bucket = new Bucket(this, 'MyBucket');
new BuildProject(this, 'MyProject', {
source: new S3BucketSource(bucket, 'path/to/source.zip')
import codebuild = require('@aws-cdk/aws-codebuild');
import s3 = require('@aws-cdk/aws-s3');

const bucket = new s3.Bucket(this, 'MyBucket');
new codebuild.Project(this, 'MyProject', {
source: new codebuild.S3BucketSource(bucket, 'path/to/source.zip')
});
```

### Using BuildProject as an event target
### Using Project as an event target

The `BuildProject` construct implements the `IEventRuleTarget` interface. This means that it can be
The `Project` construct implements the `IEventRuleTarget` interface. This means that it can be
used as a target for event rules:

```ts
// start build when a commit is pushed
codeCommitRepository.onCommit('OnCommit', buildProject);
codeCommitRepository.onCommit('OnCommit', project);
```

### Using BuildProject as an event source
### Using Project as an event source

To define CloudWatch event rules for build projects, use one of the `onXxx` methods:

Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-codebuild/lib/artifacts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import s3 = require('@aws-cdk/aws-s3');
import { cloudformation } from './codebuild.generated';
import { BuildProject } from './project';
import { Project } from './project';

export abstract class BuildArtifacts {
public abstract toArtifactsJSON(): cloudformation.ProjectResource.ArtifactsProperty;
public bind(_project: BuildProject) {
public bind(_project: Project) {
return;
}
}
Expand Down Expand Up @@ -64,7 +64,7 @@ export class S3BucketBuildArtifacts extends BuildArtifacts {
super();
}

public bind(project: BuildProject) {
public bind(project: Project) {
this.props.bucket.grantReadWrite(project.role);
}

Expand Down
Loading

0 comments on commit b8ebcbe

Please sign in to comment.