Improve Build Performance and Save Time Using Local Caching in AWS CodeBuild
AWS CodeBuild now supports local caching, which makes it possible for you to persist intermediate build artifacts locally on the build host so that they are available for reuse in subsequent build runs.
Your build project can use one of two types of caching: Amazon S3 or local. In this blog post, we will discuss how to use the local caching feature.
Local caching stores a cache on a build host. The cache is available to that build host only for a limited time and until another build is complete. For example, when you are dealing with large Java projects, compilation might take a long time. You can speed up subsequent builds by using local caching. This is a good option for large intermediate build artifacts because the cache is immediately available on the build host.
Local caching increases build performance for:
- Projects with a large, monolithic source code repository.
- Projects that generate and reuse many intermediate build artifacts.
- Projects that build large Docker images.
- Projects with many source dependencies.
To use local caching
1. Open AWS CodeBuild console at https://console.aws.amazon.com/codesuite/codebuild/home.
2. Choose Create project.
3. In Project configuration, enter a name and description for the build project.
4. In Source, for Source provider, choose the source code provider type. In this example, we use an AWS CodeCommit repository name.
5. For Environment image, choose Managed image or Custom image, as appropriate. For environment type, choose Linux or Windows Server. Specify a runtime, runtime version, and service role for your project.
6. Configure the buildspec file for your project.
7. In Artifacts, expand Additional Configuration. For Cache type, choose Local, as shown here.
Local caching supports the following caching modes:
Source cache mode caches Git metadata for primary and secondary sources. After the cache is created, subsequent builds pull only the change between commits. This mode is a good choice for projects with a clean working directory and a source that is a large Git repository. If you choose this option and your project does not use a Git repository (GitHub, GitHub Enterprise, or Bitbucket), the option is ignored. No changes are required in the buildspec file.
Docker layer cache mode caches existing Docker layers. This mode is a good choice for projects that build or pull large Docker images. It can prevent the performance issues caused by pulling large Docker images down from the network.
- You can use a Docker layer cache in the Linux environment only.
- The privileged flag must be set so that your project has the required Docker permissions
- You should consider the security implications before you use a Docker layer cache.
Custom cache mode caches directories you specify in the buildspec file. This mode is a good choice if your build scenario is not suited to one of the other two local cache modes. If you use a custom cache:
- Only directories can be specified for caching. You cannot specify individual files.
- Symlinks are used to reference cached directories.
- Cached directories are linked to your build before it downloads its project sources. Cached items are overridden if a source item has the same name. Directories are specified using cache paths in the buildspec file.
To use source cache mode
In the build project configuration, under Artifacts, expand Additional Configuration. For Cache type, choose Local. Select Source cache, as shown here.
To use Docker layer cache mode
In the build project configuration, under Artifacts, expand Additional Configuration. For Cache type, choose Local. Select Docker layer cache, as shown here.
Under Privileged, select Enable this flag if you want to build Docker images or want your builds to get elevated privileges. This grants elevated privileges to the Docker process running on the build host.
To use custom cache mode
In your buildspec file, specify the cache path, as shown here.
In the build project configuration, under Artifacts, expand Additional Configuration. For Cache type, choose Local. Select Custom cache, as shown here.
version: 0.2 phases: pre_build: commands: - echo "Enter pre_build commands" build: commands: - echo "Enter build commands" cache: paths: - '/root/.m2/**/*' - '/root/.npm/**/*' - 'build/**/*'
We hope you find the information in this post helpful. If you have feedback, please leave it in the Comments section below. If you have questions, start a new thread on the AWS CodeBuild forum or contact AWS Support.