-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
160 lines (135 loc) · 5.22 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#! groovy
import java.security.InvalidParameterException
import java.util.logging.Logger
Logger logger = Logger.getLogger('vizzyy.jenkins.deploy')
currentBuild.displayName = "Home Pipeline [ " + currentBuild.number + " ]"
try {
if (ISSUE_NUMBER)
echo "Building from pull request..."
} catch (Exception e) {
ISSUE_NUMBER = false
echo "Building from jenkins job..."
}
String getVersion(){
def versionNumber = sh (
script: 'cat build.gradle | grep "version = "',
returnStdout: true
).trim()
return versionNumber.substring(10)
}
pipeline {
agent any
stages {
stage("Acknowledge") {
steps {
script {
if (env.Build == "true" && ISSUE_NUMBER) {
prTools.comment(ISSUE_NUMBER, """{"body": "Jenkins triggered $currentBuild.displayName"}""", "spring_react")
}
}
}
}
stage("Build") {
steps {
script {
if (env.Build == "true") {
prTools.checkoutBranch(ISSUE_NUMBER, "vizzyy-org/spring_react")
sh('''
./gradlew clean build
''')
}
}
}
}
stage("Deploy") {
steps {
script {
if (env.Deploy == "true") {
currentVersion = getVersion()
//ec2 can only ssh through jumpbox
sh("""
echo "SCP file to remote server"
scp -i ~/ec2pair.pem build/libs/Home*.jar [email protected]:~/spring_react
ssh -i ~/ec2pair.pem [email protected] 'ln -sf ~/spring_react/Home-"""+currentVersion+""".jar ~/spring_react/home.jar'
""")
}
}
}
}
stage("Start") {
steps {
script {
if (env.Deploy == "true") {
sh('''
ssh -i ~/ec2pair.pem [email protected] 'sudo systemctl restart home'
''')
}
}
}
}
stage("Confirm") {
steps {
script {
if (env.Deploy == "true") {
def deployed = false
for(int i=0; i<12; i++){
try {
def health = sh (
script: 'curl --cert-type P12 --cert ~/home-cert.p12:changeit https://www.vizzyy.com/actuator/health',
returnStdout: true
).trim()
echo health
if (health == "{\"status\":\"UP\"}"){
deployed = true
break
}
} catch ( Exception e) {
echo "could not parse"
e.printStackTrace()
}
sleep time: i, unit: 'SECONDS'
}
if(!deployed)
throw new InvalidParameterException()
}
}
}
}
stage("Version") {
steps {
script {
if (env.Build == "true") {
versionNumber = getVersion()
echo versionNumber.toString()
versions = versionNumber.split("[.-]")
newMinor = versions[2].toInteger() + 1
newVersion = "'"+versions[0].substring(1)+"."+versions[1]+"."+newMinor+"-SNAPSHOT'"
echo newVersion
sh """
sed -i -E s/'[[:digit:]]+\\.[[:digit:]]+\\.[[:digit:]]+-SNAPSHOT'/$newVersion/g build.gradle
git -c core.sshCommand="ssh -i /var/lib/jenkins/.ssh/id_rsa" commit -am "Jenkins incremented build version."
git -c core.sshCommand="ssh -i /var/lib/jenkins/.ssh/id_rsa" push origin HEAD:master
"""
}
}
}
}
}
post {
success {
script {
if (env.Build == "true" && ISSUE_NUMBER) {
prTools.merge(ISSUE_NUMBER, """{"commit_title": "Jenkins merged $currentBuild.displayName","merge_method": "merge"}""", "spring_react")
prTools.comment(ISSUE_NUMBER, """{"body": "Jenkins successfully deployed $currentBuild.displayName"}""", "spring_react")
}
}
}
failure {
script {
if (env.Build == "true" && ISSUE_NUMBER) {
prTools.comment(ISSUE_NUMBER, """{"body": "Jenkins failed during $currentBuild.displayName"}""", "spring_react")
}
}
}
}
}