/
Jenkinsfile
499 lines (451 loc) · 17.3 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
import groovy.transform.Field
/*
* See https://github.com/hibernate/hibernate-jenkins-pipeline-helpers
*/
@Library('hibernate-jenkins-pipeline-helpers@1.2')
import org.hibernate.jenkins.pipeline.helpers.job.JobHelper
import org.hibernate.jenkins.pipeline.helpers.alternative.AlternativeMultiMap
import org.hibernate.jenkins.pipeline.helpers.version.Version
/*
* WARNING: DO NOT IMPORT LOCAL LIBRARIES HERE.
*
* By local, I mean libraries whose files are in the same Git repository.
*
* The Jenkinsfile is protected and will not be executed if modified in pull requests from external users,
* but other local library files are not protected.
* A user could potentially craft a malicious PR by modifying a local library.
*
* See https://blog.grdryn.me/blog/jenkins-pipeline-trust.html for a full explanation,
* and a potential solution if we really need local libraries.
* Alternatively we might be able to host libraries in a separate GitHub repo and configure
* them in the GUI: see http://ci.hibernate.org/job/hibernate-search-6-poc/configure, "Pipeline Libraries".
*/
/*
* See https://github.com/hibernate/hibernate-jenkins-pipeline-helpers for the documentation
* of the helpers library used in this Jenkinsfile,
* and for help writing Jenkinsfiles.
*
* ### Jenkins configuration
*
* #### Jenkins plugins
*
* This file requires the following plugins in particular:
*
* - everything required by the helpers library (see the org.hibernate.(...) imports for a link to its documentation)
* - https://plugins.jenkins.io/pipeline-github for the trigger on pull request comments
*
* #### Script approval
*
* If not already done, you will need to allow the following calls in <jenkinsUrl>/scriptApproval/:
*
* - everything required by the helpers library (see the org.hibernate.(...) imports for a link to its documentation)
*
* ### Integrations
*
* #### Nexus deployment
*
* This job includes two deployment modes:
*
* - A deployment of snapshot artifacts for every non-PR build on "primary" branches (master and maintenance branches).
* - A full release when starting the job with specific parameters.
*
* In the first case, the name of a Maven settings file must be provided in the job configuration file
* (see below).
*
* #### Gitter (optional)
*
* You need to enable the Jenkins integration in your Gitter room first:
* see https://gitlab.com/gitlab-org/gitter/webapp/blob/master/docs/integrations.md
*
* Then you will also need to configure *global* secret text credentials containing the Gitter webhook URL,
* and list the ID of these credentials in the job configuration file
* (see https://github.com/hibernate/hibernate-jenkins-pipeline-helpers#job-configuration-file).
*
* ### Job configuration
*
* This Jenkinsfile gets its configuration from two sources:
* branch name, and a configuration file.
* All configuration is optional for the default build (and it should stay that way),
* but some features require some configuration.
*
* #### Branch name
*
* See the org.hibernate.(...) imports for a link to the helpers library documentation,
* which explains the basics.
*
* #### Job configuration file
*
* See the org.hibernate.(...) imports for a link to the helpers library documentation,
* which explains the basic structure of this file and how to set it up.
*
* Below is the additional structure specific to this Jenkinsfile:
*
* deployment:
* maven:
* # String containing the ID of a Maven settings file registered using the config-file-provider Jenkins plugin.
* # The settings must provide credentials to the servers with ID
* # 'jboss-releases-repository' and 'jboss-snapshots-repository'.
* settingsId: ...
*/
@Field final String MAVEN_TOOL = 'Apache Maven 3.5.4'
// Default node pattern, to be used for resource-intensive stages.
// Should not include the master node.
@Field final String NODE_PATTERN_BASE = 'Slave'
// Quick-use node pattern, to be used for very light, quick, and environment-independent stages,
// such as sending a notification. May include the master node in particular.
@Field final String QUICK_USE_NODE_PATTERN = 'Master||Slave'
@Field AlternativeMultiMap<ITEnvironment> environments
@Field JobHelper helper
@Field boolean enableDefaultBuild = false
@Field boolean enableDefaultEnvIT = false
@Field boolean enableNonDefaultSupportedEnvIT = false
@Field boolean enableExperimentalEnvIT = false
@Field boolean performRelease = false
@Field boolean deploySnapshot = false
@Field Version releaseVersion
@Field Version afterReleaseDevelopmentVersion
this.helper = new JobHelper(this)
helper.runWithNotification {
stage('Configure') {
this.environments = AlternativeMultiMap.create([
jdk: [
new JdkITEnvironment(version: '8', tool: 'OpenJDK 8 Latest', status: ITEnvironmentStatus.USED_IN_DEFAULT_BUILD),
new JdkITEnvironment(version: '9', tool: 'OpenJDK 9 Latest', status: ITEnvironmentStatus.EXPERIMENTAL)
],
database: [
new DatabaseITEnvironment(dbName: 'h2', mavenProfile: 'h2', status: ITEnvironmentStatus.USED_IN_DEFAULT_BUILD),
new DatabaseITEnvironment(dbName: 'mariadb', mavenProfile: 'ci-mariadb', status: ITEnvironmentStatus.SUPPORTED),
// There are some bugs in our integration tests (the tests, not Search itself),
// and fixes have not been backported to 5.6.
// Disabling this build for now.
new DatabaseITEnvironment(dbName: 'postgresql', mavenProfile: 'ci-postgresql', status: ITEnvironmentStatus.EXPERIMENTAL)
],
esLocal: [
new EsLocalITEnvironment(versionRange: '[2.0,2.2)', mavenProfile: 'elasticsearch-2.0', status: ITEnvironmentStatus.SUPPORTED),
new EsLocalITEnvironment(versionRange: '[2.2,5.0)', mavenProfile: 'elasticsearch-2.2', status: ITEnvironmentStatus.USED_IN_DEFAULT_BUILD)
]
])
helper.configure {
configurationNodePattern QUICK_USE_NODE_PATTERN
file 'job-configuration.yaml'
jdk {
defaultTool environments.content.jdk.default.tool
}
maven {
defaultTool MAVEN_TOOL
producedArtifactPattern "org/hibernate/hibernate-search*"
}
}
properties([
buildDiscarder(
logRotator(daysToKeepStr: '90')
),
pipelineTriggers(
// HSEARCH-3417: do not add snapshotDependencies() here, this was known to cause problems.
[
issueCommentTrigger('.*test this please.*')
]
+ helper.generateUpstreamTriggers()
),
helper.generateNotificationProperty(),
parameters([
choice(
name: 'INTEGRATION_TESTS',
choices: """AUTOMATIC
DEFAULT_ENV_ONLY
SUPPORTED_ENV_ONLY
EXPERIMENTAL_ENV_ONLY
ALL_ENV""",
defaultValue: 'AUTOMATIC',
description: """Which integration tests to run.
'AUTOMATIC' chooses based on the branch name and whether a release is being performed.
'DEFAULT_ENV_ONLY' means a single build, while other options will trigger multiple Maven executions in different environments."""
),
string(
name: 'RELEASE_VERSION',
defaultValue: '',
description: 'The version to be released, e.g. 5.10.0.Final. Setting this triggers a release.',
trim: true
),
string(
name: 'RELEASE_DEVELOPMENT_VERSION',
defaultValue: '',
description: 'The next version to be used after the release, e.g. 5.10.0-SNAPSHOT.',
trim: true
),
booleanParam(
name: 'RELEASE_DRY_RUN',
defaultValue: false,
description: 'If true, just simulate the release, without pushing any commits or tags, and without uploading any artifacts or documentation.'
)
])
])
performRelease = (params.RELEASE_VERSION ? true : false)
if (!performRelease && helper.scmSource.branch.primary && !helper.scmSource.pullRequest) {
if (helper.configuration.file?.deployment?.maven?.settingsId) {
deploySnapshot = true
}
else {
echo "Missing deployment configuration in job configuration file - snapshot deployment will be skipped."
}
}
switch (params.INTEGRATION_TESTS) {
case 'DEFAULT_ENV_ONLY':
enableDefaultEnvIT = true
break
case 'SUPPORTED_ENV_ONLY':
enableDefaultEnvIT = true
enableNonDefaultSupportedEnvIT = true
break
case 'ALL_ENV':
enableDefaultEnvIT = true
enableNonDefaultSupportedEnvIT = true
enableExperimentalEnvIT = true
break
case 'EXPERIMENTAL_ENV_ONLY':
enableExperimentalEnvIT = true
break
case 'AUTOMATIC':
if (params.RELEASE_VERSION) {
echo "Skipping default build and integration tests to speed up the release of version $params.RELEASE_VERSION"
} else if (helper.scmSource.pullRequest) {
echo "Enabling only the default build and integration tests in the default environment for pull request $helper.scmSource.pullRequest.id"
enableDefaultEnvIT = true
} else if (helper.scmSource.branch.primary) {
echo "Enabling integration tests on all supported environments for primary branch '$helper.scmSource.branch.name'"
enableDefaultBuild = true
enableDefaultEnvIT = true
enableNonDefaultSupportedEnvIT = true
} else {
echo "Enabling only the default build and integration tests in the default environment for feature branch $helper.scmSource.branch.name"
enableDefaultBuild = true
enableDefaultEnvIT = true
}
break
default:
throw new IllegalArgumentException(
"Unknown value for param 'INTEGRATION_TESTS': '$params.INTEGRATION_TESTS'."
)
}
enableDefaultBuild =
enableDefaultEnvIT || enableNonDefaultSupportedEnvIT || enableExperimentalEnvIT || deploySnapshot
echo """Branch: ${helper.scmSource.branch.name}, PR: ${helper.scmSource.pullRequest?.id}, integration test setting: $params.INTEGRATION_TESTS, resulting execution plan:
enableDefaultBuild=$enableDefaultBuild
enableDefaultEnvIT=$enableDefaultEnvIT
enableNonDefaultSupportedEnvIT=$enableNonDefaultSupportedEnvIT
enableExperimentalEnvIT=$enableExperimentalEnvIT
performRelease=$performRelease
deploySnapshot=$deploySnapshot"""
// Filter environments
environments.content.each { key, envSet ->
// No need to re-test default environments, they are already tested as part of the default build
envSet.enabled.remove(envSet.default)
if (!enableNonDefaultSupportedEnvIT) {
envSet.enabled.removeAll { itEnv -> itEnv.status == ITEnvironmentStatus.SUPPORTED }
}
if (!enableExperimentalEnvIT) {
envSet.enabled.removeAll { itEnv -> itEnv.status == ITEnvironmentStatus.EXPERIMENTAL }
}
}
if (environments.isAnyEnabled()) {
echo "Enabled non-default environment ITs: ${environments.enabledAsString}"
}
else {
echo "Non-default environment ITs are completely disabled."
}
if (performRelease) {
releaseVersion = Version.parseReleaseVersion(params.RELEASE_VERSION)
echo "Inferred version family for the release to '$releaseVersion.family'"
// Check that all the necessary parameters are set
if (!params.RELEASE_DEVELOPMENT_VERSION) {
throw new IllegalArgumentException(
"Missing value for parameter RELEASE_DEVELOPMENT_VERSION." +
" This parameter must be set when RELEASE_VERSION is set."
)
}
if (!params.RELEASE_DRY_RUN && !helper.configuration.file?.deployment?.maven?.settingsId) {
throw new IllegalArgumentException(
"Missing deployment configuration in job configuration file." +
" Cannot deploy artifacts during the release."
)
}
}
if (params.RELEASE_DEVELOPMENT_VERSION) {
afterReleaseDevelopmentVersion = Version.parseDevelopmentVersion(params.RELEASE_DEVELOPMENT_VERSION)
}
}
stage('Default build') {
if (!enableDefaultBuild) {
echo 'Skipping default build and integration tests in the default environment'
helper.markStageSkipped()
return
}
node(NODE_PATTERN_BASE) {
helper.withMavenWorkspace {
sh """ \
mvn clean install \
-Pdocbook,dist \
${enableDefaultEnvIT ? '' : '-DskipTests'} \
"""
dir(helper.configuration.maven.localRepositoryPath) {
stash name:'main-build', includes:"org/hibernate/hibernate-search*/**"
}
}
}
}
stage('Non-default environment ITs') {
Map<String, Closure> executions = [:]
// Test with multiple JDKs
environments.content.jdk.enabled.each { JdkITEnvironment itEnv ->
executions.put(itEnv.tag, {
node(NODE_PATTERN_BASE) {
helper.withMavenWorkspace(jdk: itEnv.tool) {
mavenNonDefaultIT itEnv, """ \
clean install --fail-at-end \
"""
}
}
})
}
// Test ORM integration with multiple databases
environments.content.database.enabled.each { DatabaseITEnvironment itEnv ->
executions.put(itEnv.tag, {
node(NODE_PATTERN_BASE) {
helper.withMavenWorkspace {
resumeFromDefaultBuild()
mavenNonDefaultIT itEnv, """ \
clean install -pl org.hibernate:hibernate-search-orm -P$itEnv.mavenProfile \
"""
}
}
})
}
// Test Elasticsearch integration with multiple versions in a local instance
environments.content.esLocal.enabled.each { EsLocalITEnvironment itEnv ->
executions.put(itEnv.tag, {
node(NODE_PATTERN_BASE) {
helper.withMavenWorkspace {
resumeFromDefaultBuild()
mavenNonDefaultIT itEnv, """ \
clean install -pl org.hibernate:hibernate-search-elasticsearch \
${toMavenElasticsearchProfileArg(itEnv.mavenProfile)} \
"""
}
}
})
}
if (executions.isEmpty()) {
echo 'Skipping integration tests in non-default environments'
helper.markStageSkipped()
}
else {
parallel(executions)
}
}
stage('Deploy') {
if (deploySnapshot) {
echo "Deploying snapshots"
node(NODE_PATTERN_BASE) {
helper.withMavenWorkspace(mavenSettingsConfig: helper.configuration.file.deployment.maven.settingsId) {
sh "mvn clean deploy -Pdist -DskipTests"
}
}
}
else if (performRelease) {
echo "Performing full release for version ${releaseVersion.toString()}"
node(NODE_PATTERN_BASE) {
helper.withMavenWorkspace(mavenSettingsConfig: params.RELEASE_DRY_RUN ? null : helper.configuration.file.deployment.maven.settingsId) {
sh "git clone https://github.com/hibernate/hibernate-noorm-release-scripts.git"
sh "bash -xe hibernate-noorm-release-scripts/prepare-release.sh search ${releaseVersion.toString()}"
String deployCommand = "bash -xe hibernate-noorm-release-scripts/deploy.sh search"
if (!params.RELEASE_DRY_RUN) {
sh deployCommand
} else {
echo "WARNING: Not deploying. Would have executed:"
echo deployCommand
}
String uploadDistributionCommand = "bash -xe hibernate-noorm-release-scripts/upload-distribution.sh search ${releaseVersion.toString()}"
String uploadDocumentationCommand = "bash -xe hibernate-noorm-release-scripts/upload-documentation.sh search ${releaseVersion.toString()} ${releaseVersion.family}"
if (!params.RELEASE_DRY_RUN) {
sh uploadDistributionCommand
sh uploadDocumentationCommand
}
else {
echo "WARNING: Not uploading anything. Would have executed:"
echo uploadDistributionCommand
echo uploadDocumentationCommand
}
sh "bash -xe hibernate-noorm-release-scripts/update-version.sh search ${afterReleaseDevelopmentVersion.toString()}"
sh "bash -xe hibernate-noorm-release-scripts/push-upstream.sh search ${releaseVersion.toString()} ${helper.scmSource.branch.name} ${!params.RELEASE_DRY_RUN}"
}
}
}
else {
echo "Skipping deployment"
helper.markStageSkipped()
return
}
}
} // End of helper.runWithNotification
// Job-specific helpers
enum ITEnvironmentStatus {
// For environments used as part of the integration tests in the default build (tested on all branches)
USED_IN_DEFAULT_BUILD,
// For environments that are expected to work correctly (tested on master and maintenance branches)
SUPPORTED,
// For environments that may not work correctly (only tested when explicitly requested through job parameters)
EXPERIMENTAL;
// Work around JENKINS-33023
// See https://issues.jenkins-ci.org/browse/JENKINS-33023?focusedCommentId=325738&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-325738
public ITEnvironmentStatus() {}
}
abstract class ITEnvironment {
ITEnvironmentStatus status
String toString() { getTag() }
abstract String getTag()
boolean isDefault() { status == ITEnvironmentStatus.USED_IN_DEFAULT_BUILD }
}
class JdkITEnvironment extends ITEnvironment {
String version
String tool
String getTag() { "jdk-$version" }
}
class DatabaseITEnvironment extends ITEnvironment {
String dbName
String mavenProfile
String getTag() { "database-$dbName" }
}
class EsLocalITEnvironment extends ITEnvironment {
String versionRange
String mavenProfile
String getTag() { "elasticsearch-local-$versionRange" }
}
void resumeFromDefaultBuild() {
dir(helper.configuration.maven.localRepositoryPath) {
unstash name:'main-build'
}
}
void mavenNonDefaultIT(ITEnvironment itEnv, String args) {
// Add a suffix to tests to distinguish between different executions
// of the same test in different environments in reports
def testSuffix = itEnv.tag.replaceAll('[^a-zA-Z0-9_\\-+]+', '_')
sh "mvn -Dsurefire.environment=$testSuffix $args"
}
String toMavenElasticsearchProfileArg(String mavenEsProfile) {
String defaultEsProfile = environments.content.esLocal.default.mavenProfile
if (mavenEsProfile != defaultEsProfile) {
// Disable the default profile to avoid conflicting configurations
"-P!$defaultEsProfile,$mavenEsProfile"
}
else {
// Do not do as above, as we would tell Maven "disable the default profile, but enable it"
// and Maven would end up disabling it.
''
}
}