forked from QACTrainers/SpringBoot-Jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
83 lines (78 loc) · 2.27 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
pipeline {
agent any
stages{
stage('Test Application'){
steps{
sh 'mvn clean test'
}
}
stage('Save Tests'){
steps{
sh 'mkdir -p /home/jenkins/Tests/${BUILD_NUMBER}_tests/'
sh 'mv ./target/surefire-reports/*.txt /home/jenkins/Tests/${BUILD_NUMBER}_tests/'
}
}
stage('SSH Build Deploy'){
steps{
sh '''ssh -i "~/.ssh/jenkins_key" jenkins@<***app_instance_ip***> << EOF
rm -rf <***your_repository_name***>
git clone https://github.com/<***your_github_username***>/<***your_repository_name***>.git
cd <***your_repository_name***>
rm -f ./src/main/resources/application-dev.properties
echo 'spring.jpa.hibernate.ddl-auto=create-drop
spring.h2.console.enabled=false
spring.h2.console.path=/h2
spring.datasource.url=jdbc:mysql://<***your_database_endpoint***>:3306/tdl
spring.datasource.data=classpath:data-dev.sql
spring.datasource.username=<***your_database_username***>
spring.datasource.password=<***your_database_password***>' > ./src/main/resources/application-dev.properties
mvn clean package
'''
}
}
stage('Moving War'){
steps{
sh '''ssh -i "~/.ssh/jenkins_key" jenkins@<***app_instance_ip***> << EOF
cd <***your_repository_name***>
mkdir -p /home/jenkins/Wars
mv ./target/*.war /home/jenkins/Wars/project_war.war
'''
}
}
stage('Stopping Service'){
steps{
sh '''ssh -i "~/.ssh/jenkins_key" jenkins@<***app_instance_ip***> << EOF
cd <***your_repository_name***>
bash stopservice.sh
'''
}
}
stage('Create new service file'){
steps{
sh '''ssh -i "~/.ssh/jenkins_key" jenkins@<***app_instance_ip***> << EOF
mkdir -p /home/jenkins/appservice
echo '#!/bin/bash
sudo java -jar /home/jenkins/Wars/project_war.war' > /home/jenkins/appservice/start.sh
sudo chmod +x /home/jenkins/appservice/start.sh
echo '[Unit]
Description=My SpringBoot App
[Service]
User=ubuntu
Type=simple
ExecStart=/home/jenkins/appservice/start.sh
[Install]
WantedBy=multi-user.target' > /home/jenkins/myApp.service
sudo mv /home/jenkins/myApp.service /etc/systemd/system/myApp.service
'''
}
}
stage('Reload and restart service'){
steps{
sh '''ssh -i "~/.ssh/jenkins_key" jenkins@<***app_instance_ip***> << EOF
sudo systemctl daemon-reload
sudo systemctl restart myApp
'''
}
}
}
}