From 0b2b907db67bafe8cc4b7953ee9a89c29bb1884c Mon Sep 17 00:00:00 2001 From: shubhalokesh <35987852+shubhalokesh@users.noreply.github.com> Date: Fri, 3 Jan 2025 14:37:25 +0530 Subject: [PATCH 1/4] Update Jenkinsfile --- Jenkinsfile | 99 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 63 insertions(+), 36 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index d48e6c9..205ae42 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,72 +1,99 @@ pipeline { - agent { - label 'king' - } + agent any environment { - TOMCAT_HOST = '172.31.3.184' - TOMCAT_USER = 'root' - TOMCAT_DIR = '/opt/apache-tomcat-8.5.98/webapps' - JAR_FILE = 'bus-booking-app-1.0-SNAPSHOT.jar' // Replace with the actual name of your JAR file + JAVA_HOME = '/usr/lib/jvm/java-17-openjdk-amd64' + MAVEN_HOME = '/usr/share/maven' + PATH = "${JAVA_HOME}/bin:${MAVEN_HOME}/bin:${env.PATH}" } stages { - stage('checkout') { + stage('Checkout Code') { steps { - sh 'rm -rf bus_booking' - sh 'git clone https://github.com/sudhasanshi/bus_booking.git' + echo 'Checking out code...' + checkout scm } } - stage('build') { + stage('Set up Java 17') { steps { - script { - def mvnHome = tool 'Maven' - def mvnCMD = "${mvnHome}/bin/mvn" - sh "${mvnCMD} clean install" - } + echo 'Setting up Java 17...' + sh 'sudo apt update' + sh 'sudo apt install -y openjdk-17-jdk' } } - stage('Show Contents of target') { + stage('Set up Maven') { steps { - script { - // Print the contents of the target directory - sh 'ls -l target' - } + echo 'Setting up Maven...' + sh 'sudo apt install -y maven' + } + } + + stage('Build with Maven') { + steps { + echo 'Building project with Maven...' + sh 'mvn clean package' } } - stage('Run JAR Locally') { + stage('Upload Artifact') { steps { + echo 'Uploading artifact...' + archiveArtifacts artifacts: 'target/simple-parcel-service-app-1.0-SNAPSHOT.jar', allowEmptyArchive: true + } + } + + stage('Run Application') { + steps { + echo 'Running Spring Boot application...' + sh 'nohup mvn spring-boot:run &' + sleep(time: 15, unit: 'SECONDS') // Wait for the application to fully start + + // Fetch the public IP and display the access URL script { - // Run the JAR file using java -jar - sh "java -jar target/${JAR_FILE}" + def publicIp = sh(script: "curl -s https://checkip.amazonaws.com", returnStdout: true).trim() + echo "The application is running and accessible at: http://${publicIp}:8080" } } } - stage('Deploy JAR to Tomcat') { + stage('Validate App is Running') { steps { + echo 'Validating that the app is running...' script { - // Copy JAR to Tomcat server - sh "scp target/${JAR_FILE} ${TOMCAT_USER}@${TOMCAT_HOST}:${TOMCAT_DIR}/" + def response = sh(script: 'curl --write-out "%{http_code}" --silent --output /dev/null http://localhost:8080', returnStdout: true).trim() + if (response == "200") { + echo 'The app is running successfully!' + } else { + echo "The app failed to start. HTTP response code: ${response}" + currentBuild.result = 'FAILURE' + error("The app did not start correctly!") + } + } + } + } - // SSH into Tomcat server and restart Tomcat - sh "ssh ${TOMCAT_USER}@${TOMCAT_HOST} 'bash -s' < restart-tomcat.sh" + stage('Wait for 5 minutes') { + steps { + echo 'Waiting for 5 minutes...' + sleep(time: 5, unit: 'MINUTES') // Wait for 5 minutes + } + } - echo "Application deployed and Tomcat restarted" - } + stage('Gracefully Stop Spring Boot App') { + steps { + echo 'Gracefully stopping the Spring Boot application...' + sh 'mvn spring-boot:stop' } } } post { - success { - echo "Build, Run, and Deployment to Tomcat successful!" - } - failure { - echo "Build, Run, and Deployment to Tomcat failed!" + always { + echo 'Cleaning up...' + // Any cleanup steps, like stopping the app or cleaning up the environment + sh 'pkill -f "mvn spring-boot:run" || true' // Ensure the app is stopped } } } From fa8385e2cdcb1bdc6b9c32cebe5a4b97260221a3 Mon Sep 17 00:00:00 2001 From: shubhalokesh <35987852+shubhalokesh@users.noreply.github.com> Date: Sat, 4 Jan 2025 14:38:39 +0530 Subject: [PATCH 2/4] Update Jenkinsfile --- Jenkinsfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 205ae42..0032efa 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -76,8 +76,8 @@ pipeline { stage('Wait for 5 minutes') { steps { - echo 'Waiting for 5 minutes...' - sleep(time: 5, unit: 'MINUTES') // Wait for 5 minutes + echo 'Waiting for 1 minutes...' + sleep(time: 1, unit: 'MINUTES') // Wait for 5 minutes } } From 7db1db6f33ac77d6965a2a4e6e5a14ef1a443603 Mon Sep 17 00:00:00 2001 From: shubhalokesh <35987852+shubhalokesh@users.noreply.github.com> Date: Sun, 5 Jan 2025 08:40:05 +0530 Subject: [PATCH 3/4] Update Jenkinsfile --- Jenkinsfile | 102 +++++++++++++++++++++++----------------------------- 1 file changed, 45 insertions(+), 57 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 0032efa..2deb49d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,5 +1,7 @@ +@Library('jenkins-shared-libraries-@main') _ // Correct syntax + pipeline { - agent any + agent { label 'slave-2' } environment { JAVA_HOME = '/usr/lib/jvm/java-17-openjdk-amd64' @@ -10,90 +12,76 @@ pipeline { stages { stage('Checkout Code') { steps { - echo 'Checking out code...' - checkout scm - } - } - + script { + pipeline1.checkscm() + } + } + } stage('Set up Java 17') { steps { - echo 'Setting up Java 17...' - sh 'sudo apt update' - sh 'sudo apt install -y openjdk-17-jdk' + script { + pipeline1.setupjava() + } } - } + } stage('Set up Maven') { steps { - echo 'Setting up Maven...' - sh 'sudo apt install -y maven' + script { + pipeline1.mavensetup() + } } } stage('Build with Maven') { steps { - echo 'Building project with Maven...' - sh 'mvn clean package' + script { + pipeline1.build() + } } } stage('Upload Artifact') { steps { - echo 'Uploading artifact...' - archiveArtifacts artifacts: 'target/simple-parcel-service-app-1.0-SNAPSHOT.jar', allowEmptyArchive: true + uploadArtifact('target/bus-booking-app-1.0-SNAPSHOT.jar') } } stage('Run Application') { steps { - echo 'Running Spring Boot application...' - sh 'nohup mvn spring-boot:run &' - sleep(time: 15, unit: 'SECONDS') // Wait for the application to fully start - - // Fetch the public IP and display the access URL script { - def publicIp = sh(script: "curl -s https://checkip.amazonaws.com", returnStdout: true).trim() - echo "The application is running and accessible at: http://${publicIp}:8080" - } + pipeline1.runApp() + } } } stage('Validate App is Running') { - steps { - echo 'Validating that the app is running...' - script { - def response = sh(script: 'curl --write-out "%{http_code}" --silent --output /dev/null http://localhost:8080', returnStdout: true).trim() - if (response == "200") { - echo 'The app is running successfully!' - } else { - echo "The app failed to start. HTTP response code: ${response}" - currentBuild.result = 'FAILURE' - error("The app did not start correctly!") - } - } - } - } - - stage('Wait for 5 minutes') { - steps { - echo 'Waiting for 1 minutes...' - sleep(time: 1, unit: 'MINUTES') // Wait for 5 minutes - } + steps { + script { + pipeline1.validateApp() + } + } } - - stage('Gracefully Stop Spring Boot App') { - steps { - echo 'Gracefully stopping the Spring Boot application...' - sh 'mvn spring-boot:stop' - } + stage('wait') { + steps { + script { + pipeline1.waiting() + } + } } - } - - post { - always { - echo 'Cleaning up...' - // Any cleanup steps, like stopping the app or cleaning up the environment - sh 'pkill -f "mvn spring-boot:run" || true' // Ensure the app is stopped + stage('stoping') { + steps { + script { + pipeline1.stop() + } + } } + stage('cleaning') { + steps { + script { + pipeline1.clean() + } + } + } } } From 799a86d99d5807480c073b99a04f8280a0724158 Mon Sep 17 00:00:00 2001 From: shubhalokesh <35987852+shubhalokesh@users.noreply.github.com> Date: Sun, 5 Jan 2025 08:45:34 +0530 Subject: [PATCH 4/4] Update Jenkinsfile --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 2deb49d..f63c57d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,7 +1,7 @@ @Library('jenkins-shared-libraries-@main') _ // Correct syntax pipeline { - agent { label 'slave-2' } + agent { label 'slave' } environment { JAVA_HOME = '/usr/lib/jvm/java-17-openjdk-amd64'