Skip to content

Commit

Permalink
Rename BuildSources, and make some of them package-private.
Browse files Browse the repository at this point in the history
  • Loading branch information
skinny85 committed Jun 12, 2019
1 parent b80ef04 commit a39a748
Show file tree
Hide file tree
Showing 20 changed files with 280 additions and 282 deletions.
49 changes: 31 additions & 18 deletions packages/@aws-cdk/aws-codebuild/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,10 @@ methods and attributes.
## Source

Build projects are usually associated with a _source_, which is specified via
the `source` property which accepts a class that extends the `BuildSource`
abstract base class. The supported sources are:

### `NoSource`

This is the default and implies that no source is associated with this
build project.

The `buildSpec` option is required in this case.
the `source` property which accepts a class that extends the `Source`
abstract base class.
The default is to have no source associated with the build project;
the `buildSpec` option is required in that case.

Here's a CodeBuild project with no source which simply prints `Hello,
CodeBuild!`:
Expand All @@ -69,9 +64,10 @@ const repository = new codecommit.Repository(this, 'MyRepo', { repositoryName: '
new codebuild.Project(this, 'MyFirstCodeCommitProject', {
source: new codebuild.CodeCommitSource({ repository }),
});
// or: codebuild.Source.codeCommit({ repository })
```

### `S3BucketSource`
### `S3Source`

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

Expand All @@ -81,18 +77,17 @@ import s3 = require('@aws-cdk/aws-s3');

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

### `CodePipelineSource`

Used as a special source type when a CodeBuild project is used as a
CodePipeline action.

### `GitHubSource` and `GitHubEnterpriseSource`

