Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down
11 changes: 10 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,13 @@ tasks.register('scaleTest', Test) {
showStandardStreams = true
exceptionFormat = 'full'
}
}
}
// 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() }
}
}
Loading