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..bc21c70 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +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('Build Maven Project') { + steps { + // 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 Docker Image') { + steps { + // Check if Docker is installed + sh 'docker --version' + + // Build the Docker image + sh "docker build -t ${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG} ." + } + } + + stage('Run Container Locally') { + steps { + // 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..." + // Clean up only if Docker is available + sh 'docker --version && docker rmi ${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG} || true' + } + } +} \ No newline at end of file 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