From cf8a3cb4a5d0bdda42a677af36ee2ebbdc71f750 Mon Sep 17 00:00:00 2001 From: armanot <34114374+armanot@users.noreply.github.com> Date: Mon, 17 Jun 2024 22:59:33 +0800 Subject: [PATCH 1/2] Update Jenkinsfile --- Jenkinsfile | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index e3e8fb3b..60139db2 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -8,5 +8,31 @@ pipeline { archiveArtifacts artifacts: 'dist/trainSchedule.zip' } } - } -} \ No newline at end of file + stage('Build Docker Image') { + when { + branch 'master' + } + steps { + script { + app = docker.build("armanot/train-schedule") + app.inside { + sh 'echo $(curl localhost:8080)' + } + } + } + } + stage('Push Docker Image') { + when { + branch 'master' + } + steps { + script { + docker.withRegistry('https://registry.hub.docker.com', 'docker_hub_login') { + app.push("${env.BUILD_NUMBER}") + app.push("latest") + } + } + } + } + } +} From 7563958c8858aa477c27da04346fc44a6e3206da Mon Sep 17 00:00:00 2001 From: aothman Date: Mon, 17 Jun 2024 23:18:19 +0800 Subject: [PATCH 2/2] added Dockerfile --- Dockerfile | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..5deaf663 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,59 @@ +pipeline { + agent any + stages { + stage('Build') { + steps { + echo 'Running build automation' + sh './gradlew build --no-daemon' + archiveArtifacts artifacts: 'dist/trainSchedule.zip' + } + } + stage('Build Docker Image') { + when { + branch 'master' + } + steps { + script { + app = docker.build("armanot/train-schedule") + app.inside { + sh 'echo $(curl localhost:8080)' + } + } + } + } + stage('Push Docker Image') { + when { + branch 'master' + } + steps { + script { + docker.withRegistry('https://registry.hub.docker.com', 'docker_hub_login') { + 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: 'USERNAME', passwordVariable: 'USERPASS')]) { + script { + sh "sshpass -p '$USERPASS' -v ssh -o StrictHostKeyChecking=no $USERNAME@$prod_ip \"docker pull willbla/train-schedule:${env.BUILD_NUMBER}\"" + try { + sh "sshpass -p '$USERPASS' -v ssh -o StrictHostKeyChecking=no $USERNAME@$prod_ip \"docker stop train-schedule\"" + sh "sshpass -p '$USERPASS' -v ssh -o StrictHostKeyChecking=no $USERNAME@$prod_ip \"docker rm train-schedule\"" + } catch (err) { + echo: 'caught error: $err' + } + sh "sshpass -p '$USERPASS' -v ssh -o StrictHostKeyChecking=no $USERNAME@$prod_ip \"docker run --restart always --name train-schedule -p 8080:8080 -d willbla/train-schedule:${env.BUILD_NUMBER}\"" + } + } + } + } + } +} \ No newline at end of file