Skip to content

Commit c3667d7

Browse files
skinny85Elad Ben-Israel
authored andcommitted
fix(codebuild): API cleanup. (#2745)
Change the names of the source classes to be more consistent with other integrations. Make the abstract Source classes package-private. Add static factory methods to Source. Change the names of artifacts classes to be more consistent with other integrations. Add static factory methods to Artifacts. Make CodePipelineSource, CodePipelineArtifacts, NoSource, and NoArtifacts package-private. Get rid of the SourceType enum. BREAKING CHANGE: * codebuild: rename BuildSource to Source, S3BucketSource to S3Source, BuildArtifacts to Artifacts, S3BucketBuildArtifacts to S3Artifacts * codebuild: the classes CodePipelineBuildSource, CodePipelineBuildArtifacts, NoBuildSource, and NoBuildArtifacts have been removed * codebuild: rename buildScriptAsset and buildScriptAssetEntrypoint to buildScript and buildScriptEntrypoint, respectively
1 parent aa61dfb commit c3667d7

25 files changed

+499
-898
lines changed

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

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,10 @@ methods and attributes.
4242
## Source
4343

4444
Build projects are usually associated with a _source_, which is specified via
45-
the `source` property which accepts a class that extends the `BuildSource`
46-
abstract base class. The supported sources are:
47-
48-
### `NoSource`
49-
50-
This is the default and implies that no source is associated with this
51-
build project.
52-
53-
The `buildSpec` option is required in this case.
45+
the `source` property which accepts a class that extends the `Source`
46+
abstract base class.
47+
The default is to have no source associated with the build project;
48+
the `buildSpec` option is required in that case.
5449

5550
Here's a CodeBuild project with no source which simply prints `Hello,
5651
CodeBuild!`:
@@ -67,11 +62,11 @@ import codecommit = require('@aws-cdk/aws-codecommit');
6762

6863
const repository = new codecommit.Repository(this, 'MyRepo', { repositoryName: 'foo' });
6964
new codebuild.Project(this, 'MyFirstCodeCommitProject', {
70-
source: new codebuild.CodeCommitSource({ repository }),
65+
source: codebuild.Source.codeCommit({ repository }),
7166
});
7267
```
7368

74-
### `S3BucketSource`
69+
### `S3Source`
7570

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

@@ -81,25 +76,20 @@ import s3 = require('@aws-cdk/aws-s3');
8176

8277
const bucket = new s3.Bucket(this, 'MyBucket');
8378
new codebuild.Project(this, 'MyProject', {
84-
source: new codebuild.S3BucketSource({
79+
source: codebuild.Source.s3({
8580
bucket: bucket,
8681
path: 'path/to/file.zip',
8782
}),
8883
});
8984
```
9085

91-
### `CodePipelineSource`
92-
93-
Used as a special source type when a CodeBuild project is used as a
94-
CodePipeline action.
95-
9686
### `GitHubSource` and `GitHubEnterpriseSource`
9787

9888
These source types can be used to build code from a GitHub repository.
9989
Example:
10090

10191
```typescript
102-
const gitHubSource = new codebuild.GitHubSource({
92+
const gitHubSource = codebuild.Source.gitHub({
10393
owner: 'awslabs',
10494
repo: 'aws-cdk',
10595
webhook: true, // optional, default: true if `webhookFilteres` were provided, false otherwise
@@ -121,6 +111,23 @@ aws codebuild import-source-credentials --server-type GITHUB --auth-type PERSONA
121111

122112
This source type can be used to build code from a BitBucket repository.
123113

114+
## CodePipeline
115+
116+
To add a CodeBuild Project as an Action to CodePipeline,
117+
use the `PipelineProject` class instead of `Project`.
118+
It's a simple class that doesn't allow you to specify `sources`,
119+
`secondarySources`, `artifacts` or `secondaryArtifacts`,
120+
as these are handled by setting input and output CodePipeline `Artifact` instances on the Action,
121+
instead of setting them on the Project.
122+
123+
```typescript
124+
const project = new codebuild.PipelineProject(this, 'Project', {
125+
// properties as above...
126+
})
127+
```
128+
129+
For more details, see the readme of the `@aws-cdk/@aws-codepipeline` package.
130+
124131
## Caching
125132

126133
You can save time when your project builds by using a cache. A cache can store reusable pieces of your build environment and use them across multiple builds. Your build project can use one of two types of caching: Amazon S3 or local. In general, S3 caching is a good option for small and intermediate build artifacts that are more expensive to build than to download. Local caching is a good option for large intermediate build artifacts because the cache is immediately available on the build host.
@@ -225,13 +232,13 @@ multiple outputs. For example:
225232
```ts
226233
const project = new codebuild.Project(this, 'MyProject', {
227234
secondarySources: [
228-
new codebuild.CodeCommitSource({
235+
codebuild.Source.codeCommit({
229236
identifier: 'source2',
230237
repository: repo,
231238
}),
232239
],
233240
secondaryArtifacts: [
234-
new codebuild.S3BucketBuildArtifacts({
241+
codebuild.Artifacts.s3({
235242
identifier: 'artifact2',
236243
bucket: bucket,
237244
path: 'some/path',
@@ -324,7 +331,7 @@ const securityGroup = new ec2.SecurityGroup(stack, 'SecurityGroup1', {
324331
vpc: vpc,
325332
});
326333
new Project(stack, 'MyProject', {
327-
buildScriptAsset: new assets.ZipDirectoryAsset(stack, 'Bundle', { path: 'script_bundle' }),
334+
buildScript: new assets.ZipDirectoryAsset(stack, 'Bundle', { path: 'script_bundle' }),
328335
securityGroups: [securityGroup],
329336
vpc: vpc
330337
});
Lines changed: 64 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,83 @@
11
import s3 = require('@aws-cdk/aws-s3');
2+
import { Construct } from '@aws-cdk/cdk';
23
import { CfnProject } from './codebuild.generated';
3-
import { Project } from './project';
4+
import { IProject } from './project';
45

56
/**
6-
* Properties common to all Artifacts classes.
7+
* The type returned from {@link IArtifacts#bind}.
78
*/
8-
export interface BuildArtifactsProps {
9+
export interface ArtifactsConfig {
910
/**
10-
* The artifact identifier.
11-
* This property is required on secondary artifacts.
11+
* The low-level CloudFormation artifacts property.
1212
*/
13-
readonly identifier?: string;
13+
readonly artifactsProperty: CfnProject.ArtifactsProperty;
1414
}
1515

1616
/**
17-
* Artifacts definition for a CodeBuild Project.
17+
* The abstract interface of a CodeBuild build output.
18+
* Implemented by {@link Artifacts}.
1819
*/
19-
export abstract class BuildArtifacts {
20-
public readonly identifier?: string;
21-
protected abstract readonly type: string;
22-
23-
constructor(props: BuildArtifactsProps) {
24-
this.identifier = props.identifier;
25-
}
26-
20+
export interface IArtifacts {
2721
/**
28-
* @internal
22+
* The artifact identifier.
23+
* This property is required on secondary artifacts.
2924
*/
30-
public _bind(_project: Project) {
31-
return;
32-
}
25+
readonly identifier?: string;
3326

34-
public toArtifactsJSON(): CfnProject.ArtifactsProperty {
35-
const artifactsProp = this.toArtifactsProperty();
36-
return {
37-
artifactIdentifier: this.identifier,
38-
type: this.type,
39-
...artifactsProp,
40-
};
41-
}
27+
/**
28+
* The CodeBuild type of this artifact.
29+
*/
30+
readonly type: string;
4231

43-
protected toArtifactsProperty(): any {
44-
return {
45-
};
46-
}
32+
/**
33+
* Callback when an Artifacts class is used in a CodeBuild Project.
34+
*
35+
* @param scope a root Construct that allows creating new Constructs
36+
* @param project the Project this Artifacts is used in
37+
*/
38+
bind(scope: Construct, project: IProject): ArtifactsConfig;
4739
}
4840

4941
/**
50-
* A `NO_ARTIFACTS` CodeBuild Project Artifact definition.
51-
* This is the default artifact type,
52-
* if none was specified when creating the Project
53-
* (and the source was not specified to be CodePipeline).
54-
* *Note*: the `NO_ARTIFACTS` type cannot be used as a secondary artifact,
55-
* and because of that, you're not allowed to specify an identifier for it.
42+
* Properties common to all Artifacts classes.
5643
*/
57-
export class NoBuildArtifacts extends BuildArtifacts {
58-
protected readonly type = 'NO_ARTIFACTS';
59-
60-
constructor() {
61-
super({});
62-
}
44+
export interface ArtifactsProps {
45+
/**
46+
* The artifact identifier.
47+
* This property is required on secondary artifacts.
48+
*/
49+
readonly identifier?: string;
6350
}
6451

6552
/**
66-
* CodePipeline Artifact definition for a CodeBuild Project.
67-
* *Note*: this type cannot be used as a secondary artifact,
68-
* and because of that, you're not allowed to specify an identifier for it.
53+
* Artifacts definition for a CodeBuild Project.
6954
*/
70-
export class CodePipelineBuildArtifacts extends BuildArtifacts {
71-
protected readonly type = 'CODEPIPELINE';
55+
export abstract class Artifacts implements IArtifacts {
56+
public static s3(props: S3ArtifactsProps): Artifacts {
57+
return new S3Artifacts(props);
58+
}
7259

73-
constructor() {
74-
super({});
60+
public readonly identifier?: string;
61+
public abstract readonly type: string;
62+
63+
protected constructor(props: ArtifactsProps) {
64+
this.identifier = props.identifier;
65+
}
66+
67+
public bind(_scope: Construct, _project: IProject): ArtifactsConfig {
68+
return {
69+
artifactsProperty: {
70+
artifactIdentifier: this.identifier,
71+
type: this.type,
72+
},
73+
};
7574
}
7675
}
7776

7877
/**
79-
* Construction properties for {@link S3BucketBuildArtifacts}.
78+
* Construction properties for {@link S3Artifacts}.
8079
*/
81-
export interface S3BucketBuildArtifactsProps extends BuildArtifactsProps {
80+
export interface S3ArtifactsProps extends ArtifactsProps {
8281
/**
8382
* The name of the output bucket.
8483
*/
@@ -119,27 +118,25 @@ export interface S3BucketBuildArtifactsProps extends BuildArtifactsProps {
119118
/**
120119
* S3 Artifact definition for a CodeBuild Project.
121120
*/
122-
export class S3BucketBuildArtifacts extends BuildArtifacts {
123-
protected readonly type = 'S3';
121+
class S3Artifacts extends Artifacts {
122+
public readonly type = 'S3';
124123

125-
constructor(private readonly props: S3BucketBuildArtifactsProps) {
124+
constructor(private readonly props: S3ArtifactsProps) {
126125
super(props);
127126
}
128127

129-
/**
130-
* @internal
131-
*/
132-
public _bind(project: Project) {
128+
public bind(_scope: Construct, project: IProject): ArtifactsConfig {
133129
this.props.bucket.grantReadWrite(project);
134-
}
135-
136-
protected toArtifactsProperty(): any {
130+
const superConfig = super.bind(_scope, project);
137131
return {
138-
location: this.props.bucket.bucketName,
139-
path: this.props.path,
140-
namespaceType: this.props.includeBuildId === false ? 'NONE' : 'BUILD_ID',
141-
name: this.props.name,
142-
packaging: this.props.packageZip === false ? 'NONE' : 'ZIP',
132+
artifactsProperty: {
133+
...superConfig.artifactsProperty,
134+
location: this.props.bucket.bucketName,
135+
path: this.props.path,
136+
namespaceType: this.props.includeBuildId === false ? 'NONE' : 'BUILD_ID',
137+
name: this.props.name,
138+
packaging: this.props.packageZip === false ? 'NONE' : 'ZIP',
139+
}
143140
};
144141
}
145142
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Artifacts } from './artifacts';
2+
3+
/**
4+
* CodePipeline Artifact definition for a CodeBuild Project.
5+
* *Note*: this type cannot be used as a secondary artifact,
6+
* and because of that, you're not allowed to specify an identifier for it.
7+
*/
8+
export class CodePipelineArtifacts extends Artifacts {
9+
public readonly type = 'CODEPIPELINE';
10+
11+
constructor() {
12+
super({});
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Source } from './source';
2+
import { CODEPIPELINE_SOURCE_ARTIFACTS_TYPE } from './source-types';
3+
4+
/**
5+
* CodePipeline Source definition for a CodeBuild Project.
6+
* *Note*: this type cannot be used as a secondary source,
7+
* and because of that, you're not allowed to specify an identifier for it.
8+
*/
9+
export class CodePipelineSource extends Source {
10+
public readonly type = CODEPIPELINE_SOURCE_ARTIFACTS_TYPE;
11+
12+
constructor() {
13+
super({});
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Artifacts } from './artifacts';
2+
3+
/**
4+
* A `NO_ARTIFACTS` CodeBuild Project Artifact definition.
5+
* This is the default artifact type,
6+
* if none was specified when creating the Project
7+
* (and the source was not specified to be CodePipeline).
8+
* *Note*: the `NO_ARTIFACTS` type cannot be used as a secondary artifact,
9+
* and because of that, you're not allowed to specify an identifier for it.
10+
*
11+
* This class is private to the @aws-codebuild package.
12+
*/
13+
export class NoArtifacts extends Artifacts {
14+
public readonly type = 'NO_ARTIFACTS';
15+
16+
constructor() {
17+
super({});
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Source } from './source';
2+
import { NO_SOURCE_TYPE } from './source-types';
3+
4+
/**
5+
* A `NO_SOURCE` CodeBuild Project Source definition.
6+
* This is the default source type,
7+
* if none was specified when creating the Project.
8+
* *Note*: the `NO_SOURCE` type cannot be used as a secondary source,
9+
* and because of that, you're not allowed to specify an identifier for it.
10+
*
11+
* This class is private to the aws-codebuild package.
12+
*/
13+
export class NoSource extends Source {
14+
public readonly type = NO_SOURCE_TYPE;
15+
16+
constructor() {
17+
super({});
18+
}
19+
}

packages/@aws-cdk/aws-codebuild/lib/pipeline-project.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cdk = require('@aws-cdk/cdk');
2-
import { CodePipelineBuildArtifacts } from './artifacts';
2+
import { CodePipelineArtifacts } from './codepipeline-artifacts';
3+
import { CodePipelineSource } from './codepipeline-source';
34
import { CommonProjectProps, Project } from './project';
4-
import { CodePipelineSource } from './source';
55

66
// tslint:disable-next-line:no-empty-interface
77
export interface PipelineProjectProps extends CommonProjectProps {
@@ -14,7 +14,7 @@ export class PipelineProject extends Project {
1414
constructor(scope: cdk.Construct, id: string, props?: PipelineProjectProps) {
1515
super(scope, id, {
1616
source: new CodePipelineSource(),
17-
artifacts: new CodePipelineBuildArtifacts(),
17+
artifacts: new CodePipelineArtifacts(),
1818
...props
1919
});
2020
}

0 commit comments

Comments
 (0)