这些示例可用于在 CodeBuild 中体验预留容量实例集。
使用预留容量进行缓存示例
缓存可以存储构建环境的可重用部分,并在多个构建中使用它们。此示例演示了如何使用预留容量在构建项目中启用缓存。有关更多信息,请参阅 缓存构建以提高性能。
您可以先在项目设置中指定一种或多种缓存模式:
Cache:
Type: LOCAL
Modes:
- LOCAL_CUSTOM_CACHE
- LOCAL_DOCKER_LAYER_CACHE
- LOCAL_SOURCE_CACHE
注意
要使用 Docker 层缓存,请务必启用特权模式。
您的项目构建规范设置应如下所示:
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'
您可以先用新项目运行构建,为缓存做种子。完成后,您应该使用重写的构建规范开始另一个构建,如下所示:
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'