Update Jenkinsfile

This commit is contained in:
sprientera992 2021-06-08 12:55:56 +03:00 committed by GitHub
parent 41e1e0645e
commit a8866c44c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

67
Jenkinsfile vendored
View file

@ -1,12 +1,59 @@
pipeline { pipeline {
agent any agent any
stages { stages {
stage ('Build') { stage('Build') {
steps { steps {
echo 'Running build automation' echo 'Running build automation'
sh './mvnw package' sh './mvnw package'
sh 'java -jar target/*.jar' 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 <DOCKER_HUB_USERNAME>/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 <DOCKER_HUB_USERNAME>/train-schedule:${env.BUILD_NUMBER}\""
}
}
}
}
}
} }