From a8866c44c3767fba30f0acc7000b5ab46ef094be Mon Sep 17 00:00:00 2001 From: sprientera992 <58562385+sprientera992@users.noreply.github.com> Date: Tue, 8 Jun 2021 12:55:56 +0300 Subject: [PATCH] Update Jenkinsfile --- Jenkinsfile | 67 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index b3ea8359e..a741a3bee 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,12 +1,59 @@ pipeline { - agent any - stages { - stage ('Build') { - steps { - echo 'Running build automation' - sh './mvnw package' - sh 'java -jar target/*.jar' - } - } - } + agent any + stages { + stage('Build') { + steps { + echo 'Running build automation' + sh './mvnw package' + archiveArtifacts artifacts: 'dist/Petclinic.zip' + } + } + stage('Build Docker Image') { + when { + branch 'main' + } + steps { + script { + app = docker.build("sprientera/train-schedule") + app.inside { + sh 'echo $(curl localhost:8080)' + } + } + } + } + stage('Push Docker Image') { + when { + branch 'main' + } + steps { + script { + docker.withRegistry('https://registry.hub.docker.com', 'sprientera') { + app.push("${env.BUILD_NUMBER}") + app.push("latest") + } + } + } + } + stage ('DeployToProduction') { + when { + branch 'master' + } + steps { + input 'Deploy to Production' + milestone(1) + withCredentials ([usernamePassword(credentialsId: 'webserver_login', usernameVariable: '', passwordVariable: 'USERPASS')]) { + script { + sh "sshpass -p '$USERPASS' -v ssh -o StrictHostKeyChecking=no $USERNAME@${env.prod_ip} \"docker pull /train-schedule:${env.BUILD_NUMBER}\"" + try { + sh "sshpass -p '$USERPASS' -v ssh -o StrictHostKeyChecking=no $USERNAME@${env.prod_ip} \"docker stop train-schedule\"" + sh "sshpass -p '$USERPASS' -v ssh -o StrictHostKeyChecking=no $USERNAME@${env.prod_ip} \"docker rm train-schedule\"" + } catch (err) { + echo: 'caught error: $err' + } + sh "sshpass -p '$USERPASS' -v ssh -o StrictHostKeyChecking=no $USERNAME@${env.prod_ip} \"docker run --restart always --name train-schedule -p 8080:8080 -d /train-schedule:${env.BUILD_NUMBER}\"" + } + } + } + } + } }