From c556ebc30878cfc130fcea4688bc38034e9225ed Mon Sep 17 00:00:00 2001 From: Lihan Date: Sat, 27 Jul 2024 15:33:16 -0400 Subject: [PATCH] latest --- Dockerfile | 29 +++++++++++++++++++---------- Jenkinsfile | 7 +++---- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Dockerfile b/Dockerfile index 012bb41ca..91b031ec3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,23 @@ -# Use an official OpenJDK runtime as a parent image -FROM openjdk:11 - -# Set the working directory inside the container +# First stage: Build the application using Maven +FROM maven:3.8.1-openjdk-17 AS build WORKDIR /app -# Copy the built JAR file from the build context -COPY target/*.jar app.jar +# Copy the Maven wrapper and the pom.xml file +COPY .mvn/ .mvn +COPY mvnw pom.xml ./ + +# Copy the project source code +COPY src ./src + +# Package the application +RUN ./mvnw package + +# Second stage: Use an official OpenJDK runtime as a parent image +FROM openjdk +WORKDIR /app + +# Copy the JAR file from the build stage +COPY --from=build /app/target/*.jar app.jar # Run the jar file -CMD ["java", "-jar", "app.jar"] - -# Expose the port the app runs on -EXPOSE 8080 +CMD ["java", "-jar", "app"] \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile index ff8a76707..bd0ff8d89 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -21,8 +21,6 @@ pipeline { steps { script { echo "Building Docker Image..." - // Ensure Maven build is executed - sh './mvnw clean package -DskipTests' def dockerImage = docker.build("spring-petclinic") echo "Docker Image built: ${dockerImage.id}" // Store the Docker image ID in the environment if needed across stages @@ -31,16 +29,17 @@ pipeline { } } + // Further stages would reference env.DOCKER_IMAGE_ID if needed } post { always { script { + // Use the saved Docker image ID from the environment if needed if (env.DOCKER_IMAGE_ID) { echo "Stopping and removing Docker Image with ID: ${env.DOCKER_IMAGE_ID}" - // Using shell command to remove Docker image - sh "docker rmi ${env.DOCKER_IMAGE_ID}" + docker.rmi(env.DOCKER_IMAGE_ID) } } }