Reserved capacity samples with AWS CodeBuild - AWS CodeBuild

Reserved capacity samples with AWS CodeBuild

These samples can be used to experiment with reserved capacity fleets in CodeBuild.

Caching with reserved capacity sample

A cache can store reusable pieces of your build environment and use them across multiple builds. This sample demonstrated how to enable caching within your build project using reserved capacity. For more information, see Cache builds to improve performance.

You can start by specifying one or more cache modes in your project settings:

Cache: Type: LOCAL Modes: - LOCAL_CUSTOM_CACHE - LOCAL_DOCKER_LAYER_CACHE - LOCAL_SOURCE_CACHE
Note

Make sure to enable privileged mode in order to use Docker layer cache.

Your project buildspec settings should look like the following:

version: 0.2 phases: build: commands: - echo testing local source cache - touch /codebuild/cache/workspace/foobar.txt - git checkout -b cached_branch - echo testing local docker layer cache - docker run alpine:3.14 2>&1 | grep 'Pulling from' || exit 1 - echo testing local custom cache - touch foo - mkdir bar && ln -s foo bar/foo2 - mkdir bar/bar && touch bar/bar/foo3 && touch bar/bar/foo4 - "[ -f foo ] || exit 1" - "[ -L bar/foo2 ] || exit 1" - "[ -f bar/bar/foo3 ] || exit 1" - "[ -f bar/bar/foo4 ] || exit 1" cache: paths: - './foo' - './bar/**/*' - './bar/bar/foo3'

You can start by running a build with the new project to seed the cache. Once that's complete, you should start another build with an overriding buildspec, similar to the following:

version: 0.2 phases: build: commands: - echo testing local source cache - git branch | if grep 'cached_branch'; then (exit 0); else (exit 1); fi - ls /codebuild/cache/workspace | if grep 'foobar.txt'; then (exit 0); else (exit 1); fi - echo testing local docker layer cache - docker run alpine:3.14 2>&1 | if grep 'Pulling from'; then (exit 1); else (exit 0); fi - echo testing local custom cache - "[ -f foo ] || exit 1" - "[ -L bar/foo2 ] || exit 1" - "[ -f bar/bar/foo3 ] || exit 1" - "[ -f bar/bar/foo4 ] || exit 1" cache: paths: - './foo' - './bar/**/*' - './bar/bar/foo3'