These source types can be used to build code from a GitHub repository.
Expand All @@ -107,6 +102,7 @@ const gitHubSource = new codebuild.GitHubSource({
codebuild.FilterGroup.inEventOf(codebuild.EventAction.PUSH).andBranchIs('master'),
], // optional, by default all pushes and Pull Requests will trigger a build
});
// or: codebuild.Source.gitHub({ ... })
```

To provide GitHub credentials, please either go to AWS CodeBuild Console to connect
Expand All @@ -121,6 +117,23 @@ aws codebuild import-source-credentials --server-type GITHUB --auth-type PERSONA

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

## CodePipeline

To add a CodeBuild Project as an Action to CodePipeline,
use the `PipelineProject` class instead of `Project`.
It's a simple class that doesn't allow you to specify `sources`,
`secondarySources`, `artifacts` or `secondaryArtifacts`,
as these are handled by setting input and output CodePipeline `Artifact` instances on the Action,
instead of setting them on the Project.

```typescript
const project = new codebuild.PipelineProject(this, 'Project', {
// properties as above...
})
```

For more details, see the readme of the `@aws-cdk/@aws-codepipeline` package.

## Caching

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.
Expand Down Expand Up @@ -231,7 +244,7 @@ const project = new codebuild.Project(this, 'MyProject', {
}),
],
secondaryArtifacts: [
new codebuild.S3BucketBuildArtifacts({
new codebuild.S3Artifacts({
identifier: 'artifact2',
bucket: bucket,
path: 'some/path',
Expand Down Expand Up @@ -324,7 +337,7 @@ const securityGroup = new ec2.SecurityGroup(stack, 'SecurityGroup1', {
vpc: vpc,
});
new Project(stack, 'MyProject', {
buildScriptAsset: new assets.ZipDirectoryAsset(stack, 'Bundle', { path: 'script_bundle' }),
buildScript: new assets.ZipDirectoryAsset(stack, 'Bundle', { path: 'script_bundle' }),
securityGroups: [securityGroup],
vpc: vpc
});
Expand Down
47 changes: 11 additions & 36 deletions packages/@aws-cdk/aws-codebuild/lib/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Project } from './project';
/**
* Properties common to all Artifacts classes.
*/
export interface BuildArtifactsProps {
export interface ArtifactsProps {
/**
* The artifact identifier.
* This property is required on secondary artifacts.
Expand All @@ -16,11 +16,15 @@ export interface BuildArtifactsProps {
/**
* Artifacts definition for a CodeBuild Project.
*/
export abstract class BuildArtifacts {
export abstract class Artifacts {
public static s3(props: S3ArtifactsProps): S3Artifacts {
return new S3Artifacts(props);
}

public readonly identifier?: string;
protected abstract readonly type: string;

constructor(props: BuildArtifactsProps) {
constructor(props: ArtifactsProps) {
this.identifier = props.identifier;
}

Expand All @@ -47,38 +51,9 @@ export abstract class BuildArtifacts {
}

/**
* A `NO_ARTIFACTS` CodeBuild Project Artifact definition.
* This is the default artifact type,
* if none was specified when creating the Project
* (and the source was not specified to be CodePipeline).
* *Note*: the `NO_ARTIFACTS` type cannot be used as a secondary artifact,
* and because of that, you're not allowed to specify an identifier for it.
*/
export class NoBuildArtifacts extends BuildArtifacts {
protected readonly type = 'NO_ARTIFACTS';

constructor() {
super({});
}
}

/**
* CodePipeline Artifact definition for a CodeBuild Project.
* *Note*: this type cannot be used as a secondary artifact,
* and because of that, you're not allowed to specify an identifier for it.
*/
export class CodePipelineBuildArtifacts extends BuildArtifacts {
protected readonly type = 'CODEPIPELINE';

constructor() {
super({});
}
}

/**
* Construction properties for {@link S3BucketBuildArtifacts}.
* Construction properties for {@link S3Artifacts}.
*/
export interface S3BucketBuildArtifactsProps extends BuildArtifactsProps {
export interface S3ArtifactsProps extends ArtifactsProps {
/**
* The name of the output bucket.
*/
Expand Down Expand Up @@ -119,10 +94,10 @@ export interface S3BucketBuildArtifactsProps extends BuildArtifactsProps {
/**
* S3 Artifact definition for a CodeBuild Project.
*/
export class S3BucketBuildArtifacts extends BuildArtifacts {
export class S3Artifacts extends Artifacts {
protected readonly type = 'S3';

constructor(private readonly props: S3BucketBuildArtifactsProps) {
constructor(private readonly props: S3ArtifactsProps) {
super(props);
}

Expand Down
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-codebuild/lib/codepipeline-artifacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Artifacts } from './artifacts';

/**
* CodePipeline Artifact definition for a CodeBuild Project.
* *Note*: this type cannot be used as a secondary artifact,
* and because of that, you're not allowed to specify an identifier for it.
*/
export class CodePipelineArtifacts extends Artifacts {
protected readonly type = 'CODEPIPELINE';

constructor() {
super({});
}
}
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-codebuild/lib/codepipeline-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Source } from './source';
import { CODEPIPELINE_SOURCE_ARTIFACTS_TYPE } from './source-types';

/**
* CodePipeline Source definition for a CodeBuild Project.
* *Note*: this type cannot be used as a secondary source,
* and because of that, you're not allowed to specify an identifier for it.
*/
export class CodePipelineSource extends Source {
public readonly type = CODEPIPELINE_SOURCE_ARTIFACTS_TYPE;

constructor() {
super({});
}
}
19 changes: 19 additions & 0 deletions packages/@aws-cdk/aws-codebuild/lib/no-artifacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Artifacts } from './artifacts';

/**
* A `NO_ARTIFACTS` CodeBuild Project Artifact definition.
* This is the default artifact type,
* if none was specified when creating the Project
* (and the source was not specified to be CodePipeline).
* *Note*: the `NO_ARTIFACTS` type cannot be used as a secondary artifact,
* and because of that, you're not allowed to specify an identifier for it.
*
* This class is private to the @aws-codebuild package.
*/
export class NoArtifacts extends Artifacts {
protected readonly type = 'NO_ARTIFACTS';

constructor() {
super({});
}
}
19 changes: 19 additions & 0 deletions packages/@aws-cdk/aws-codebuild/lib/no-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Source } from './source';
import { NO_SOURCE_TYPE } from './source-types';

/**
* A `NO_SOURCE` CodeBuild Project Source definition.
* This is the default source type,
* if none was specified when creating the Project.
* *Note*: the `NO_SOURCE` type cannot be used as a secondary source,
* and because of that, you're not allowed to specify an identifier for it.
*
* This class is private to the aws-codebuild package.
*/
export class NoSource extends Source {
public readonly type = NO_SOURCE_TYPE;

constructor() {
super({});
}
}
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-codebuild/lib/pipeline-project.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cdk = require('@aws-cdk/cdk');
import { CodePipelineBuildArtifacts } from './artifacts';
import { CodePipelineArtifacts } from './codepipeline-artifacts';
import { CodePipelineSource } from './codepipeline-source';
import { CommonProjectProps, Project } from './project';
import { CodePipelineSource } from './source';

// tslint:disable-next-line:no-empty-interface
export interface PipelineProjectProps extends CommonProjectProps {
Expand All @@ -14,7 +14,7 @@ export class PipelineProject extends Project {
constructor(scope: cdk.Construct, id: string, props?: PipelineProjectProps) {
super(scope, id, {
source: new CodePipelineSource(),
artifacts: new CodePipelineBuildArtifacts(),
artifacts: new CodePipelineArtifacts(),
...props
});
}
Expand Down
Loading

0 comments on commit a39a748

Please sign in to comment.