-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(codebuild): add support for local cache modes #2529
Merged
eladb
merged 7 commits into
aws:master
from
SanderKnape:sanderknape/codebuild-cache-modes
May 17, 2019
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
417fa6d
feat(codebuild): add support for local cache modes
SanderKnape 056e24a
Implement requested changes
SanderKnape a75f0e8
add additional documentation regarding local caching
SanderKnape 5c65d48
refactor codebuild caching strategy to use DI
SanderKnape f2fc1be
implement requested changes
SanderKnape c92a86c
small refactor
SanderKnape 1923a7f
update documentation
SanderKnape File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,36 @@ aws codebuild import-source-credentials --server-type GITHUB --auth-type PERSONA | |
|
||
This source type can be used to build code from a BitBucket repository. | ||
|
||
## 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. | ||
|
||
### S3 Caching | ||
|
||
With S3 caching, the cache is stored in an S3 bucket which is available from multiple hosts. | ||
|
||
```typescript | ||
new codebuild.Project(this, 'Project', { | ||
source: new codebuild.CodePipelineSource(), | ||
cache: Cache.bucket(new Bucket(this, 'Bucket')) | ||
}); | ||
``` | ||
|
||
### Local Caching | ||
|
||
With local caching, the cache is stored on the codebuild instance itself. CodeBuild cannot guarantee a reuse of instance. For example, when a build starts and caches files locally, if two subsequent builds start at the same time afterwards only one of those builds would get the cache. Three different cache modes are supported: | ||
|
||
* `LocalCacheMode.Source` caches Git metadata for primary and secondary sources. | ||
* `LocalCacheMode.DockerLayer` caches existing Docker layers. | ||
* `LocalCacheMode.Custom` caches directories you specify in the buildspec file. | ||
|
||
```typescript | ||
new codebuild.Project(this, 'Project', { | ||
source: new codebuild.CodePipelineSource(), | ||
cache: Cache.local(LocalCacheMode.DockerLayer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Show an example with two modes to demonstrate that this is a list |
||
}); | ||
``` | ||
|
||
## Environment | ||
|
||
By default, projects use a small instance with an Ubuntu 18.04 image. You | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { IBucket } from "@aws-cdk/aws-s3"; | ||
import { Aws, Fn } from "@aws-cdk/cdk"; | ||
import { CfnProject } from "./codebuild.generated"; | ||
import { IProject } from "./project"; | ||
|
||
export interface BucketCacheOptions { | ||
/** | ||
* The prefix to use to store the cache in the bucket | ||
*/ | ||
readonly prefix?: string; | ||
} | ||
|
||
/** | ||
* Local cache modes to enable for the CodeBuild Project | ||
*/ | ||
export enum LocalCacheMode { | ||
eladb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* Caches Git metadata for primary and secondary sources | ||
*/ | ||
Source = 'LOCAL_SOURCE_CACHE', | ||
|
||
/** | ||
* Caches existing Docker layers | ||
*/ | ||
DockerLayer = 'LOCAL_DOCKER_LAYER_CACHE', | ||
|
||
/** | ||
* Caches directories you specify in the buildspec file | ||
*/ | ||
Custom = 'LOCAL_CUSTOM_CACHE', | ||
} | ||
|
||
/** | ||
* Cache options for CodeBuild Project. | ||
* A cache can store reusable pieces of your build environment and use them across multiple builds. | ||
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/build-caching.html | ||
eladb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
export abstract class Cache { | ||
public static none(): Cache { | ||
return { _toCloudFormation: () => undefined, _bind: () => { return; } }; | ||
} | ||
|
||
/** | ||
* Create a local caching strategy. | ||
* @param modes the mode(s) to enable for local caching | ||
*/ | ||
public static local(...modes: LocalCacheMode[]): Cache { | ||
return { | ||
_toCloudFormation: () => ({ | ||
type: 'LOCAL', | ||
modes | ||
}), | ||
_bind: () => { return; } | ||
}; | ||
} | ||
|
||
/** | ||
* Create an S3 caching strategy. | ||
* @param bucket the S3 bucket to use for caching | ||
* @param options additional options to pass to the S3 caching | ||
*/ | ||
public static bucket(bucket: IBucket, options?: BucketCacheOptions): Cache { | ||
return { | ||
_toCloudFormation: () => ({ | ||
type: 'S3', | ||
location: Fn.join('/', [bucket.bucketName, options && options.prefix || Aws.noValue]) | ||
}), | ||
_bind: (project) => { | ||
bucket.grantReadWrite(project); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public abstract _toCloudFormation(): CfnProject.ProjectCacheProperty | undefined; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public abstract _bind(project: IProject): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be codebuild.Cache