forked from realm/realm-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
750 lines (703 loc) · 31.1 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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
/*
* Copyright 2020 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import groovy.json.JsonOutput
@Library('realm-ci') _
// Branches from which we release SNAPSHOT's. Only release branches need to run on actual hardware.
releaseBranches = [ 'main', 'releases', 'next-major' ]
// Branches that are "important", so if they do not compile they will generate a Slack notification
slackNotificationBranches = [ 'main', 'releases', 'next-major' ]
// Shortcut to current branch name that is being tested
currentBranch = (env.CHANGE_BRANCH == null) ? env.BRANCH_NAME : env.CHANGE_BRANCH
// Will be set to `true` if this build is a full release that should be available on Bintray.
// This is determined by comparing the current git tag to the version number of the build.
publishBuild = false
// Version of Realm Kotlin being tested. This values is defined in `buildSrc/src/main/kotlin/Config.kt`.
version = null
// Wether or not to run test steps
runTests = true
isReleaseBranch = releaseBranches.contains(currentBranch)
// Manually wipe the workspace before checking out the code. This happens automatically on release branches.
forceWipeWorkspace = false
// References to Docker containers holding the MongoDB Test server and infrastructure for
// controlling it.
mongoDbRealmContainer = null
// Mac CI dedicated machine
node_label = 'osx_kotlin'
// When having multiple executors available, Jenkins might append @2/@3/etc. to workspace folders in order
// to allow multiple parallel builds on the same branch. Unfortunately this breaks Ninja and thus us
// building native code. To work around this, we force the workspace to mirror the git path.
// This has two side-effects: 1) It isn't possible to use this JenkinsFile on a worker with multiple
// executors. At least not if we want to support building multiple versions of the same PR.
workspacePath = "/Users/realm/workspace-realm-kotlin/${currentBranch}"
// Build type for native libraries for Linux and Windows. Should either be "Release" or "Debug"
jvmNativeBuildType="Release"
pipeline {
agent none
options {
// In Realm Java, we had to lock the entire build as sharing the global Gradle
// cache was causing issues. We never discovered the root cause, but
// https://github.com/gradle/gradle/issues/851 seems to indicate that the problem
// is when running builds inside Docker containers that share a host .gradle
// folder.
//
// This isn't the case for Kotlin, so it seems safe to remove the lock.
// Locking is furthermore complicated by the fact that there doesn't seem an
// easy way to grap a node-lock for pipeline syntax builds.
// https://stackoverflow.com/a/44758361/1389357.
//
// So in summary, removing the lock should work fine. I'm mostly keeping this
// description in case we run into problems down the line.
// lock resource: 'kotlin_build_lock'
// Overall job timeout. This doesn't include time for waiting on the agent.
// Setting 'activity'-timeouts here doesn't clear the overall job timeout, so
// not an option. More finegrained timeouts can be targeted at specific
// stages, but these will include the time waiting for resources/nodes.
timeout(time: 180, unit: 'MINUTES')
}
environment {
ANDROID_HOME='/Users/realm/Library/Android/sdk/'
ANDROID_SDK_ROOT='/Users/realm/Library/Android/sdk/'
NDK_HOME='/Users/realm/Library/Android/sdk/ndk/22.0.6917172'
ANDROID_NDK="${NDK_HOME}"
ANDROID_NDK_HOME="${NDK_HOME}"
REALM_DISABLE_ANALYTICS=true
JAVA_8='/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home'
JAVA_11='/Library/Java/JavaVirtualMachines/jdk-11.0.12.jdk/Contents/Home'
JAVA_HOME="${JAVA_11}"
}
stages {
stage('Prepare CI') {
// Force all stages to use the same node, so we can take advantage
// of the gradle cache between steps, otherwise Jenkins are free
// to move a stage to a different node.
agent {
node {
label node_label
customWorkspace workspacePath
}
}
stages {
stage('SCM') {
steps {
runScm()
setBuildDetails()
genAndStashSwigJNI()
}
}
stage('build-jvm-native-libs') {
parallel{
stage('build_jvm_linux') {
when { expression { shouldBuildJvmABIs() } }
agent {
node {
label 'docker'
}
}
steps {
// It is an order of magnitude faster to checkout the repo
// rather then stashing/unstashing all files to build Linux and Win
runScm()
build_jvm_linux(jvmNativeBuildType)
}
}
stage('build_jvm_windows') {
when { expression { shouldBuildJvmABIs() } }
agent {
node {
// FIXME aws-windows-02 has issue with checking out the repo with symlinks
label 'aws-windows-01'
}
}
steps {
runScm()
build_jvm_windows(jvmNativeBuildType)
}
}
}
}
stage('Build') {
steps {
runBuild()
}
}
stage('Static Analysis') {
when { expression { runTests } }
steps {
runStaticAnalysis()
}
}
stage('Benchmarks') {
steps {
runBenchmarks()
}
}
stage('Tests Compiler Plugin') {
when { expression { runTests } }
steps {
runCompilerPluginTest()
}
}
stage('Tests macOS - Unit Tests') {
when { expression { runTests } }
steps {
testAndCollect("packages", "cleanAllTests macosTest -PincludeTestModules=false")
}
}
stage('Tests Android - Unit Tests') {
when { expression { runTests } }
steps {
withLogcatTrace(
"unittest",
{
testAndCollect("packages", "cleanAllTests connectedAndroidTest -PincludeTestModules=false")
}
)
}
}
stage('Integration Tests - Android') {
when { expression { runTests } }
steps {
testWithServer([
{
withLogcatTrace(
"integrationtest",
{
forwardAdbPorts()
testAndCollect("packages", "cleanAllTests -PincludeSdkModules=false connectedAndroidTest")
}
)
}
])
}
}
stage('Integration Tests - macOS - New memory model') {
when { expression { runTests } }
steps {
testWithServer([
// This will overwrite previous test results, but should be ok as we would not get here
// if previous stages failed.
{
testAndCollect("packages", "cleanAllTests macosTest -PincludeSdkModules=false")
},
])
}
}
stage('Tests JVM') {
when { expression { runTests } }
steps {
testWithServer([
{
testAndCollect("packages", 'cleanAllTests jvmTest -PincludeSdkModules=false ')
}
])
}
}
stage('Integration Tests - iOS') {
when { expression { runTests } }
steps {
testWithServer([
{
testAndCollect("packages", "cleanAllTests iosTest -PincludeSdkModules=false")
}
])
}
}
stage('Minified Sync Tests - Android') {
when { expression { runTests } }
steps {
testWithServer([
{
testAndCollect("packages", 'cleanAllTests :test-sync:connectedAndroidtest -PincludeSdkModules=false -PtestBuildType=debugMinified')
}
])
sh 'rm mapping.zip || true'
zip([
'zipFile': 'mapping.zip',
'archive': true,
'glob': 'packages/test-sync/build/outputs/mapping/debugMinified/mapping.txt'
])
}
}
stage('Gradle Plugin Integration Tests') {
when { expression { runTests } }
steps {
testAndCollect("integration-tests/gradle-plugin-test", "integrationTest")
}
}
stage('Tests Android Sample App') {
when { expression { runTests } }
steps {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
runMonkey()
}
}
}
stage('Build Android on minimum versions') {
when { expression { runTests } }
steps {
runBuildMinAndroidApp()
}
}
stage('Test Realm Java Compatibility App') {
when { expression { runTests } }
steps {
testAndCollect("examples/realm-java-compatibility", "connectedAndroidTest")
}
}
stage('Track build metrics') {
when { expression { currentBranch == "main" } }
steps {
trackBuildMetrics(version)
}
}
stage('Publish SNAPSHOT to Maven Central') {
when { expression { shouldPublishSnapshot(version) } }
steps {
runPublishSnapshotToMavenCentral()
}
}
stage('Publish Release to Maven Central') {
when { expression { publishBuild } }
steps {
runPublishReleaseOnMavenCentral()
}
}
}
}
}
post {
failure {
notifySlackIfRequired("*The realm-kotlin/${currentBranch} branch is broken!*")
}
unstable {
notifySlackIfRequired("*The realm-kotlin/${currentBranch} branch is unstable!*")
}
fixed {
notifySlackIfRequired("*The realm-kotlin/${currentBranch} branch has been fixed!*")
}
}
}
def runScm() {
def repoExtensions = [
[$class: 'SubmoduleOption', recursiveSubmodules: true]
]
if (isReleaseBranch || forceWipeWorkspace) {
repoExtensions += [
[$class: 'WipeWorkspace'],
[$class: 'CleanCheckout'],
]
}
checkout([
$class : 'GitSCM',
branches : scm.branches,
gitTool : 'native git',
extensions : scm.extensions + repoExtensions,
userRemoteConfigs: scm.userRemoteConfigs
])
}
def setBuildDetails() {
// Check type of Build. We are treating this as a release build if we are building
// the exact Git SHA that was tagged.
gitTag = readGitTag()
version = sh(returnStdout: true, script: 'grep "const val version" buildSrc/src/main/kotlin/Config.kt | cut -d \\" -f2').trim()
echo "Git branch/tag: ${currentBranch}/${gitTag ?: 'none'}"
if (!gitTag) {
gitSha = sh(returnStdout: true, script: 'git rev-parse HEAD').trim().take(8)
echo "Building commit: ${version} - ${gitSha}"
setBuildName(gitSha)
publishBuild = false
} else {
if (gitTag != "v${version}") {
error "Git tag '${gitTag}' does not match v${version}"
} else {
echo "Building release: '${gitTag}'"
setBuildName("Tag ${gitTag}")
publishBuild = true
}
}
}
def genAndStashSwigJNI() {
withEnv(['PATH+USER_BIN=/usr/local/bin']) {
sh """
cd packages/jni-swig-stub
../gradlew assemble
"""
}
stash includes: 'packages/jni-swig-stub/build/generated/sources/jni/realmc.cpp,packages/jni-swig-stub/build/generated/sources/jni/realmc.h', name: 'swig_jni'
}
def runBuild() {
def buildJvmAbiFlag = "-PcopyJvmABIs=false"
if (shouldBuildJvmABIs()) {
unstash name: 'linux_so_file'
unstash name: 'win_dll'
buildJvmAbiFlag = "-PcopyJvmABIs=true"
}
withCredentials([
[$class: 'StringBinding', credentialsId: 'maven-central-kotlin-ring-file', variable: 'SIGN_KEY'],
[$class: 'StringBinding', credentialsId: 'maven-central-kotlin-ring-file-password', variable: 'SIGN_KEY_PASSWORD'],
]) {
withEnv(['PATH+USER_BIN=/usr/local/bin']) {
startEmulatorInBgIfNeeded()
def signingFlags = ""
if (isReleaseBranch) {
signingFlags = "-PsignBuild=true -PsignSecretRingFileKotlin=\"${env.SIGN_KEY}\" -PsignPasswordKotlin=${env.SIGN_KEY_PASSWORD}"
}
sh """
cd packages
chmod +x gradlew && ./gradlew publishAllPublicationsToTestRepository ${buildJvmAbiFlag} ${signingFlags} --info --stacktrace --no-daemon
"""
}
}
archiveArtifacts artifacts: 'packages/cinterop/src/jvmMain/resources/**/*.*', allowEmptyArchive: true
}
def runBenchmarks() {
// For now, just make sure that the benchmarks compile.
sh '''
cd benchmarks
chmod +x gradlew && ./gradlew assembleAndroidTest jmhCompileGeneratedClasses --stacktrace --no-daemon
'''
}
def runStaticAnalysis() {
try {
sh '''
./gradlew --no-daemon ktlintCheck detekt
'''
} finally {
// CheckStyle Publisher plugin is deprecated and does not support multiple Checkstyle files
// New Generation Warnings plugin throw a NullPointerException when used with recordIssues()
// As a work-around we just stash the output of Ktlint and Detekt for manual inspection.
sh '''
rm -rf /tmp/ktlint
rm -rf /tmp/detekt
mkdir /tmp/ktlint
mkdir /tmp/detekt
rsync -a --delete --ignore-errors examples/kmm-sample/androidApp/build/reports/ktlint/ /tmp/ktlint/example/ || true
rsync -a --delete --ignore-errors test/build/reports/ktlint/ /tmp/ktlint/test/ || true
rsync -a --delete --ignore-errors packages/library-base/build/reports/ktlint/ /tmp/ktlint/library-base/ || true
rsync -a --delete --ignore-errors packages/library-sync/build/reports/ktlint/ /tmp/ktlint/library-sync/ || true
rsync -a --delete --ignore-errors packages/plugin-compiler/build/reports/ktlint/ /tmp/ktlint/plugin-compiler/ || true
rsync -a --delete --ignore-errors packages/gradle-plugin/build/reports/ktlint/ /tmp/ktlint/plugin-gradle/ || true
rsync -a --delete --ignore-errors packages/runtime-api/build/reports/ktlint/ /tmp/ktlint/runtime-api/ || true
rsync -a --delete --ignore-errors examples/kmm-sample/androidApp/build/reports/detekt/ /tmp/detekt/example/ || true
rsync -a --delete --ignore-errors test/build/reports/detekt/ /tmp/detekt/test/ || true
rsync -a --delete --ignore-errors packages/library-base/build/reports/detekt/ /tmp/detekt/library-base/ || true
rsync -a --delete --ignore-errors packages/library-sync/build/reports/detekt/ /tmp/detekt/library-sync/ || true
rsync -a --delete --ignore-errors packages/plugin-compiler/build/reports/detekt/ /tmp/detekt/plugin-compiler/ || true
rsync -a --delete --ignore-errors packages/gradle-plugin/build/reports/detekt/ /tmp/detekt/plugin-gradle/ || true
rsync -a --delete --ignore-errors packages/runtime-api/build/reports/detekt/ /tmp/detekt/runtime-api/ || true
'''
sh 'rm ktlint.zip || true'
zip([
'zipFile': 'ktlint.zip',
'archive': true,
'dir' : '/tmp/ktlint'
])
sh 'rm detekt.zip || true'
zip([
'zipFile': 'detekt.zip',
'archive': true,
'dir' : '/tmp/detekt'
])
}
}
def runPublishSnapshotToMavenCentral() {
withEnv(['PATH+USER_BIN=/usr/local/bin']) {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'maven-central-credentials', passwordVariable: 'MAVEN_CENTRAL_PASSWORD', usernameVariable: 'MAVEN_CENTRAL_USER']]) {
sh """
cd packages
./gradlew publishToSonatype -PossrhUsername=${env.MAVEN_CENTRAL_USER} -PossrhPassword=${env.MAVEN_CENTRAL_PASSWORD} --no-daemon
"""
}
}
}
def runPublishReleaseOnMavenCentral() {
withCredentials([
[$class: 'StringBinding', credentialsId: 'maven-central-kotlin-ring-file', variable: 'SIGN_KEY'],
[$class: 'StringBinding', credentialsId: 'maven-central-kotlin-ring-file-password', variable: 'SIGN_KEY_PASSWORD'],
[$class: 'StringBinding', credentialsId: 'slack-webhook-java-ci-channel', variable: 'SLACK_URL_CI'],
[$class: 'StringBinding', credentialsId: 'slack-webhook-releases-channel', variable: 'SLACK_URL_RELEASE'],
[$class: 'StringBinding', credentialsId: 'gradle-plugin-portal-key', variable: 'GRADLE_PORTAL_KEY'],
[$class: 'StringBinding', credentialsId: 'gradle-plugin-portal-secret', variable: 'GRADLE_PORTAL_SECRET'],
[$class: 'UsernamePasswordMultiBinding', credentialsId: 'maven-central-credentials', passwordVariable: 'MAVEN_CENTRAL_PASSWORD', usernameVariable: 'MAVEN_CENTRAL_USER'],
[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'DOCS_S3_ACCESS_KEY', credentialsId: 'mongodb-realm-docs-s3', secretKeyVariable: 'DOCS_S3_SECRET_KEY'],
[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'REALM_S3_ACCESS_KEY', credentialsId: 'tightdb-s3-ci', secretKeyVariable: 'REALM_S3_SECRET_KEY']
]) {
sh """
set +x
sh tools/publish_release.sh '$MAVEN_CENTRAL_USER' '$MAVEN_CENTRAL_PASSWORD' \
'$REALM_S3_ACCESS_KEY' '$REALM_S3_SECRET_KEY' \
'$DOCS_S3_ACCESS_KEY' '$DOCS_S3_SECRET_KEY' \
'$SLACK_URL_RELEASE' '$SLACK_URL_CI' \
'$GRADLE_PORTAL_KEY' '$GRADLE_PORTAL_SECRET' \
'-PsignBuild=true -PsignSecretRingFileKotlin="$SIGN_KEY" -PsignPasswordKotlin=$SIGN_KEY_PASSWORD'
"""
}
}
def runCompilerPluginTest() {
withEnv(['PATH+USER_BIN=/usr/local/bin']) {
sh """
cd packages
./gradlew --no-daemon :plugin-compiler:test --info --stacktrace
"""
// See https://stackoverflow.com/a/51206394/1389357
script {
def testResults = findFiles(glob: "packages/plugin-compiler/build/**/TEST-*.xml")
for(xml in testResults) {
touch xml.getPath()
}
}
step([ $class: 'JUnitResultArchiver', allowEmptyResults: true, testResults: "packages/plugin-compiler/build/**/TEST-*.xml"])
}
}
def testWithServer(tasks) {
// Work-around for https://github.com/docker/docker-credential-helpers/issues/82
withCredentials([
[$class: 'StringBinding', credentialsId: 'realm-kotlin-ci-password', variable: 'PASSWORD'],
[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'realm-kotlin-baas-aws-credentials', accessKeyVariable: 'BAAS_AWS_ACCESS_KEY_ID', secretKeyVariable: 'BAAS_AWS_SECRET_ACCESS_KEY']
]) {
sh "security -v unlock-keychain -p $PASSWORD"
try {
// Prepare Docker containers with MongoDB Realm Test Server infrastructure for
// integration tests.
// TODO: How much of this logic can be moved to start_server.sh for shared logic with local testing.
def props = readProperties file: 'dependencies.list'
echo "Version in dependencies.list: ${props.MONGODB_REALM_SERVER}"
def mdbRealmImage = docker.image("docker.pkg.github.com/realm/ci/mongodb-realm-test-server:${props.MONGODB_REALM_SERVER}")
docker.withRegistry('https://docker.pkg.github.com', 'github-packages-token') {
mdbRealmImage.pull()
}
mongoDbRealmContainer = mdbRealmImage.run("--rm -i -t -d -p9090:9090 -p26000:26000 -e AWS_ACCESS_KEY_ID='$BAAS_AWS_ACCESS_KEY_ID' -e AWS_SECRET_ACCESS_KEY='$BAAS_AWS_SECRET_ACCESS_KEY'")
// Technically, this is only needed for Android, but since all tests are
// executed on same host and tasks are grouped in same stage we just do it
// here
forwardAdbPorts()
tasks.each { task ->
task()
}
} finally {
// We assume that creating these containers and the docker network can be considered an atomic operation.
if (mongoDbRealmContainer != null) {
try {
archiveServerLogs(mongoDbRealmContainer.id)
} finally {
mongoDbRealmContainer.stop()
}
}
}
}
}
def withLogcatTrace(name, task) {
try {
backgroundPid = startLogCatCollector(name)
task()
} finally {
stopLogCatCollector(backgroundPid, name)
}
}
String startLogCatCollector(name) {
// Cancel build quickly if no device is available. The lock acquired already should
// ensure we have access to a device. If not, it is most likely a more severe problem.
timeout(time: 1, unit: 'MINUTES') {
// Need ADB as root to clear all buffers: https://stackoverflow.com/a/47686978/1389357
sh '$ANDROID_SDK_ROOT/platform-tools/adb devices'
sh """
$ANDROID_SDK_ROOT/platform-tools/adb root
$ANDROID_SDK_ROOT/platform-tools/adb logcat -b all -c
$ANDROID_SDK_ROOT/platform-tools/adb logcat -v time > 'logcat-${name}.txt' &
echo \$! > pid
"""
return readFile("pid").trim()
}
}
def stopLogCatCollector(String backgroundPid, name) {
// The pid might not be available if the build was terminated early or stopped due to
// a build error.
if (backgroundPid != null) {
sh "kill ${backgroundPid}"
// Zip file generation will fail if the file is already there
// Pipeline Utility Steps Plugin 2.6.1 introduces 'overwrite' property
// https://issues.jenkins.io/browse/JENKINS-42591
sh "rm -f logcat-${name}.zip"
zip([
'zipFile': "logcat-${name}.zip",
'archive': true,
'glob' : "logcat-${name}.txt"
])
sh "rm logcat-${name}.txt"
}
}
def forwardAdbPorts() {
sh """
$ANDROID_SDK_ROOT/platform-tools/adb reverse tcp:9080 tcp:9080
$ANDROID_SDK_ROOT/platform-tools/adb reverse tcp:9443 tcp:9443
$ANDROID_SDK_ROOT/platform-tools/adb reverse tcp:9090 tcp:9090
"""
}
def testAndCollect(dir, tasks) {
withEnv(['PATH+USER_BIN=/usr/local/bin']) {
try {
sh """
pushd $dir
./gradlew $tasks --info --stacktrace --no-daemon
popd
"""
} finally {
step([$class: 'JUnitResultArchiver', allowEmptyResults: true, testResults: "$dir/**/build/**/TEST-*.xml"])
}
}
}
def runMonkey() {
try {
withEnv(['PATH+USER_BIN=/usr/local/bin']) {
sh """
cd examples/kmm-sample
./gradlew uninstallAll installRelease --stacktrace --no-daemon
$ANDROID_SDK_ROOT/platform-tools/adb shell monkey --pct-syskeys 0 -p io.realm.example.kmmsample.androidApp -v 500 --kill-process-after-error
"""
}
} catch (err) {
currentBuild.result = 'FAILURE'
currentBuild.stageResult = 'FAILURE'
}
}
def runBuildMinAndroidApp() {
try {
sh """
cd examples/min-android-sample
java -version
./gradlew assembleDebug jvmJar --stacktrace --no-daemon
"""
} catch (err) {
currentBuild.result = 'FAILURE'
currentBuild.stageResult = 'FAILURE'
}
}
def notifySlackIfRequired(String slackMessage) {
// We should only generate Slack notifications for important branches that all team members use.
if (slackNotificationBranches.contains(currentBranch)) {
node {
withCredentials([[$class: 'StringBinding', credentialsId: 'slack-webhook-java-ci-channel', variable: 'SLACK_URL']]) {
def payload = JsonOutput.toJson([
username: "Realm CI",
icon_emoji: ":realm_new:",
text: "${slackMessage}\n<${env.BUILD_URL}|Click here> to check the build."
])
sh "curl -X POST --data-urlencode \'payload=${payload}\' ${env.SLACK_URL}"
}
}
}
}
def readGitTag() {
def command = 'git describe --exact-match --tags HEAD'
def returnStatus = sh(returnStatus: true, script: command)
if (returnStatus != 0) {
return null
}
return sh(returnStdout: true, script: command).trim()
}
def startEmulatorInBgIfNeeded() {
def command = '$ANDROID_SDK_ROOT/platform-tools/adb shell pidof com.android.phone'
def returnStatus = sh(returnStatus: true, script: command)
if (returnStatus != 0) {
// Changing the name of the emulator image requires that this emulator image is
// present on both atlanta_host13 and atlanta_host14.
sh '/usr/local/Cellar/daemonize/1.7.8/sbin/daemonize -E JENKINS_NODE_COOKIE=dontKillMe $ANDROID_SDK_ROOT/emulator/emulator -avd Pixel_2_API_30_x86_64 -no-boot-anim -no-window -wipe-data -noaudio -partition-size 4098 -memory 2048'
}
}
boolean shouldPublishSnapshot(version) {
if (!releaseBranches.contains(currentBranch)) {
return false
}
if (version == null || !version.endsWith("-SNAPSHOT")) {
return false
}
return true
}
def archiveServerLogs(String mongoDbRealmContainerId) {
sh "docker cp ${mongoDbRealmContainerId}:/var/log/stitch.log ./stitch.log"
sh 'rm stitchlog.zip || true'
zip([
'zipFile': 'stitchlog.zip',
'archive': true,
'glob': 'stitch.log'
])
sh 'rm stitch.log'
sh "docker cp ${mongoDbRealmContainerId}:/var/log/mongodb.log ./mongodb.log"
sh 'rm mongodb.zip || true'
zip([
'zipFile': 'mongodb.zip',
'archive': true,
'glob': 'mongodb.log'
])
sh 'rm mongodb.log'
}
def runCommand(String command){
return sh(script: command, returnStdout: true).trim()
}
def shouldBuildJvmABIs() {
if (publishBuild || shouldPublishSnapshot(version)) return true else return false
}
def build_jvm_linux(String buildType) {
// Any change to CMAKE properties here, should be reflected in /packages/cinterop/build.gradle.kts
// In the `buildSharedLibrariesForJVM` task as well as `build_jvm_windows` in this JenkinsFile.
unstash name: 'swig_jni'
docker.build('jvm_linux', '-f packages/cinterop/src/jvmMain/generic.Dockerfile .').inside {
sh """
cd packages/cinterop/src/jvmMain/
rm -rf linux-build-dir
mkdir linux-build-dir
cd linux-build-dir
cmake -DCMAKE_BUILD_TYPE=${buildType} \
-DREALM_ENABLE_SYNC=1 \
-DREALM_NO_TESTS=1 \
-DREALM_BUILD_LIB_ONLY=true \
../../jvm
make -j8
"""
stash includes:'packages/cinterop/src/jvmMain/linux-build-dir/librealmc.so', name: 'linux_so_file'
}
}
def build_jvm_windows(String buildType) {
// Any change to CMAKE properties here, should be reflected in /packages/cinterop/build.gradle.kts
// In the `buildSharedLibrariesForJVM` task as well as `build_jvm_linux` in this JenkinsFile.
unstash name: 'swig_jni'
def cmakeOptions = [
CMAKE_GENERATOR_PLATFORM: 'x64',
CMAKE_BUILD_TYPE: "${buildType}",
REALM_ENABLE_SYNC: "ON",
CMAKE_TOOLCHAIN_FILE: "c:\\src\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake",
CMAKE_SYSTEM_VERSION: '8.1',
REALM_NO_TESTS: '1',
REALM_BUILD_LIB_ONLY: "true",
VCPKG_TARGET_TRIPLET: 'x64-windows-static'
]
def cmakeDefinitions = cmakeOptions.collect { k,v -> "-D$k=$v" }.join(' ')
dir('packages') {
bat "cd cinterop\\src\\jvmMain && rmdir /s /q windows-build-dir & mkdir windows-build-dir && cd windows-build-dir && \"${tool 'cmake'}\" ${cmakeDefinitions} ..\\..\\jvm && \"${tool 'cmake'}\" --build . --config Release"
}
stash includes: 'packages/cinterop/src/jvmMain/windows-build-dir/Release/realmc.dll', name: 'win_dll'
}
def trackBuildMetrics(version) {
withCredentials([[$class: 'StringBinding', credentialsId: 'kotlin-build-metrics-url', variable: 'METRICS_URL']]) {
sh """
sh ./tools/collect_metrics.sh '${version}' result.json
curl --location --request POST '${METRICS_URL}' \
--header 'Content-Type: application/json' \
--data-binary @result.json
"""
}
}