Build times can become a major problem when an application is built frequently through a CI/CD pipeline. Every build may need to download dependencies, install tools, compile source code, and prepare container image layers. When a pipeline uses multiple CI runners, the problem becomes more noticeable because each runner may start with an empty local environment.
Cloud Native Buildpacks help reduce this work by caching reusable build layers. However, a cache stored on one runner is not automatically available to another runner. If a build runs on a different machine, the application may repeat work that was already completed by an earlier build.
Sharing buildpack caches across CI runners solves this problem. Instead of depending only on local storage, teams can use a remote container registry, distributed cache storage, or shared infrastructure to make reusable build data available across machines.
This article explains how buildpack caching works, why local caches are insufficient for distributed pipelines, and how to configure remote cache images with the pack CLI. It also covers GitHub Actions, GitLab CI, cache keys, security, performance considerations, and common mistakes.
Understanding Buildpack Caching
Cloud Native Buildpacks convert application source code into production-ready container images without requiring developers to maintain a traditional Dockerfile.
A buildpack detects the application type, installs the required runtime and dependencies, and creates the layers needed to run the application.
For example, a Java application may require:
A Node.js application may require a Node.js runtime, npm dependencies, and application build output.
Downloading and preparing these components during every build consumes time and network resources.
Buildpack caching allows reusable layers to be restored during a later build. If a dependency has not changed, the buildpack may reuse its existing layer instead of downloading or compiling it again.
How the Buildpack Lifecycle Uses Cache
The Cloud Native Buildpacks lifecycle manages several stages during an application build.
The important stages include:
Analyze: Examines the previous application image and its metadata.
Detect: Determines which buildpacks can process the application.
Restore: Restores available cached layers.
Build: Executes buildpacks and creates or updates application layers.
Export: Produces the final application image and updates the cache.
The restore stage is particularly important. It makes previously stored layers available to buildpacks before they perform their work.
Consider a Spring Boot application with several dependencies. If the dependency versions remain unchanged, the buildpack may reuse the dependency layer. If the application source changes but its dependencies do not, the build can avoid repeating some expensive operations.
The amount of work avoided depends on the buildpack's caching strategy and whether the previous layers are still valid.
Why Local Buildpack Caches Are Not Enough
A local build cache works well when builds repeatedly execute on the same machine.
For example, a self-hosted runner may retain Docker volumes between pipeline executions. The next build can reuse the cache from the previous execution.
However, many modern CI environments use ephemeral runners.
An ephemeral runner is created for a job or workflow and removed after the job finishes. The next pipeline execution may run on a completely different machine.
Consider this scenario:
Pipeline | Runner | Available cache |
|---|
Build 1 | Runner A | Empty |
Build 2 | Runner B | Empty |
Build 3 | Runner C | Empty |
Build 4 | Runner A | Previous local cache |
Even though the application has already been built several times, runners B and C cannot access runner A's local cache.
This creates unnecessary dependency downloads, repeated compilation, and longer build times.
A remote cache provides a shared location that different runners can access.
Instead of depending on the filesystem of a particular runner, each runner restores the required layers from a remote location and publishes updated cache information after the build.
Understanding Different Buildpack Cache Options
Cloud Native Buildpacks support multiple cache approaches. Choosing the right one depends on the CI platform and infrastructure.
Volume Cache
A volume cache stores build layers in a Docker volume.
For example:
pack build my-app \
--builder paketobuildpacks/builder-jammy-base \
--cache type=build;format=volume;name=my-app-build-cache \
--cache type=launch;format=volume;name=my-app-launch-cache
The volume remains available to later builds that use the same Docker environment.
This approach works well for persistent self-hosted runners.
However, a Docker volume normally belongs to a particular Docker host. Another runner on a different machine cannot access it automatically.
Bind Cache
A bind cache uses a directory on the host machine.
pack build my-app \
--builder paketobuildpacks/builder-jammy-base \
--cache type=build;format=bind;source=/var/cache/my-app-build \
--cache type=launch;format=bind;source=/var/cache/my-app-launch
This is useful when the runner has a persistent directory or a shared filesystem mounted at the same path.
For multiple runners, the directory must be available to all participating machines through appropriate shared storage.
Bind caches can be useful in controlled infrastructure, but filesystem permissions, concurrent access, and storage performance need careful management.
Remote Cache Image
A remote cache image stores reusable build layers in a container registry.
Instead of depending on a local Docker volume or directory, the build process publishes cache data to a registry.
Other runners can retrieve the cache image during their builds.
For example:
pack build my-app \
--builder paketobuildpacks/builder-jammy-base \
--cache-image registry.example.com/my-team/my-app-cache:main \
--publish
The --cache-image option identifies the remote cache image.
The --publish option is required when using a remote cache image. It publishes the resulting application image and the cache information to their respective registry locations.
This is generally a practical approach for ephemeral CI runners because the registry is accessible independently of the machine running the build.
Comparing Cache Options
Cache type | Storage location | Suitable environment | Cross-runner sharing |
|---|
Volume | Docker volume | Persistent self-hosted runners | Not automatic |
Bind | Host directory | Persistent or shared filesystem | Possible with shared storage |
Remote cache image | Container registry | Cloud CI and ephemeral runners | Yes |
Platform-managed cache | CI platform infrastructure | Managed build environments | Depends on platform |
For distributed CI pipelines, a remote cache image is often the simplest option because it uses the container registry infrastructure already required for publishing application images.
Sharing Buildpack Caches With a Container Registry
The following example uses the pack CLI and a remote container registry.
Assume the application image and cache image use these names:
Application image:
registry.example.com/my-team/customer-api:latest
Cache image:
registry.example.com/my-team/customer-api-cache:main
The application image contains the deployable application. The cache image stores reusable build layers.
Keeping these images separate makes it easier to manage deployment artifacts and build optimization data independently.
Preparing the Registry
Before building, make sure the CI runner can authenticate with the container registry.
For example:
docker login registry.example.com \
--username "$REGISTRY_USERNAME" \
--password "$REGISTRY_PASSWORD"
In a production pipeline, credentials should come from the CI platform's secret store or a short-lived identity mechanism.
Do not commit registry passwords or access tokens to the repository.
The registry must support the image operations required by the build process, and the runner must have permission to pull and push the relevant images.
Running a Build With a Remote Cache
Once authentication is configured, run:
pack build \
registry.example.com/my-team/customer-api:latest \
--builder paketobuildpacks/builder-jammy-base \
--cache-image registry.example.com/my-team/customer-api-cache:main \
--publish
This command performs several operations:
Uses the specified builder to detect and build the application.
Attempts to restore reusable layers from the remote cache image.
Executes the required buildpack operations.
Produces the application image.
Publishes the application image.
Updates the remote cache image.
The first build may not provide a performance improvement because the remote cache has not yet been populated.
After the first successful build, subsequent runners can use the published cache.
Using the Previous Application Image
A previous application image can also provide information about the last build.
For example:
pack build \
registry.example.com/my-team/customer-api:latest \
--builder paketobuildpacks/builder-jammy-base \
--previous-image registry.example.com/my-team/customer-api:latest \
--cache-image registry.example.com/my-team/customer-api-cache:main \
--publish
The --previous-image option identifies the previous application image for analysis.
This is separate from the cache image. The previous image and the remote cache serve related but different purposes in the build lifecycle.
Use the cache image to restore cached build layers. Use the previous application image when the build needs to analyze the previously published application image.
Configuring Remote Buildpack Caching in GitHub Actions
GitHub Actions supports jobs running on different hosted runners. Since hosted runners normally start with clean environments, a remote cache image is a useful option for sharing buildpack layers.
Consider a Java application that uses Maven and Paketo Buildpacks.
The following workflow builds the application and publishes its cache image.
name: Build Spring Boot Application
on:
push:
branches:
- main
pull_request:
branches:
- main
permissions:
contents: read
packages: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v6
- name: Set up Java
uses: actions/setup-java@v5
with:
distribution: temurin
java-version: '21'
- name: Build application
run: ./mvnw clean package -DskipTests
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to container registry
if: github.event_name == 'push'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Install Pack CLI
run: |
curl -sSL https://github.com/buildpacks/pack/releases/download/v0.40.11/pack-v0.40.11-linux.tgz \
| sudo tar -C /usr/local/bin/ -xzv pack
- name: Build and publish application
if: github.event_name == 'push'
run: |
pack build ghcr.io/${{ github.repository }}/customer-api:latest \
--builder paketobuildpacks/builder-jammy-base \
--path . \
--cache-image ghcr.io/${{ github.repository }}/customer-api-cache:main \
--publish
This example demonstrates the basic remote-cache workflow.
The Maven build runs before the buildpack command. The buildpack then packages the application into a container image.
For an application where the buildpack itself should execute Maven or Gradle, configure the source directory and buildpack settings accordingly instead of producing the application artifact separately.
The registry login step is restricted to push events because pull requests from forks should not receive permission to publish images or update shared caches.
Handling Pull Requests
Pull requests need special consideration.
A pull request should be able to use an existing cache where the CI platform and registry permissions allow it. However, untrusted pull-request code should not be allowed to overwrite the trusted cache used by production builds.
A safer approach is to use a separate cache namespace for pull requests or allow them to restore a trusted cache without publishing changes to it.
For example:
customer-api-cache:main
customer-api-cache:feature-123
customer-api-cache:pull-request-123
The exact cache publishing permissions should depend on the repository's trust model.
Also, avoid publishing a production application image from an untrusted pull request unless the pipeline has an explicit security design for doing so.
Sharing Caches Across GitLab CI Runners
GitLab Runner provides a distributed cache feature that can store CI cache archives in object storage.
This feature is useful for sharing downloaded dependencies and other files between jobs running on different runners.
However, it is important to distinguish GitLab's general CI cache from the Cloud Native Buildpacks cache.
GitLab's distributed cache does not automatically become a buildpack cache image. They are separate caching mechanisms.
For buildpack layers, use the remote cache image approach with a container registry.
For Maven dependencies, Gradle dependencies, npm packages, or other files used by pipeline steps, GitLab's distributed cache can be useful.
A simplified GitLab Runner configuration using S3 looks like this:
[[runners]]
name = "shared-build-runner"
executor = "docker"
[runners.cache]
Type = "s3"
Path = "build-cache"
Shared = true
[runners.cache.s3]
ServerAddress = "s3.amazonaws.com"
BucketName = "my-ci-cache"
BucketLocation = "us-east-1"
AccessKey = "AWS_ACCESS_KEY"
SecretKey = "AWS_SECRET_KEY"
The Shared = true setting allows configured runners to use the shared distributed cache namespace.
In production, prefer short-lived credentials or an appropriate workload identity mechanism instead of storing long-lived access keys directly in the configuration.
Configuring GitLab CI Cache
A GitLab pipeline can cache Maven dependencies separately from the buildpack cache image.
stages:
- build
- package
variables:
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
cache:
key:
files:
- pom.xml
paths:
- .m2/repository/
build:
stage: build
image: maven:3.9-eclipse-temurin-21
script:
- mvn clean package -DskipTests
artifacts:
paths:
- target/*.jar
package:
stage: package
image: paketobuildpacks/pack
script:
- |
pack build registry.example.com/my-team/customer-api:latest \
--builder paketobuildpacks/builder-jammy-base \
--cache-image registry.example.com/my-team/customer-api-cache:main \
--publish
The first cache definition is managed by GitLab Runner. It reuses Maven dependencies between jobs and pipeline runs.
The --cache-image argument manages buildpack layers through the container registry.
Both approaches can be used together because they solve different caching problems.
Designing Cache Keys and Cache Namespaces
A shared cache should not be treated as one universal storage location for every application and environment.
Different applications, branches, architectures, and builder configurations may require different caches.
A practical cache naming strategy might look like this:
registry.example.com/team-a/customer-api-cache:main
registry.example.com/team-a/customer-api-cache:release
registry.example.com/team-a/customer-api-cache:feature-123
registry.example.com/team-a/order-api-cache:main
registry.example.com/team-a/order-api-cache:release
This separation helps prevent unrelated builds from overwriting each other's cache information.
What Should Influence Cache Separation?
Consider the following factors:
Factor | Why it matters |
|---|
Application | Different applications may have different dependencies |
Branch | Feature branches may contain experimental changes |
Builder | Different builders may use different buildpack configurations |
Architecture | Native binaries and some dependencies are architecture-specific |
Operating environment | Different build environments may require different cached layers |
Runtime version | Changes to the runtime can invalidate previously reusable layers |
A cache should be shared only when the builds are compatible.
For example, sharing a cache between Linux AMD64 and ARM64 builds without considering architecture-specific dependencies can lead to incorrect reuse or cache misses.
The buildpack lifecycle and individual buildpacks perform compatibility checks, but CI teams should still maintain sensible cache boundaries.
Handling Concurrent Builds
Concurrent pipelines can create another problem.
Suppose two jobs start at the same time and both use:
customer-api-cache:main
Both jobs may restore the same cache and attempt to publish updated cache data.
The final cache image can depend on which job finishes last. A build that finishes later may publish cache information based on an older source revision.
This can reduce cache effectiveness and make debugging more difficult.
A safer strategy is to use unique cache references for concurrent builds and control how the shared cache is updated.
For example:
customer-api-cache:build-1001
customer-api-cache:build-1002
customer-api-cache:main
A pipeline can build using an isolated cache and update the shared branch cache through a controlled process.
Alternatively, serialize cache-writing jobs for a particular application and branch.
Avoid allowing unrelated pipelines to overwrite the same shared cache without a clear coordination strategy.
Improving Cache Performance
Sharing caches does not automatically guarantee faster builds. The cache must be designed around the actual build workload.
Keep the Cache Close to the Runners
Network distance affects cache transfer time.
If runners operate in one cloud region but the container registry is in another region, restoring large cache images can introduce additional latency and data transfer costs.
Use a registry close to the build infrastructure whenever possible.
Cache Only Useful Build Layers
Buildpacks manage the cache behavior of their own layers.
A layer can be configured with different purposes, including build-time availability, launch-time availability, and persistence across builds.
The buildpack specification defines these layer types through build, launch, and cache settings.
For example, a dependency download layer may be useful only during the build process. A runtime layer may be included in the final application image.
Do not assume that every layer should be cached or included in the final image. Follow the buildpack's documented caching behavior.
Monitor Cache Hit Rates
Track build duration and cache restoration behavior.
Useful measurements include:
If restoring a cache takes longer than rebuilding the dependencies, the caching strategy may need adjustment.
Avoid Excessively Large Cache Images
A very large cache image can create unnecessary network traffic.
Review whether the cache is retaining useful layers and whether outdated cache references can be removed.
Use container registry retention policies carefully. Do not delete the only cache image that active pipelines depend on.
Security Considerations
A shared cache is part of the CI/CD supply chain. It should be protected like other build infrastructure.
Restrict Registry Permissions
Use separate permissions for pulling and publishing cache images.
For example, developers and untrusted pull-request jobs may need permission to read a trusted cache, while only trusted branch builds should be able to publish updates.
Protect Secrets
Do not include registry passwords, cloud credentials, API keys, or other secrets in cache layers.
Build caches can be retained for a long time and may be accessible to multiple jobs or teams.
Use secret bindings and secure build-time configuration supported by the relevant buildpacks and platform.
Use Trusted Builders
Buildpacks execute code during the build process. Use trusted builders and buildpack versions from approved sources.
Pin builder and buildpack versions where reproducibility and supply-chain control are important.
Separate Untrusted and Trusted Builds
Do not allow untrusted pull requests to modify caches used by production release pipelines.
Use separate cache namespaces and appropriate registry permissions.
Maintain Cache Retention Policies
Old cache images can consume registry storage.
Configure retention policies that remove unused cache versions while preserving the references needed by active branches and release workflows.
Before deleting a cache, confirm that no active pipeline depends on it.
Common Problems and Troubleshooting
Cache Is Not Restored on Another Runner
The most common reason is that the build uses a local volume or bind cache rather than a remote cache image.
Check the pack build command and confirm that it includes the correct --cache-image value and --publish option.
Also verify that the new runner can authenticate with and access the registry.
Cache Image Cannot Be Published
Check registry authentication and permissions.
The CI identity needs permission to push the cache image as well as the application image.
Confirm that the registry repository exists and supports the required image operations.
Builds Still Download All Dependencies
A cache image may exist without containing reusable layers for the current build.
Check whether the builder, buildpacks, runtime, architecture, and dependency versions are compatible with the previous build.
Also inspect the build output to understand which layers were restored and which were rebuilt.
Cache Is Frequently Overwritten
Concurrent jobs may be publishing to the same cache reference.
Use branch-specific or build-specific cache references and introduce a controlled process for updating shared caches.
Cache Restoration Is Slower Than Rebuilding
A remote cache image may be too large or too far from the runner.
Compare the time spent downloading and restoring cached layers with the time required to rebuild them.
If necessary, change the cache location, reduce unnecessary cache data, or use a persistent local cache for runners that execute frequently on the same machine.
Pull Requests Cannot Publish Cache Images
This may be intentional because the workflow does not grant write permissions to untrusted pull requests.
Keep this security boundary. Allow pull requests to use a read-only trusted cache where appropriate, and publish cache updates only from trusted workflows.
Best Practices for Sharing Buildpack Caches
Use remote cache images for ephemeral CI runners.
Keep application images and cache images separate.
Use branch-specific cache references for independent workflows.
Separate caches for different applications and incompatible build environments.
Avoid uncontrolled concurrent writes to a shared cache.
Keep the registry close to the CI runners.
Monitor cache restoration and publishing time.
Protect cache images with appropriate registry permissions.
Keep untrusted pull requests from overwriting trusted caches.
Use GitLab or GitHub dependency caching for package-manager files separately from buildpack layer caching.
Apply registry retention policies to control storage costs.
Measure actual build-time improvements instead of assuming every cache hit is beneficial.
Advantages and Disadvantages
Advantages
Reuses build layers across different CI runners.
Reduces repeated dependency downloads and compilation.
Improves build performance in ephemeral CI environments.
Reduces unnecessary network traffic.
Supports distributed pipelines without requiring shared local disks.
Uses container registries that many teams already maintain.
Works alongside package-manager and CI platform caching.
Disadvantages
Requires registry storage and network access.
Cache restoration and publishing introduce additional network operations.
Concurrent builds need a cache-writing strategy.
Cache compatibility depends on the build environment and buildpack behavior.
Incorrect permissions can expose or overwrite shared cache data.
Large cache images may increase storage and transfer costs.
The first build does not benefit from a cache that has not yet been populated.
Conclusion
Sharing buildpack caches across CI runners is an effective way to improve container build performance, especially when pipelines use ephemeral or autoscaled infrastructure.
Local Docker volumes and bind caches are useful for persistent runners, but they do not automatically provide cache sharing across different machines. Remote cache images solve this problem by storing reusable build layers in a container registry that multiple runners can access.
The pack build command provides the --cache-image option for this purpose and requires --publish when using a remote cache image. Teams can combine this approach with GitHub Actions or GitLab distributed caching to reuse package-manager dependencies and other CI files.
The most important part is to design cache namespaces, permissions, and concurrent publishing behavior carefully. A well-managed shared cache can reduce repeated build work, improve pipeline consistency, and make distributed CI/CD workflows more efficient.
Join the conversation! Your thoughts help the community grow.