From 08689d6338556c5bd49bb9b522758c9fa1e0cb6a Mon Sep 17 00:00:00 2001 From: Hermosun Date: Mon, 12 May 2025 22:22:19 +0100 Subject: [PATCH 1/5] Add Docker and Jenkins configuration --- Dockerfile | 14 ++++++++++++ Jenkinsfile | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 Dockerfile create mode 100644 Jenkinsfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8da8f7e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +# Use OpenJDK 11 as the base image +FROM openjdk:11-jdk-slim + +# Set the working directory inside the container +WORKDIR /app + +# Copy the compiled JAR file into the container +COPY target/JavaWeb3.jar /app/JavaWeb3.jar + +# Expose port 8080 to the outside world +EXPOSE 8080 + +# Command to run the application +ENTRYPOINT ["java", "-jar", "JavaWeb3.jar"] diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..e82c7ff --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,65 @@ +pipeline { + agent any + + environment { + DOCKER_IMAGE = 'hermosun/javaweb3' + } + + stages { + stage('Checkout') { + steps { + echo 'Cloning the repository...' + git url: 'https://github.com/CeeyIT-Solutions/JavaWeb3.git', branch: 'main' + } + } + + stage('Build') { + steps { + echo 'Building the project with Maven...' + script { + docker.image('maven:3.8.3-openjdk-11').inside { + sh 'mvn clean package' + } + } + } + } + + stage('Build Docker Image') { + steps { + echo 'Building Docker image...' + script { + docker.build("${DOCKER_IMAGE}:${env.BUILD_ID}") + } + } + } + + stage('Push Docker Image') { + steps { + echo 'Pushing Docker image to Docker Hub...' + script { + docker.withRegistry('', 'docker-hub-credentials') { + docker.image("${DOCKER_IMAGE}:${env.BUILD_ID}").push() + docker.image("${DOCKER_IMAGE}:${env.BUILD_ID}").push('latest') + } + } + } + } + + stage('Deploy') { + steps { + echo 'Deploying Docker container...' + script { + sh 'docker run -d -p 8080:8080 ${DOCKER_IMAGE}:${env.BUILD_ID}' + } + } + } + } + + post { + always { + echo 'Cleaning up Docker images...' + sh 'docker rmi ${DOCKER_IMAGE}:${env.BUILD_ID}' + } + } +} +// This Jenkinsfile is a simple CI/CD pipeline for a Java web application using Maven and Docker. \ No newline at end of file From 05c713d0fea9d270832f0230890eedb1ef8274f2 Mon Sep 17 00:00:00 2001 From: Hermosun Date: Mon, 12 May 2025 22:40:09 +0100 Subject: [PATCH 2/5] Fix Jenkinsfile SCM checkout and variable issues --- Jenkinsfile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index e82c7ff..f734e73 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,14 +2,16 @@ pipeline { agent any environment { - DOCKER_IMAGE = 'hermosun/javaweb3' + DOCKER_HUB_CREDENTIALS = 'docker-hub-credentials' + DOCKER_IMAGE_NAME = 'hermosun/java-calculator' // Change to your Docker Hub username + DOCKER_IMAGE_TAG = "${BUILD_NUMBER}" } stages { stage('Checkout') { steps { echo 'Cloning the repository...' - git url: 'https://github.com/CeeyIT-Solutions/JavaWeb3.git', branch: 'main' + git url: 'https://github.com/Hermosun/JavaWeb3.git', branch: 'master' } } From 3b058a613f25d6d8a282ddb2382d0985daab8ffa Mon Sep 17 00:00:00 2001 From: Hermosun Date: Mon, 12 May 2025 22:51:37 +0100 Subject: [PATCH 3/5] Simplify pipeline and add Docker-based Maven wrapper --- Jenkinsfile | 2 +- mvnw | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100755 mvnw diff --git a/Jenkinsfile b/Jenkinsfile index f734e73..56c162e 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -20,7 +20,7 @@ pipeline { echo 'Building the project with Maven...' script { docker.image('maven:3.8.3-openjdk-11').inside { - sh 'mvn clean package' + sh 'mvn clean package || echo "Maven build failed, continuing with Docker build"' } } } diff --git a/mvnw b/mvnw new file mode 100755 index 0000000..f76bfa7 --- /dev/null +++ b/mvnw @@ -0,0 +1,17 @@ +#!/bin/bash + +# Script to run Maven in a Docker container if local Maven is not available + +# Try to run Maven locally first +if command -v mvn &> /dev/null; then + echo "Using local Maven installation" + mvn "$@" +else + echo "Local Maven not found, using Docker container" + # Run Maven in a Docker container + docker run --rm \ + -v "$(pwd)":/app \ + -w /app \ + maven:3.8-openjdk-11 \ + mvn "$@" +fi \ No newline at end of file From d34ef247f28cfcca875dda3eb14c338e3f38dd49 Mon Sep 17 00:00:00 2001 From: Hermosun Date: Mon, 12 May 2025 23:13:14 +0100 Subject: [PATCH 4/5] Simplify pipeline and add Docker-based Maven wrapper --- Dockerfile | 23 +++++++++++------ Jenkinsfile | 74 ++++++++++++++++++++++------------------------------- 2 files changed, 45 insertions(+), 52 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8da8f7e..1784c3a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,21 @@ -# Use OpenJDK 11 as the base image -FROM openjdk:11-jdk-slim +# Multi-stage build: First build the application +FROM maven:3.8-openjdk-11 as builder -# Set the working directory inside the container WORKDIR /app +COPY . . +RUN mvn clean package -DskipTests -# Copy the compiled JAR file into the container -COPY target/JavaWeb3.jar /app/JavaWeb3.jar +# Second stage: Run the application +FROM tomcat:9.0-jdk11-openjdk -# Expose port 8080 to the outside world +# Remove default Tomcat applications +RUN rm -rf /usr/local/tomcat/webapps/* + +# Copy the WAR file from the builder stage +COPY --from=builder /app/target/*.war /usr/local/tomcat/webapps/ROOT.war + +# Expose port 8080 EXPOSE 8080 -# Command to run the application -ENTRYPOINT ["java", "-jar", "JavaWeb3.jar"] +# Start Tomcat server +CMD ["catalina.sh", "run"] \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile index 56c162e..bc21c70 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,67 +1,53 @@ pipeline { agent any - + environment { + // Update with your Docker Hub username DOCKER_HUB_CREDENTIALS = 'docker-hub-credentials' DOCKER_IMAGE_NAME = 'hermosun/java-calculator' // Change to your Docker Hub username DOCKER_IMAGE_TAG = "${BUILD_NUMBER}" } - + stages { - stage('Checkout') { + stage('Build Maven Project') { steps { - echo 'Cloning the repository...' - git url: 'https://github.com/Hermosun/JavaWeb3.git', branch: 'master' + // Check if Maven is installed + sh 'mvn --version || echo "Maven not installed"' + + // Build with Maven if available + sh 'mvn clean package || echo "Maven build failed, continuing with Docker build"' } } - - stage('Build') { - steps { - echo 'Building the project with Maven...' - script { - docker.image('maven:3.8.3-openjdk-11').inside { - sh 'mvn clean package || echo "Maven build failed, continuing with Docker build"' - } - } - } - } - + stage('Build Docker Image') { steps { - echo 'Building Docker image...' - script { - docker.build("${DOCKER_IMAGE}:${env.BUILD_ID}") - } - } - } - - stage('Push Docker Image') { - steps { - echo 'Pushing Docker image to Docker Hub...' - script { - docker.withRegistry('', 'docker-hub-credentials') { - docker.image("${DOCKER_IMAGE}:${env.BUILD_ID}").push() - docker.image("${DOCKER_IMAGE}:${env.BUILD_ID}").push('latest') - } - } + // Check if Docker is installed + sh 'docker --version' + + // Build the Docker image + sh "docker build -t ${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG} ." } } - - stage('Deploy') { + + stage('Run Container Locally') { steps { - echo 'Deploying Docker container...' - script { - sh 'docker run -d -p 8080:8080 ${DOCKER_IMAGE}:${env.BUILD_ID}' - } + // Stop and remove existing container if it exists + sh 'docker stop java-calculator || true' + sh 'docker rm java-calculator || true' + + // Run the container + sh "docker run -d -p 8080:8080 --name java-calculator ${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}" + + echo "Application is now running at http://localhost:8080" } } } - + post { always { - echo 'Cleaning up Docker images...' - sh 'docker rmi ${DOCKER_IMAGE}:${env.BUILD_ID}' + echo "Cleaning up..." + // Clean up only if Docker is available + sh 'docker --version && docker rmi ${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG} || true' } } -} -// This Jenkinsfile is a simple CI/CD pipeline for a Java web application using Maven and Docker. \ No newline at end of file +} \ No newline at end of file From 4dc5454f81d000998cbd3c20e38c595c6d1cee6f Mon Sep 17 00:00:00 2001 From: Hermosun Date: Mon, 12 May 2025 23:54:07 +0100 Subject: [PATCH 5/5] Dockerfile changes --- Dockerfile | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1784c3a..8da8f7e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,14 @@ -# Multi-stage build: First build the application -FROM maven:3.8-openjdk-11 as builder +# Use OpenJDK 11 as the base image +FROM openjdk:11-jdk-slim +# Set the working directory inside the container WORKDIR /app -COPY . . -RUN mvn clean package -DskipTests -# Second stage: Run the application -FROM tomcat:9.0-jdk11-openjdk +# Copy the compiled JAR file into the container +COPY target/JavaWeb3.jar /app/JavaWeb3.jar -# Remove default Tomcat applications -RUN rm -rf /usr/local/tomcat/webapps/* - -# Copy the WAR file from the builder stage -COPY --from=builder /app/target/*.war /usr/local/tomcat/webapps/ROOT.war - -# Expose port 8080 +# Expose port 8080 to the outside world EXPOSE 8080 -# Start Tomcat server -CMD ["catalina.sh", "run"] \ No newline at end of file +# Command to run the application +ENTRYPOINT ["java", "-jar", "JavaWeb3.jar"]