From b3b9f97ae81e2076923b0bd92fabfbe9453d3cd1 Mon Sep 17 00:00:00 2001 From: hudge1 Date: Wed, 31 Jul 2024 15:40:28 +0530 Subject: [PATCH 1/3] Create jenkinsrsync --- jenkinsrsync | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 jenkinsrsync diff --git a/jenkinsrsync b/jenkinsrsync new file mode 100644 index 0000000..05e71bd --- /dev/null +++ b/jenkinsrsync @@ -0,0 +1,35 @@ +pipeline { + agent none + stages { + stage('Checkout') { + agent {label 'master'} + steps { + git branch: 'main', url: 'https://github.com/sudheer76R/java-example.git' + } + } + stage('Static-test') { + agent {label 'master'} + steps { + echo 'Running static tests on code' + } + } + stage('Build') { + environment { + private_key=credentials('ssh_key') + } + agent {label 'master'} + steps { + sh 'mvn clean package' + sh 'rsync -avzh -e "ssh -i ${private_key} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" $WORKSPACE/target/*.war ubuntu@13.234.115.8:/home/ubuntu' + } + } + stage('Deploy') { + agent {label 'tomcatsp'} + steps { + echo 'Deploying into Dev environment' + sh 'sudo cp -r /home/ubuntu/*.war /opt/tomcat/apache-tomcat-9.0.68/webapps/' + sh 'sudo rm -rf /home/ubuntu/*.war' + } + } + } +} From 5a2003cefbd311c51af18a06a275479c9a02437f Mon Sep 17 00:00:00 2001 From: hudge1 Date: Wed, 31 Jul 2024 15:48:42 +0530 Subject: [PATCH 2/3] Update jenkinsrsync --- jenkinsrsync | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jenkinsrsync b/jenkinsrsync index 05e71bd..7db11f7 100644 --- a/jenkinsrsync +++ b/jenkinsrsync @@ -4,7 +4,7 @@ pipeline { stage('Checkout') { agent {label 'master'} steps { - git branch: 'main', url: 'https://github.com/sudheer76R/java-example.git' + git branch: 'main', url: 'https://github.com/hudge1/java-example.git' } } stage('Static-test') { @@ -27,7 +27,7 @@ pipeline { agent {label 'tomcatsp'} steps { echo 'Deploying into Dev environment' - sh 'sudo cp -r /home/ubuntu/*.war /opt/tomcat/apache-tomcat-9.0.68/webapps/' + sh 'sudo cp -r /home/ubuntu/*.war /home/ec2-user/tomcat/webapps/' sh 'sudo rm -rf /home/ubuntu/*.war' } } From 356de4df060454f0de962f19cfa8c7f120e1237a Mon Sep 17 00:00:00 2001 From: hudge1 Date: Wed, 31 Jul 2024 16:40:39 +0530 Subject: [PATCH 3/3] Create jenkinsrsync2 --- jenkinsrsync2 | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 jenkinsrsync2 diff --git a/jenkinsrsync2 b/jenkinsrsync2 new file mode 100644 index 0000000..9e46c4c --- /dev/null +++ b/jenkinsrsync2 @@ -0,0 +1,54 @@ +pipeline { + agent any + + environment { + TOMCAT_USER = 'ec2-user' + TOMCAT_HOST = '52.66.8.70' + TOMCAT_PATH = '/home/ec2-usser/tomcat/webapps/' + ARTIFACT_NAME = 'works-with-heroku-1.0.war' + } + + stages { + stage('Build') { + steps { + echo 'Building the project...' + // Assuming Maven is used + sh 'mvn clean package' + } + } + + stage('Deploy') { + steps { + script { + def artifactPath = "target/${env.ARTIFACT_NAME}" + + echo "Deploying ${artifactPath} to ${env.TOMCAT_HOST}..." + + // Deploy artifact using rsync + sh """ + rsync -avz --delete ${artifactPath} ${env.TOMCAT_USER}@${env.TOMCAT_HOST}:${env.TOMCAT_PATH} + """ + } + } + } + + stage('Restart Tomcat') { + steps { + script { + echo "Restarting Tomcat on ${env.TOMCAT_HOST}..." + + // Restart Tomcat (Optional) + sh """ + ssh ${env.TOMCAT_USER}@${env.TOMCAT_HOST} 'sudo systemctl restart tomcat' + """ + } + } + } + } + + post { + always { + echo 'Pipeline execution finished.' + } + } +}