Registry-Backed Buildpack Build Caches in Spring Boot 4.2: Faster CI Builds

Spring Boot 4.2 introduces registry-backed build caches for Cloud Native Buildpacks. Instead of keeping Buildpack caches only on a local Docker volume or CI runner, teams can now store and share them through a container registry.

This is a major improvement for CI/CD pipelines that use ephemeral runners. It can reduce repeated dependency downloads, speed up image builds, and make container builds more consistent across build agents.

Table of Contents

What Is a Buildpack Build Cache?

Cloud Native Buildpacks convert source code into OCI container images. During the build process, buildpacks may create cached layers for dependencies, compiled artifacts, and other reusable build outputs.

By default, Spring Boot stores these caches as named Docker volumes. The cache name is derived from the target image name.

Default behavior

  • Build caches are local to the Docker daemon.
  • They are stored as named volumes.
  • They are tied to the image name.
  • They may be invalidated if the image name changes frequently.

This works well for a single developer machine or a stable CI runner. It becomes less effective when CI runners are ephemeral and do not retain Docker volumes between builds.

Why Use a Registry-Backed Cache?

A registry-backed cache stores Buildpack cache layers in a container registry. CI runners can pull the cache before building and push updated cache layers after building.

Key benefits

  • CI runners can share a warm cache.
  • Ephemeral CI runners do not need persistent local volumes.
  • Builds can become faster after the first run.
  • Dependency layers can be reused across builds.
  • Multiple CI agents can benefit from the same cache.

When it helps most

  • Large Spring Boot applications with many dependencies.
  • Microservices built frequently in CI.
  • Teams using Kubernetes-based ephemeral CI runners.
  • Organizations with multiple build agents.
  • Pipelines where container image build time is a bottleneck.

How Registry-Backed Caching Works

Spring Boot 4.2 allows the Buildpack build cache to be stored as an image in a registry. The CI pipeline can push and pull this cache image like any other container image.

For example, a build cache can be stored at:

docker.io/myorg/order-service:build-cache

During the next CI run, the Buildpack lifecycle can pull the cache image, reuse valid cached layers, and push updated layers back to the registry.

Important distinction

Cache type Storage location Shared across CI runners?
Default Buildpack cache Local Docker volume No
Bind-mount cache Host filesystem or mounted volume Only if the mount is shared
Registry-backed cache Container registry image Yes

Maven Configuration

Configure the build cache in the Spring Boot Maven plugin under <image>.

Maven registry-backed build cache

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <image>
      <buildCache>
        <image>
          <name>docker.io/myorg/order-service:build-cache</name>
        </image>
      </buildCache>
    </image>
  </configuration>
</plugin>

Replace docker.io/myorg/order-service:build-cache with the actual registry repository and tag for your project.

Build the image

./mvnw spring-boot:build-image

Use a stable cache tag

Use a stable tag such as build-cache or cache. Avoid using the application version in the cache tag, because changing versions would create a new cache and reduce reuse.

# Recommended
docker.io/myorg/order-service:build-cache

# Avoid
docker.io/myorg/order-service:1.0.0-build-cache

Gradle Configuration

Configure the build cache in the Spring Boot Gradle plugin using the bootBuildImage task.

Gradle registry-backed build cache

tasks.named('bootBuildImage') {
    buildCache {
        image {
            name = 'docker.io/myorg/order-service:build-cache'
        }
    }
}

Kotlin DSL example

tasks.named<BootBuildImage>("bootBuildImage") {
    buildCache {
        image {
            name = "docker.io/myorg/order-service:build-cache"
        }
    }
}

Build the image

./gradlew bootBuildImage

Docker Registry Authentication

The CI runner must be able to authenticate with the container registry to pull and push the cache image. Authentication depends on the registry and CI platform.

Docker Hub example

docker login -u "$DOCKERHUB_USERNAME" -p "$DOCKERHUB_TOKEN"

GitHub Container Registry example

echo "$GITHUB_TOKEN" | docker login ghcr.io \
  -u "$GITHUB_ACTOR" \
  --password-stdin

Use least-privilege credentials

  • Create a dedicated CI service account.
  • Grant only the permissions required to read and write the cache repository.
  • Do not reuse a personal registry account in CI.
  • Store registry credentials in CI secrets, not in source code.
  • Rotate credentials regularly.

CI/CD Best Practices

Use a dedicated cache repository

Use a separate repository or tag for the Buildpack cache. Do not use the same repository and tag as your production application image.

myorg/order-service:latest          # Application image
myorg/order-service:build-cache     # Buildpack cache

Protect the cache repository

  • Use a private repository.
  • Apply access controls and least-privilege permissions.
  • Enable vulnerability scanning where available.
  • Apply retention policies to remove stale cache images.
  • Monitor registry storage usage.

Test cache effectiveness

  1. Run the first build with an empty cache.
  2. Confirm that the cache image is pushed successfully.
  3. Run a second build from a different CI runner.
  4. Compare build duration and cache-hit behavior.
  5. Change a dependency or source file and verify that only relevant layers are rebuilt.

Example GitHub Actions workflow

name: Build and Publish

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up JDK
        uses: actions/setup-java@v4
        with:
          java-version: '21'
          distribution: 'temurin'

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build OCI image
        run: ./mvnw spring-boot:build-image

Ensure the Maven or Gradle configuration points to a cache image in the same registry and that the CI identity has permission to push to it.

FAQs

What is a registry-backed Buildpack build cache?

It is a Buildpack cache stored as an image in a container registry. CI runners can share it by pulling and pushing the cache image.

Is this feature available in Spring Boot 4.2?

Yes. Spring Boot 4.2 adds support for storing Buildpack build caches in an image registry.

Does this replace Docker layer caching?

No. Docker layer caching and Buildpack build caching solve different parts of the build process. A registry-backed Buildpack cache is useful when Docker layer cache is unavailable or not shared across CI runners.

Can multiple services use the same cache?

No. Use a separate cache repository or tag for each service. Sharing one cache across unrelated applications can create conflicts and reduce cache effectiveness.

What happens if the cache image is deleted?

The next build can still succeed, but it will start without the cached layers. This usually makes the build slower.

Is a registry-backed cache secure?

It can be secure if you use a private repository, least-privilege credentials, access controls, and vulnerability scanning. Do not expose cache repositories publicly.

Does this work with GitHub Actions?

Yes. It works with GitHub Actions, GitLab CI, Jenkins, CircleCI, and other CI systems that can authenticate with a container registry and run Spring Boot image builds.

Final Thoughts

Registry-backed Buildpack build caches are one of the most practical Spring Boot 4.2 improvements for teams using containerized CI/CD pipelines. They make warm caches portable across ephemeral runners and can significantly reduce repeated build work.

Start with one service, use a dedicated private cache repository, validate permissions, and measure build time before and after enabling the cache.

Next in this series: Ubuntu 26.04 Base Images and Faster Executable JAR Startup in Spring Boot 4.2

Comments