Skip to content

Instantly share code, notes, and snippets.

@ArloL
Last active August 10, 2024 20:48
Show Gist options
  • Save ArloL/46f48e9965a8ea684a3d85f1fdbd1523 to your computer and use it in GitHub Desktop.
Save ArloL/46f48e9965a8ea684a3d85f1fdbd1523 to your computer and use it in GitHub Desktop.
a gradle dockerfile containing multiple best practices
ARG DOCKER_REGISTRY=index.docker.io
FROM $DOCKER_REGISTRY/library/gradle:8.9.0-jdk21-alpine AS base
RUN mkdir -p /root/.gradle
RUN echo "org.gradle.daemon=false" >> /root/.gradle/gradle.properties
RUN echo "org.gradle.caching=true" >> /root/.gradle/gradle.properties
RUN echo "org.gradle.configuration-cache=true" >> /root/.gradle/gradle.properties
WORKDIR /app
COPY build.gradle \
settings.gradle \
gradlew \
./
COPY gradle gradle
RUN \
--mount=type=secret,id=initgradle,target=/root/.gradle/init.gradle \
--mount=type=secret,id=ARTIFACTORY_TOKEN \
--mount=type=cache,id=gradle-caches,target=/root/.gradle/caches,sharing=private \
--mount=type=cache,id=gradle-configuration-cache,target=/app/.gradle/configuration-cache,sharing=private \
gradle --write-verification-metadata sha256 help \
&& rm gradle/verification-metadata.xml
# persist the current gradle cache to disk for registry caching
RUN \
--mount=type=cache,id=gradle-caches,target=/gradle-caches,sharing=private \
cp -r /gradle-caches /root/.gradle/caches
FROM base AS build
COPY src src
RUN \
--mount=type=secret,id=initgradle,target=/root/.gradle/init.gradle \
--mount=type=secret,id=ARTIFACTORY_TOKEN \
--mount=type=cache,id=gradle-caches,target=/root/.gradle/caches,sharing=private,from=base,source=/root/.gradle/caches \
--mount=type=cache,id=gradle-configuration-cache,target=/app/.gradle/configuration-cache,sharing=private \
gradle build -x test
FROM build AS test
RUN \
--mount=type=secret,id=initgradle,target=/root/.gradle/init.gradle \
--mount=type=secret,id=ARTIFACTORY_TOKEN \
--mount=type=cache,id=gradle-caches,target=/root/.gradle/caches,sharing=private,from=base,source=/root/.gradle/caches \
--mount=type=cache,id=gradle-configuration-cache,target=/app/.gradle/configuration-cache,sharing=private \
gradle test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment