-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
289 lines (270 loc) · 11.4 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
pipeline {
agent any
environment {
//BRANCH = "${env.GIT_BRANCH.split("/")[1]}"
BRANCH = getBranchName()
DOCKER_REGISTRY = getDockerRegistry(BRANCH)
DOCKER_TLS_VERIFY = "1"
DOCKER_HOST = "tcp://192.168.99.104:2376"
DOCKER_CERT_PATH = "/Users/aloksingh/.docker/machine/machines/default"
DOCKER_MACHINE_NAME = "default"
ENV_NAME = getEnvName(BRANCH)
AWS_CLI_KEY = getAwsCliKey(BRANCH)
AWS_CLI_SECRET = getAwsCliSecret(BRANCH)
//Use Pipeline Utility Steps plugin to read information from pom.xml into env variables - pipeline-utility-steps plugin
//ARTIFACT = readMavenPom().getArtifactId()
//VERSION = readMavenPom().getVersion()
DO_NOT_SKIP_BUILD = doNotSkipBuild(BRANCH)
}
stages {
stage ('Compile, Test and Package') {
when {
expression {return DO_NOT_SKIP_BUILD == 'true' }
}
steps {
withMaven(maven : 'maven-3-6-3') {
sh './mvnw clean jxr:jxr verify sonar:sonar package surefire-report:report-only'
}
}
}
stage ('Deploy Artifact') {
when {
expression {return DO_NOT_SKIP_BUILD == 'true' }
}
steps {
withMaven(maven : 'maven-3-6-3') {
echo "Skipping for now!"
//sh 'mvn deploy -DskipTests'
}
}
}
stage ('Build Docker Image app-one') {
when {
expression {return DO_NOT_SKIP_BUILD == 'true' }
}
steps {
script {
module = 'app-one'
def pom = readMavenPom file: "${module}/pom.xml"
ARTIFACT = pom.artifactId
VERSION = pom.version
echo "Building ${ARTIFACT} - ${VERSION} - ${ENV_NAME}"
if (BRANCH == 'master') {
sh "docker build -t ${DOCKER_REGISTRY}/${ARTIFACT}:latest -t ${DOCKER_REGISTRY}/${ARTIFACT}:${VERSION} --build-arg JAR_FILE=target/${ARTIFACT}-${VERSION}.jar --build-arg ENV_NAME=${ENV_NAME} ${module}/."
} else if (BRANCH == 'dev') {
sh "docker build -t ${DOCKER_REGISTRY}/${ARTIFACT}-dev:latest -t ${DOCKER_REGISTRY}/${ARTIFACT}-dev:${VERSION} --build-arg JAR_FILE=target/${ARTIFACT}-${VERSION}.jar --build-arg ENV_NAME=${ENV_NAME} ${module}/."
} else {
echo "Don't know how to create image for ${env.GIT_BRANCH} branch"
}
}
}
}
stage ('Push Docker Image app-one') {
when {
expression {return DO_NOT_SKIP_BUILD == 'true' }
}
steps {
script {
module = 'app-one'
def pom = readMavenPom file: "${module}/pom.xml"
ARTIFACT = pom.artifactId
VERSION = pom.version
if (BRANCH == 'master') {
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}:${VERSION}"
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}:latest"
} else if (BRANCH == 'dev') {
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}-dev:${VERSION}"
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}-dev:latest"
} else {
echo "Don't know which image to push ${env.BRANCH_NAME} branch"
}
}
}
}
stage ('Build Docker Image app-two') {
when {
expression {return DO_NOT_SKIP_BUILD == 'true' }
}
steps {
script {
module = 'app-two'
def pom = readMavenPom file: "${module}/pom.xml"
ARTIFACT = pom.artifactId
VERSION = pom.version
echo "Building ${ARTIFACT} - ${VERSION} - ${ENV_NAME}"
if (BRANCH == 'master') {
sh "docker build -t ${DOCKER_REGISTRY}/${ARTIFACT}:latest -t ${DOCKER_REGISTRY}/${ARTIFACT}:${VERSION} --build-arg JAR_FILE=target/${ARTIFACT}-${VERSION}.jar --build-arg ENV_NAME=${ENV_NAME} ${module}/."
} else if (BRANCH == 'dev') {
sh "docker build -t ${DOCKER_REGISTRY}/${ARTIFACT}-dev:latest -t ${DOCKER_REGISTRY}/${ARTIFACT}-dev:${VERSION} --build-arg JAR_FILE=target/${ARTIFACT}-${VERSION}.jar --build-arg ENV_NAME=${ENV_NAME} ${module}/."
} else {
echo "Don't know how to create image for ${env.GIT_BRANCH} branch"
}
}
}
}
stage ('Push Docker Image app-two') {
when {
expression {return DO_NOT_SKIP_BUILD == 'true' }
}
steps {
script {
module = 'app-two'
def pom = readMavenPom file: "${module}/pom.xml"
ARTIFACT = pom.artifactId
VERSION = pom.version
if (BRANCH == 'master') {
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}:${VERSION}"
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}:latest"
} else if (BRANCH == 'dev') {
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}-dev:${VERSION}"
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}-dev:latest"
} else {
echo "Don't know which image to push ${env.BRANCH_NAME} branch"
}
}
}
}
stage ('Build Docker Image rain-sensor') {
when {
expression {return DO_NOT_SKIP_BUILD == 'true' }
}
steps {
script {
module = 'rain-sensor'
def pom = readMavenPom file: "${module}/pom.xml"
ARTIFACT = pom.artifactId
VERSION = pom.version
echo "Building ${ARTIFACT} - ${VERSION} - ${ENV_NAME}"
if (BRANCH == 'master') {
sh "docker build -t ${DOCKER_REGISTRY}/${ARTIFACT}:latest -t ${DOCKER_REGISTRY}/${ARTIFACT}:${VERSION} --build-arg JAR_FILE=target/${ARTIFACT}-${VERSION}.jar --build-arg ENV_NAME=${ENV_NAME} ${module}/."
} else if (BRANCH == 'dev') {
sh "docker build -t ${DOCKER_REGISTRY}/${ARTIFACT}-dev:latest -t ${DOCKER_REGISTRY}/${ARTIFACT}-dev:${VERSION} --build-arg JAR_FILE=target/${ARTIFACT}-${VERSION}.jar --build-arg ENV_NAME=${ENV_NAME} ${module}/."
} else {
echo "Don't know how to create image for ${env.GIT_BRANCH} branch"
}
}
}
}
stage ('Push Docker Image rain-sensor') {
when {
expression {return DO_NOT_SKIP_BUILD == 'true' }
}
steps {
script {
module = 'rain-sensor'
def pom = readMavenPom file: "${module}/pom.xml"
ARTIFACT = pom.artifactId
VERSION = pom.version
if (BRANCH == 'master') {
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}:${VERSION}"
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}:latest"
} else if (BRANCH == 'dev') {
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}-dev:${VERSION}"
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}-dev:latest"
} else {
echo "Don't know which image to push ${env.BRANCH_NAME} branch"
}
}
}
}
stage ('Build Docker Image temperature-sensor') {
when {
expression {return DO_NOT_SKIP_BUILD == 'true' }
}
steps {
script {
module = 'temperature-sensor'
def pom = readMavenPom file: "${module}/pom.xml"
ARTIFACT = pom.artifactId
VERSION = pom.version
echo "Building ${ARTIFACT} - ${VERSION} - ${ENV_NAME}"
if (BRANCH == 'master') {
sh "docker build -t ${DOCKER_REGISTRY}/${ARTIFACT}:latest -t ${DOCKER_REGISTRY}/${ARTIFACT}:${VERSION} --build-arg JAR_FILE=target/${ARTIFACT}-${VERSION}.jar --build-arg ENV_NAME=${ENV_NAME} ${module}/."
} else if (BRANCH == 'dev') {
sh "docker build -t ${DOCKER_REGISTRY}/${ARTIFACT}-dev:latest -t ${DOCKER_REGISTRY}/${ARTIFACT}-dev:${VERSION} --build-arg JAR_FILE=target/${ARTIFACT}-${VERSION}.jar --build-arg ENV_NAME=${ENV_NAME} ${module}/."
} else {
echo "Don't know how to create image for ${env.GIT_BRANCH} branch"
}
}
}
}
stage ('Push Docker Image temperature-sensor') {
when {
expression {return DO_NOT_SKIP_BUILD == 'true' }
}
steps {
script {
module = 'temperature-sensor'
def pom = readMavenPom file: "${module}/pom.xml"
ARTIFACT = pom.artifactId
VERSION = pom.version
if (BRANCH == 'master') {
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}:${VERSION}"
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}:latest"
} else if (BRANCH == 'dev') {
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}-dev:${VERSION}"
sh "docker push ${DOCKER_REGISTRY}/${ARTIFACT}-dev:latest"
} else {
echo "Don't know which image to push ${env.BRANCH_NAME} branch"
}
}
}
}
}
}
def getBranchName() {
if (env.BRANCH_NAME.startsWith('PR')) {
return "${env.CHANGE_BRANCH}"
} else {
return "${env.BRANCH_NAME}"
}
}
def getEnvName(branchName) {
if( branchName == "master") {
return "prod";
} else if (branchName == "dev") {
return "dev";
} else {
return "future";
}
}
def doNotSkipBuild(branchName) {
echo "Branch Name: ${branchName}"
if( branchName == "master") {
echo "Master"
return 'true';
} else if (branchName == "dev") {
echo "Dev"
return 'true';
} else {
echo "Other"
return 'false';
}
}
def getDockerRegistry(branchName) {
if( branchName == "master") {
return "alokkusingh";
} else if (branchName == "dev") {
return "alokkusingh";
} else {
return "unknown";
}
}
def getAwsCliKey(branchName) {
if( branchName == "master") {
return "";
} else if (branchName == "dev") {
return "";
} else {
return "unknown";
}
}
def getAwsCliSecret(branchName) {
if( branchName == "master") {
return "";
} else if (branchName == "dev") {
return "";
} else {
return "unknown";
}
}