-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jenkinsfile
441 lines (382 loc) · 19.5 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// The GIT repository for this pipeline lib is defined in the global Jenkins setting
@Library('jenkins-pipeline-library@nexus') import com.gentics.*
// Make the helpers aware of this jobs environment
JobContext.set(this)
final def gitCommitTag = '[Jenkins | ' + env.JOB_BASE_NAME + ']';
final def testDbManagerHost = "gcn-testdb-manager.gtx-dev.svc"
final def testDbManagerPort = "8080"
def branchName = null
def version = null
def releaseVersion = ""
def tagName = null
def dockerImageTag = null
def runJUnitTests = true
def qaDeploy = false
def qaDeployBranchList = ["dev"] as String[]
def imageHost = "push.docker.gentics.com"
def imageRepo = "docker-snapshots"
pipeline {
agent {
kubernetes {
label env.BUILD_TAG.take(63)
defaultContainer 'build'
yaml ocpWorker("""
apiVersion: v1
kind: Pod
metadata:
labels:
jenkinsbuild: true
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: jenkinsbuild
operator: In
values:
- true
topologyKey: kubernetes.io/hostname
containers:
- name: build
image: """ + buildEnvironmentDockerImage("build/Dockerfile", "cms-oss") + """
resources:
requests:
cpu: '0'
memory: '0'
limits:
cpu: '0'
memory: '0'
- name: docker
resources:
limits:
cpu: '0'
memory: '0'
requests:
cpu: '0'
memory: '0'
- name: jnlp
resources:
limits:
cpu: '0'
memory: '0'
requests:
cpu: '0'
memory: '0'
""")
}
}
parameters {
booleanParam(name: 'checkGitCommit', defaultValue: false, description: 'If set to true, the current git revision is compared with the git revision of the last successful build. If they are equal, the build is skipped and env.BUILD_SKIPPED is set to true')
booleanParam(name: 'runTests', defaultValue: true, description: "Whether to run the unit tests. tests will be skipped for MR builds if there are no relevant changes.")
booleanParam(name: 'runBaseLibTests', defaultValue: false, description: "Whether to run tests from the base-lib module.")
string(name: 'singleTest', defaultValue: "", description: "Only this test will be run. Example: com.gentics.contentnode.tests.validation.validator.impl.AttributeValidatorTest")
booleanParam(name: 'integrationTests', defaultValue: false, description: "Whether to run integration tests.")
booleanParam(name: 'deploy', defaultValue: false, description: "Deploy the Maven artifacts, push the docker image and push GIT commits and tags")
booleanParam(name: 'deployTesting', defaultValue: false, description: "Like deploy, but only the server image will be built and deployed to a different repository")
booleanParam(name: 'install', defaultValue: false, description: "Install the Maven artifacts to the local repository (unless deploy or runReleaseBuild is true). If this is set, no tests will be executed (regardless of other settings).")
booleanParam(name: 'runReleaseBuild', defaultValue: false, description: "Do a release build including setting the release version, and adding GIT commits and a GIT tag (last two for releases only)")
booleanParam(name: 'tagRelease', defaultValue: true, description: "Release: Whether to create a GIT tag")
booleanParam(name: 'releaseWithNewChangesOnly', defaultValue: true, description: "Release: Abort the build if there are no new changes")
booleanParam(name: 'mergeHotfixBranch', defaultValue: true, description: "Release: Whether to merge the corresponding hotfix branch first (release branches only)")
booleanParam(name: 'runDockerBuild', defaultValue: true, description: "Whether to build the docker image (use deploy to push it also).")
string(name: 'forceVersion', defaultValue: "", description: "If not empty, the build/release will be done using this POM version")
string(name: 'sourceBranch', defaultValue: "", description: "Will only work if the job has */\${sourceBranch} as GIT branch defined")
}
options {
withCredentials([usernamePassword(credentialsId: 'docker.gentics.com', usernameVariable: 'repoUsername', passwordVariable: 'repoPassword')])
gitLabConnection('git.gentics.com')
gitlabBuilds(builds: ['Jenkins build'])
timestamps()
timeout(time: 4, unit: 'HOURS')
ansiColor('xterm')
}
stages {
stage('Check git commit') {
when {
expression {
return Boolean.valueOf(params.checkGitCommit)
}
}
steps {
script {
if ( env.GIT_COMMIT == env.GIT_PREVIOUS_SUCCESSFUL_COMMIT ) {
echo "env.GIT_COMMIT (" + env.GIT_COMMIT + ") = env.GIT_PREVIOUS_SUCCESSFUL_COMMIT (" + env.GIT_PREVIOUS_SUCCESSFUL_COMMIT + "). Skip building."
env.BUILD_SKIPPED = "true"
} else {
echo "env.GIT_COMMIT (" + env.GIT_COMMIT + ") != env.GIT_PREVIOUS_SUCCESSFUL_COMMIT (" + env.GIT_PREVIOUS_SUCCESSFUL_COMMIT + "). Need to rebuild."
}
}
}
}
stage("Build, Deploy") {
when {
expression {
return env.BUILD_SKIPPED != "true"
}
}
steps {
updateGitlabCommitStatus name: 'Jenkins build', state: "running"
script {
def mvnGoal = "package"
def mvnArguments = "-Dnodejs.npm.bin=/opt/node/bin/npm "
version = params.forceVersion
branchName = GitHelper.fetchCurrentBranchName()
if (!version && params.runReleaseBuild) {
version = MavenHelper.getVersion()
}
// Merge the hotfix branch if building a release branch
if (params.mergeHotfixBranch && branchName.startsWith("release-")) {
def branchToMerge = branchName.replaceFirst(/^release-/, "hotfix-")
try {
GitHelper.merge('origin/' + branchToMerge)
} catch (Exception e) {
error 'Couldn\'t merge ref origin/' + branchToMerge + 'into ' + branchName
}
}
if (version) {
if (params.runReleaseBuild) {
version = MavenHelper.transformSnapshotToReleaseVersion(version)
}
MavenHelper.setVersion(version)
currentBuild.description = version
}
if (params.runTests) {
if (!params.runBaseLibTests) {
mvnArguments += "-Dsurefire.baselib.excludedGroups=com.gentics.contentnode.tests.category.BaseLibTest"
}
mvnArguments += (params.singleTest ? " -am -pl 'cms-core,cms-oss-server' -Dui.skip.build -DfailIfNoTests=false -Dsurefire.failIfNoSpecifiedTests=false -Dtest=" + params.singleTest : "")
// Check if triggered by a Gitlab merge request
if (env.gitlabTargetBranch) {
runJUnitTests = GitHelper.checkForChangesInPaths("origin/" + env.gitlabTargetBranch,(String[])[
"base-api/",
"base-lib/",
"cms-api/",
"cms-cache/",
"cms-cache/",
"cms-core/",
"cms-oss-server/",
"cms-restapi/"
])
if (!runJUnitTests) {
mvnArguments += " -Dskip.unit.tests"
}
}
} else {
mvnArguments += " -DskipTests=true -Dskip.unit.tests=true -Dui.skip.test=true"
runJUnitTests = false
}
// when deploying for the test systems, we do not build the changelog or doc
if (params.deployTesting) {
mvnArguments += " -Dui.skip.publish -pl '!cms-oss-changelog,!cms-oss-doc'"
}
// Update chrome to the latest version
//sh "sudo apt-get update"
//sh "sudo apt-get install --assume-yes --allow-unauthenticated google-chrome-beta"
if (params.runReleaseBuild) {
// Release
echo "Invoking release build on branch " + branchName + ".."
currentBuild.description += ' - Release'
if (params.releaseWithNewChangesOnly) {
def lastCommitMessage = GitHelper.getLastCommitMessage().trim()
if (lastCommitMessage.startsWith(gitCommitTag)) {
error "Aborting the release build because there are no new changes. Last commit message is: \"" + lastCommitMessage + "\""
}
}
def codeName = null
if (branchName =~ /(release|hotfix)-.*/) {
def branchNameSplitters = branchName.split('-')
codeName = branchNameSplitters[1]
} else {
echo 'Warning: The current branch name ' + branchName + ' does not match the patterns hotfix-* or release-*. Pushing to the default Artifactory repository'
}
mvnGoal = "deploy"
} else {
// for now, do not build modules in parallel
// mvnArguments += " -T 1C"
if (params.deploy || params.deployTesting) {
// Deploy
mvnGoal = "deploy"
} else if (params.install) {
// Install
mvnGoal = "install"
mvnArguments = " -am -pl 'cms-oss-bom,cms-core,cms-oss-server,cms-ui' -DskipTests=true -Dskip.unit.tests -Dui.skip.test=true -Dnodejs.npm.bin=/opt/node/bin/npm -Dui.skip.publish"
}
}
if (mvnGoal == "deploy") {
// Fix for NPE when uploading pom to Artifactory that includes <?m2e ?> directives
// See: https://www.jfrog.com/jira/browse/RTFACT-17932
sh "find . -maxdepth 3 -type f -name 'pom.xml' -print0 | xargs -0 sed -i -r 's/<\\?m2e[[:blank:]]+[[:alnum:]]+[[:blank:]]*\\?>//g'"
}
// Add private repository credentials and scopes
sh "echo @gentics:registry=https://repo.gentics.com/repository/npm-products/> ~/.npmrc"
withCredentials([string(credentialsId: 'nexus-npm', variable: 'NPM_TOKEN')]) {
sh "echo //repo.gentics.com/repository/npm-products/:_auth=${env.NPM_TOKEN} >> ~/.npmrc"
}
// Login to docker.gentics.com so that the tests can pull all Mesh images
authDockerRegistry("docker.gentics.com", "docker.gentics.com")
authDockerRegistry("docker.gentics.com", "push.docker.gentics.com")
withEnv(["TESTMANAGER_HOSTNAME=" + testDbManagerHost, "TESTMANAGER_PORT=" + testDbManagerPort, "TESTCONTAINERS_RYUK_DISABLED=true"]) {
sh "mvn -B -Dstyle.color=always -U -Dskip.integration.tests -Dui.skip.integrationTest=true " +
" -fae -Dmaven.test.failure.ignore=true " + mvnArguments + " clean " + mvnGoal
}
if (params.runReleaseBuild) {
// Fix for NPE when uploading pom to Artifactory that includes <?m2e ?> directives
// See: https://www.jfrog.com/jira/browse/RTFACT-17932
// Revert the workaround again for the GIT commit
sh "find . -maxdepth 3 -type f -name 'pom.xml' -print0 -exec git checkout {} \\;"
MavenHelper.setVersion(version)
// Add the modified pom.xml and the generated changelog file
def releaseMessage = 'Release of version ' + version
GitHelper.addCommit('.', gitCommitTag + ' ' + releaseMessage)
if (params.tagRelease) {
tagName = version
GitHelper.addTag(tagName, releaseMessage)
}
}
}
}
post {
always {
script {
// Ignore missing test results if we only run one test
boolean allowEmptyResults = (params.singleTest ? true : false)
boolean allowEmptyBaseLibResults = (!params.runBaseLibTests || params.singleTest ? true : false)
if (params.runTests) {
if (runJUnitTests) {
junit testResults: "base-lib/target/surefire-reports/TEST-*.xml", allowEmptyResults: allowEmptyBaseLibResults
junit testResults: "cms-core/target/surefire-reports/TEST-*.xml", allowEmptyResults: allowEmptyResults
junit testResults: "cms-oss-server/target/surefire-reports/TEST-*.xml", allowEmptyResults: allowEmptyResults
}
junit testResults: "cms-ui/.reports/**/JEST-report.xml", allowEmptyResults: allowEmptyResults
junit testResults: "cms-ui/.reports/**/KARMA-report.xml", allowEmptyResults: allowEmptyResults
junit testResults: "cms-ui/.reports/**/CYPRESS-component-report.xml", allowEmptyResults: allowEmptyResults
}
}
}
}
}
stage("Docker build") {
when {
expression {
// Build the docker image only if the parameter runDockerBuild is enabled and
return env.BUILD_SKIPPED != "true" && params.runDockerBuild &&
(!env.gitlabTargetBranch || qaDeployBranchList.contains(branchName))
}
}
environment {
DOCKER_TAG = "${branchName}"
}
steps {
script {
if (params.runReleaseBuild) {
imageRepo = "docker-products"
}
def imageName = "${imageHost}/${imageRepo}/gentics/cms-oss"
def imageNameWithTag = "${imageName}:${branchName}"
authDockerRegistry("docker.gentics.com", "docker.gentics.com")
authDockerRegistry("docker.gentics.com", "push.docker.gentics.com")
sh "cd cms-oss-server ; docker build --network=host -t ${imageNameWithTag} ."
scanImage image: imageNameWithTag, exitCode: 1
if (tagName != null) {
String dockerImageVersionTag = imageName + ":" + tagName
sh "docker tag " + imageNameWithTag + " " + dockerImageVersionTag
}
}
}
}
stage("Docker Push") {
when {
expression {
// Build the docker image only if the parameter runDockerBuild is enabled and
return env.BUILD_SKIPPED != "true" && params.runDockerBuild &&
(!env.gitlabTargetBranch || qaDeployBranchList.contains(branchName))
}
}
environment {
DOCKER_TAG = "${branchName}"
}
steps {
script {
def imageName = "${imageHost}/${imageRepo}/gentics/cms-oss"
def imageNameWithTag = "${imageName}:${branchName}"
authDockerRegistry("docker.gentics.com", "docker.gentics.com")
authDockerRegistry("docker.gentics.com", "push.docker.gentics.com")
// Push released image
if (tagName != null) {
String dockerImageVersionTag = imageName + ":" + tagName
sh "docker push " + dockerImageVersionTag
} else if (params.deploy || params.deployTesting) {
// push snapshot build image
sh "docker push ${imageNameWithTag}"
}
}
}
}
stage("UI Integration Tests") {
when {
expression {
// Needs built docker images to be deployed (as they are used in another job)
// And only if the tests should be performed as well
return env.BUILD_SKIPPED != "true" && params.runDockerBuild && (params.deploy || params.deployTesting) && params.integrationTests
}
}
steps {
script {
def integrationCmsVersion = tagName != null && !tagName.isEmpty() ? tagName : branchName;
def testJob = build(job: 'cms-ui-integration-tests/' + branchName,
parameters: [
string(name: 'variant', value: 'OSS'),
string(name: 'cmsVersion', value: branchName),
// TODO: Get Mesh Version from POM?
// string(name: 'meshVersion', value: '2.1.0')
],
wait: true
)
if ( testJob.getBuildVariables()["BUILD_SKIPPED"] == "true" ) {
echo "Integration Tests finished with an error"
} else {
echo "Integration Tests finished successfully"
}
}
}
}
stage("Deploy QA images to Kubernetes") {
when {
expression {
return env.BUILD_SKIPPED != "true" && qaDeploy
}
}
steps {
build job: 'contentnode-qa-deploy',
parameters: [
string(name: 'branchName', value: branchName)
],
wait: false
}
}
stage("Git push") {
when {
expression {
return env.BUILD_SKIPPED != "true" && params.runReleaseBuild && params.deploy
}
}
steps {
script {
releaseVersion = version
version = MavenHelper.getNextSnapShotVersion(version)
MavenHelper.setVersion(version)
GitHelper.addCommit('.', gitCommitTag + ' Prepare for the next development iteration (' + version + ')')
sshagent(["git"]) {
GitHelper.pushBranch(branchName)
if (tagName != null) {
GitHelper.pushTag(tagName)
}
}
}
}
}
}
}