1- FROM maven:3-amazoncorretto-21 AS base
1+ # Stage 1: Download dependencies (cached separately from source code)
2+ FROM maven:3-eclipse-temurin-21 AS dependencies
3+ WORKDIR /app
4+
5+ # Copy only POM files first to cache dependency downloads
6+ COPY pom.xml ./
7+ COPY persistence/pom.xml ./persistence/
8+ COPY web/pom.xml ./web/
9+ COPY forms/pom.xml ./forms/
210
3- FROM base AS builder
11+ # Download all dependencies (this layer will be cached unless POM files change)
12+ RUN --mount=type=cache,target=/root/.m2 \
13+ mvn -B dependency:go-offline -DskipTests -Dcheckstyle.skip || true
14+
15+ # Stage 2: Build the application
16+ FROM dependencies AS builder
417WORKDIR /app
518
6- COPY . /app
7- # Use cache mounts so dependencies aren't re-downloaded on every build
8- RUN --mount=type=cache,target=/root/.m2 mvn -B clean install -DskipTests -Dcheckstyle.skip
19+ # Accept git branch/tag as build argument
20+ ARG GIT_BRANCH=unknown
21+
22+ # Now copy source code (changes here won't invalidate dependency cache)
23+ COPY persistence/src ./persistence/src
24+ COPY web/src ./web/src
25+ COPY forms/src ./forms/src
26+ COPY checkstyle.xml checkstyle-suppressions.xml ./
927
10- # Create directory for extracting the JAR
11- RUN mkdir -p forms/target/deps
28+ # Build with cache mount for any additional dependencies
29+ # Pass git.closest.tag.name as a Maven property
30+ RUN --mount=type=cache,target=/root/.m2 \
31+ mvn -B clean package -DskipTests -Dcheckstyle.skip -Dgit.closest.tag.name=${GIT_BRANCH}
1232
13- # Extract the JAR contents
33+ # Stage 3: Extract JAR layers (Spring Boot layered JAR)
1434FROM builder AS assembler
1535WORKDIR /app/forms/target/deps
16- RUN jar -xf ../*.jar
36+ RUN jar -xf ../tcdi-admin-forms- *.jar
1737
18- # Build the runtime image
19- FROM openjdk:21-jdk-slim AS runtime
38+ # Stage 4: Build the runtime image
39+ FROM eclipse-temurin:21-jre-alpine AS runtime
40+ RUN apk add --no-cache bash curl
2041WORKDIR /opt/devgateway/tcdi/admin
2142
22- # Copy the application code
43+ # Copy the extracted application in optimal order for layer caching
2344COPY --from=assembler /app/forms/target/deps ./deps
2445
25- # Copy entrypoint script
46+ # Copy entrypoint script and set permissions in one layer
2647COPY entrypoint.sh ./
27- EXPOSE 8080
2848RUN chmod +x entrypoint.sh
49+
50+ EXPOSE 8080
2951ENTRYPOINT ["/opt/devgateway/tcdi/admin/entrypoint.sh" ]
3052CMD ["admin" ]
0 commit comments