From 2ac5cb45ab729b92ef050b2ff3b91a968347ef9f Mon Sep 17 00:00:00 2001 From: op-techy Date: Thu, 3 Dec 2026 02:20:05 +0100 Subject: [PATCH] build(docker): download dependencies in their own cached layer CI's Docker job re-downloaded every dependency on each run: the BuildKit cache mount isn't exported to the GitHub Actions layer cache, and the build layer is invalidated by any source change. Twice that hit Maven Central's rate limit (HTTP 429) and failed the build. A new downloadDependencies task resolves every configuration; the Dockerfile runs it before copying src, so the layer changes only with the build files and is reused from the gha cache. The compile step then runs with --offline. --- Dockerfile | 12 ++++++++---- README.md | 2 +- build.gradle | 11 ++++++++++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index f45ce0f..b7601a7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,12 +9,16 @@ COPY gradlew . COPY gradle gradle COPY build.gradle . COPY settings.gradle . + +# Gradle and every dependency are downloaded in their own layer, before the source is copied in, so the layer +# is rebuilt only when the build files change. Local builds and CI's layer cache (cache-to: type=gha) reuse it, +# so a code change downloads nothing (no Maven Central rate limits). This build stage is not in the final image. +RUN ./gradlew --no-daemon downloadDependencies + COPY src src -# The Gradle home (wrapper distribution + downloaded dependencies) is kept in a BuildKit cache that survives -# between builds, so only the first build downloads everything. It is not part of the image. -RUN --mount=type=cache,target=/root/.gradle \ - ./gradlew --no-daemon clean build -x test +# --offline: everything needed was downloaded above +RUN ./gradlew --no-daemon --offline clean build -x test # Stage 2: Runtime with Java 21 JRE FROM eclipse-temurin:21-jre-alpine diff --git a/README.md b/README.md index ec52172..5c894a7 100644 --- a/README.md +++ b/README.md @@ -379,7 +379,7 @@ java -jar build/libs/mailengine-0.0.1-SNAPSHOT.jar ### Docker -The `Dockerfile` builds the app with JDK 21 and runs it on a JRE 21 image as a non-root user. Gradle downloads are kept in a BuildKit cache, so rebuilds are quick. +The `Dockerfile` builds the app with JDK 21 and runs it on a JRE 21 image as a non-root user. Gradle and all dependencies are downloaded in their own layer (`./gradlew downloadDependencies`) before the source is copied in, so after a code change only the compile step runs. CI keeps that layer in its build cache, so it doesn't download from Maven Central on every run. ```bash docker build -t mailengine . diff --git a/build.gradle b/build.gradle index edc77fc..2992312 100644 --- a/build.gradle +++ b/build.gradle @@ -147,4 +147,13 @@ tasks.register('scaleTest', Test) { showStandardStreams = true exceptionFormat = 'full' } -} \ No newline at end of file +} +// Downloads every dependency without compiling anything. The Dockerfile runs it before copying the source, so +// the downloads sit in their own (cacheable) layer. +tasks.register('downloadDependencies') { + description = 'Downloads all dependencies into the Gradle cache.' + def resolvable = configurations.matching { it.canBeResolved } + doLast { + resolvable.each { it.resolve() } + } +}