Latest Jenkins config

This commit is contained in:
Lihan 2024-07-28 14:34:20 -04:00
parent d40ba0ccb9
commit eb1f493e71
3 changed files with 19 additions and 17 deletions

View file

@ -1,21 +1,11 @@
# Use an official OpenJDK runtime as a parent image # Use an official OpenJDK runtime as a parent image
FROM openjdk FROM openjdk:11-jdk-slim
# Set the working directory inside the container # Set the working directory inside the container
WORKDIR /app WORKDIR /app
# Copy the Maven wrapper and the pom.xml file # Copy the JAR file to the app directory (adjust this path according to your actual JAR file name in the target directory)
COPY .mvn/ .mvn COPY target/*.jar app.jar
COPY mvnw pom.xml ./
# Copy the project source code
COPY src ./src
# Package the application
RUN ./mvnw clean package -DskipTests
# Copy the JAR file to the app directory. This is created in the build process
RUN cp /app/target/*.jar app.jar
# Run the jar file # Run the jar file
CMD ["java", "-jar", "app.jar"] CMD ["java", "-jar", "app.jar"]

14
Jenkinsfile vendored
View file

@ -17,11 +17,20 @@ pipeline {
} }
} }
stage('Build JAR') {
steps {
script {
echo "Building JAR..."
sh './mvnw clean package -Dmaven.test.skip=true'
}
}
}
stage('Build Docker Image') { stage('Build Docker Image') {
steps { steps {
script { script {
echo "Building Docker Image..." echo "Building Docker Image..."
def dockerImage = docker.build("spring-petclinic") def dockerImage = docker.build("spring-petclinic", "--no-cache .")
echo "Docker Image built: ${dockerImage.id}" echo "Docker Image built: ${dockerImage.id}"
// Store the Docker image ID in the environment if needed across stages // Store the Docker image ID in the environment if needed across stages
env.DOCKER_IMAGE_ID = dockerImage.id env.DOCKER_IMAGE_ID = dockerImage.id
@ -29,6 +38,7 @@ pipeline {
} }
} }
// Further stages would reference env.DOCKER_IMAGE_ID if needed
} }
post { post {
@ -37,7 +47,7 @@ pipeline {
// Use the saved Docker image ID from the environment if needed // Use the saved Docker image ID from the environment if needed
if (env.DOCKER_IMAGE_ID) { if (env.DOCKER_IMAGE_ID) {
echo "Stopping and removing Docker Image with ID: ${env.DOCKER_IMAGE_ID}" echo "Stopping and removing Docker Image with ID: ${env.DOCKER_IMAGE_ID}"
docker.rmi(env.DOCKER_IMAGE_ID) sh "docker rmi -f ${env.DOCKER_IMAGE_ID}"
} }
} }
} }

View file

@ -39,15 +39,17 @@ services:
build: build:
context: . context: .
dockerfile: Dockerfile.jenkins dockerfile: Dockerfile.jenkins
image: my-jenkins:latest
ports: ports:
- "8081:8080" - "8081:8080"
- "50000:50000" - "50000:50000"
privileged: true
user: root
environment: environment:
- JAVA_OPTS=-Djenkins.install.runSetupWizard=false - JAVA_OPTS=-Djenkins.install.runSetupWizard=false
- CASC_JENKINS_CONFIG=/var/jenkins_home/casc_configs/jenkins.yaml - CASC_JENKINS_CONFIG=/var/jenkins_home/casc_configs/jenkins.yaml
volumes: volumes:
- ./jenkins_data:/var/jenkins_home - jenkins_data:/var/jenkins_home
- ./jenkins.yaml:/var/jenkins_home/casc_configs/jenkins.yaml - ./jenkins.yaml:/var/jenkins_home/casc_configs/jenkins.yaml
- /var/run/docker.sock:/var/run/docker.sock - /var/run/docker.sock:/var/run/docker.sock
networks: networks: