- Newest
- Most votes
- Most comments
AWS CodeBuild supports Amazon S3 Caching and Local caching. S3 works best for smaller artifacts as they won't be affected transfer speeds and for artifacts that need to be used across multiple build hosts. Local caching is only available on a single build host and is suited for larger artifacts. Local caching also supports Docker layer caching as a build mode which will be useful to you.
For more differences on the two and various option for each please refer to the documentation.
An example of local caching using the custom cache, docker layer cache, and local source cache modes in CloudFormation can also be found in the documentation. When using the custom cache mode you can specify the directories to cache (such as npm modules) in the buildspec file.
I hope this helped!
To cache Docker images and npm dependencies in AWS CodeBuild to avoid pulling them every time a build starts, you can use CodeBuild's caching feature. CodeBuild supports two types of caching:
1. Local caching: Cache directories, Docker layer cache, and custom cache. 2. S3 caching: Cache files and directories to S3.
Here’s how you can set up caching for Docker images and npm dependencies using local caching with a CodeBuild project defined in CloudFormation.
Step-by-Step Guide
1. Enable Local Caching in CodeBuild:
- Configure local caching in your CodeBuild project to cache both Docker layers and npm dependencies.
2. CloudFormation Template:
- Create or update your CloudFormation template to include the local caching configuration.
Example CloudFormation Template
Here’s a sample CloudFormation template to illustrate how to set up local caching for Docker images and npm dependencies in CodeBuild:
AWSTemplateFormatVersion: '2010-09-09'
Resources:
MyCodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Name: MyCodeBuildProject
Source:
Type: GITHUB
Location: https://github.com/my-repo/my-project
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_MEDIUM
Image: aws/codebuild/standard:5.0 # Change this to the appropriate image
PrivilegedMode: true # Required for Docker builds
ServiceRole: arn:aws:iam::123456789012:role/service-role/codebuild-service-role # Replace with your IAM role ARN
Artifacts:
Type: NO_ARTIFACTS
Cache:
Type: LOCAL
Modes:
- LOCAL_SOURCE_CACHE
- LOCAL_DOCKER_LAYER_CACHE
- LOCAL_CUSTOM_CACHE
SourceVersion: master
EnvironmentVariables:
- Name: NODE_ENV
Value: production
TimeoutInMinutes: 60
Buildspec File
Here is an example buildspec.yml file that uses the local cache for npm dependencies and Docker layers:
version: 0.2
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- $(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION)
- echo Build started on `date`
- echo Caching npm dependencies and Docker images
- if [ -d /root/.npm ]; then echo "NPM cache exists"; else echo "NPM cache does not exist"; fi
- if [ -d /root/.cache/docker ]; then echo "Docker cache exists"; else echo "Docker cache does not exist"; fi
build:
commands:
- echo Building the Docker image...
- docker build -t my-app .
- echo Running npm install...
- npm install
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker image to ECR...
- docker tag my-app:latest 123456789012.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/my-app:latest
- docker push 123456789012.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/my-app:latest
cache:
paths:
- '/root/.npm/**/*'
- '/root/.cache/docker/**/*'
Explanation
- Local Caching in CloudFormation: The Cache section in the CloudFormation template enables local caching for source, Docker layers, and custom files.
- Buildspec Configuration: The cache section in the buildspec.yml file specifies the paths to cache npm dependencies and Docker layers.
Additional Notes
- Local Source Cache: Caches your Git source repository.
- Local Docker Layer Cache: Caches intermediate layers for Docker builds, speeding up subsequent builds.
- Local Custom Cache: Caches custom directories and files, such as npm dependencies.
By configuring local caching for both Docker images and npm dependencies, you can significantly reduce the build time in AWS CodeBuild by reusing previously cached layers and dependencies.
Relevant content
- asked 2 years ago
- Accepted Answerasked a year ago
- asked 2 years ago
- AWS OFFICIALUpdated 3 months ago
- AWS OFFICIALUpdated 3 months ago
- AWS OFFICIALUpdated 19 days ago