forked from realm/realm-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
483 lines (428 loc) · 16.2 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
#!groovy
@Library('realm-ci') _
configuration = 'Release'
def AndroidABIs = [ 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64' ]
def WindowsPlatforms = [ 'Win32', 'x64' ]
def WindowsUniversalPlatforms = [ 'Win32', 'x64', 'ARM' ]
String versionSuffix = ''
boolean enableLTO = true
stage('Checkout') {
rlmNode('docker') {
checkout([
$class: 'GitSCM',
branches: scm.branches,
gitTool: 'native git',
extensions: scm.extensions + [
[$class: 'CloneOption', depth: 0, shallow: true],
[$class: 'SubmoduleOption', recursiveSubmodules: true]
],
userRemoteConfigs: scm.userRemoteConfigs
])
if (shouldPublishPackage()) {
versionSuffix = "alpha.${env.BUILD_ID}"
}
else if (env.CHANGE_BRANCH == null || !env.CHANGE_BRANCH.startsWith('release')) {
versionSuffix = "PR-${env.CHANGE_ID}.${env.BUILD_ID}"
enableLTO = false
}
stash includes: '**', excludes: 'wrappers/**', name: 'dotnet-source', useDefaultExcludes: false
stash includes: 'wrappers/**', name: 'dotnet-wrappers-source'
}
}
stage('Build wrappers') {
def bashExtraArgs = ''
def psExtraArgs = ''
if (enableLTO) {
bashExtraArgs = '-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON'
psExtraArgs = '-EnableLTO'
}
def jobs = [
'iOS': {
rlmNode('osx || macos-catalina') {
unstash 'dotnet-wrappers-source'
dir('wrappers') {
sh "./build-ios.sh --configuration=${configuration} ${bashExtraArgs}"
}
stash includes: 'wrappers/build/**', name: 'ios-wrappers'
}
},
'macOS': {
rlmNode('osx || macos-catalina') {
unstash 'dotnet-wrappers-source'
dir('wrappers') {
sh "REALM_CMAKE_CONFIGURATION=${configuration} ./build-macos.sh -GXcode ${bashExtraArgs}"
}
stash includes: 'wrappers/build/**', name: 'macos-wrappers'
}
},
'Linux': {
rlmNode('docker') {
unstash 'dotnet-wrappers-source'
dir('wrappers') {
buildWrappersInDocker('wrappers', 'centos.Dockerfile', "REALM_CMAKE_CONFIGURATION=${configuration} ./build.sh ${bashExtraArgs}")
}
stash includes: 'wrappers/build/**', name: 'linux-wrappers'
}
}
]
for(abi in AndroidABIs) {
def localAbi = abi
jobs["Android ${localAbi}"] = {
rlmNode('docker') {
unstash 'dotnet-wrappers-source'
dir('wrappers') {
buildWrappersInDocker('wrappers_android', 'android.Dockerfile', "./build-android.sh --configuration=${configuration} --ARCH=${localAbi} ${bashExtraArgs}")
}
stash includes: 'wrappers/build/**', name: "android-wrappers-${localAbi}"
}
}
}
for(platform in WindowsPlatforms) {
def localPlatform = platform
jobs["Windows ${localPlatform}"] = {
rlmNode('windows') {
unstash 'dotnet-wrappers-source'
dir('wrappers') {
powershell ".\\build.ps1 Windows -Configuration ${configuration} -Platforms ${localPlatform} ${psExtraArgs}"
}
stash includes: 'wrappers/build/**', name: "windows-wrappers-${localPlatform}"
if (shouldPublishPackage()) {
archiveArtifacts 'wrappers/build/**/*.pdb'
}
}
}
}
for(platform in WindowsUniversalPlatforms) {
def localPlatform = platform
jobs["WindowsUniversal ${localPlatform}"] = {
rlmNode('windows') {
unstash 'dotnet-wrappers-source'
dir('wrappers') {
powershell ".\\build.ps1 WindowsStore -Configuration ${configuration} -Platforms ${localPlatform} ${psExtraArgs}"
}
stash includes: 'wrappers/build/**', name: "windowsuniversal-wrappers-${localPlatform}"
if (shouldPublishPackage()) {
archiveArtifacts 'wrappers/build/**/*.pdb'
}
}
}
}
parallel jobs
}
packageVersion = ''
stage('Package') {
rlmNode('windows && dotnet') {
unstash 'dotnet-source'
unstash 'ios-wrappers'
unstash 'macos-wrappers'
unstash 'linux-wrappers'
for(abi in AndroidABIs) {
unstash "android-wrappers-${abi}"
}
for(platform in WindowsPlatforms) {
unstash "windows-wrappers-${platform}"
}
for(platform in WindowsUniversalPlatforms) {
unstash "windowsuniversal-wrappers-${platform}"
}
dir('Realm') {
def props = [ Configuration: configuration, PackageOutputPath: "${env.WORKSPACE}/Realm/packages", VersionSuffix: versionSuffix]
dir('Realm.Fody') {
msbuild target: 'Pack', properties: props, restore: true
}
dir('Realm') {
msbuild target: 'Pack', properties: props, restore: true
}
recordIssues (
tool: msBuild(),
ignoreQualityGate: false,
ignoreFailedBuilds: true,
filters: [
excludeFile(".*/wrappers/.*"), // warnings produced by building the wrappers dll
excludeFile(".*zlib.lib.*"), // warning due to linking zlib without debug info
excludeFile(".*Microsoft.Build.Tasks.Git.targets.*") // warning due to sourcelink not finding objectstore
]
)
dir('packages') {
stash includes: '*.nupkg', name: 'packages'
archiveArtifacts '*.nupkg'
// extract the package version from the weaver package because it has the most definite name
def packages = findFiles(glob: 'Realm.Fody.*.nupkg')
packageVersion = getVersion(packages[0].name);
echo "Inferred version is ${packageVersion}"
if (shouldPublishPackage()) {
withCredentials([usernamePassword(credentialsId: 'github-packages-token', usernameVariable: 'GITHUB_USERNAME', passwordVariable: 'GITHUB_PASSWORD')]) {
echo "Publishing Realm.Fody.${packageVersion} to github packages"
bat "dotnet nuget add source https://nuget.pkg.github.com/realm/index.json -n github -u ${env.GITHUB_USERNAME} -p ${env.GITHUB_PASSWORD} & exit 0"
bat "dotnet nuget update source github -s https://nuget.pkg.github.com/realm/index.json -u ${env.GITHUB_USERNAME} -p ${env.GITHUB_PASSWORD} & exit 0"
bat "dotnet nuget push \"Realm.Fody.${packageVersion}.nupkg\" -s \"github\""
bat "dotnet nuget push \"Realm.${packageVersion}.nupkg\" -s \"github\""
}
}
}
}
}
}
stage('Unity Package') {
rlmNode('dotnet && macos') {
unstash 'dotnet-source'
unstash 'packages'
def packagePath = findFiles(glob: "Realm.${packageVersion}.nupkg")[0].path
sh "dotnet run --project Tools/SetupUnityPackage/SetupUnityPackage/ -- --path ${packagePath} --pack"
dir('Realm/Realm.Unity') {
archiveArtifacts "realm.unity-${packageVersion}.tgz"
sh "rm realm.unity-${packageVersion}.tgz"
}
sh "dotnet run --project Tools/SetupUnityPackage/SetupUnityPackage/ -- --path ${packagePath} --include-dependencies --pack"
dir('Realm/Realm.Unity') {
archiveArtifacts "*.tgz"
}
}
}
stage('Test') {
Map props = [ Configuration: configuration, UseRealmNupkgsWithVersion: packageVersion ]
def jobs = [
'Xamarin iOS': {
rlmNode('xamarin.ios') {
unstash 'dotnet-source'
dir('Realm/packages') { unstash 'packages' }
sh 'mkdir -p temp'
dir('Tests/Tests.iOS') {
msbuild restore: true,
properties: [ Platform: 'iPhoneSimulator', TargetFrameworkVersion: 'v1.0', RestoreConfigFile: "${env.WORKSPACE}/Tests/Test.NuGet.config" ] << props
dir("bin/iPhoneSimulator/${configuration}") {
runSimulator('Tests.iOS.app', 'io.realm.dotnettests', "--headless --resultpath ${env.WORKSPACE}/temp/TestResults.iOS.xml")
}
}
junit 'temp/TestResults.iOS.xml'
}
},
'Xamarin macOS': {
rlmNode('xamarin.mac') {
unstash 'dotnet-source'
dir('Realm/packages') { unstash 'packages' }
sh 'mkdir -p temp'
dir('Tests/Tests.XamarinMac') {
msbuild restore: true,
properties: [ RestoreConfigFile: "${env.WORKSPACE}/Tests/Test.NuGet.config", TargetFrameworkVersion: 'v2.0' ] << props
dir("bin/${configuration}/Tests.XamarinMac.app/Contents") {
sh "MacOS/Tests.XamarinMac --headless --labels=All --result=${env.WORKSPACE}/temp/TestResults.macOS.xml"
}
}
junit 'temp/TestResults.macOS.xml'
}
},
'Xamarin Android': {
rlmNode('windows && xamarin.android') {
unstash 'dotnet-source'
dir('Realm/packages') { unstash 'packages' }
dir('Tests/Tests.Android') {
msbuild target: 'SignAndroidPackage', restore: true,
properties: [ AndroidUseSharedRuntime: false, EmbedAssembliesIntoApk: true, RestoreConfigFile: "${env.WORKSPACE}/Tests/Test.NuGet.config" ] << props
dir("bin/${configuration}") {
stash includes: 'io.realm.xamarintests-Signed.apk', name: 'android-tests'
}
}
}
// The android tests fail on CI due to a CompilerServices.Unsafe issue. Uncomment when resolved
rlmNode('android-hub') {
unstash 'android-tests'
lock("${env.NODE_NAME}-android") {
boolean archiveLog = true
try {
// start logcat
sh '''
adb logcat -c
adb logcat -v time > "logcat.txt" &
echo $! > logcat.pid
'''
sh '''
adb uninstall io.realm.xamarintests
adb install io.realm.xamarintests-Signed.apk
adb shell pm grant io.realm.xamarintests android.permission.READ_EXTERNAL_STORAGE
adb shell pm grant io.realm.xamarintests android.permission.WRITE_EXTERNAL_STORAGE
'''
def instrumentationOutput = sh script: '''
adb shell am instrument -w -r io.realm.xamarintests/.TestRunner
adb pull /storage/sdcard0/RealmTests/TestResults.Android.xml TestResults.Android.xml
adb shell rm /sdcard/Realmtests/TestResults.Android.xml
''', returnStdout: true
def result = readProperties text: instrumentationOutput.trim().replaceAll(': ', '=')
if (result.INSTRUMENTATION_CODE != '-1') {
echo instrumentationOutput
error result.INSTRUMENTATION_RESULT
}
archiveLog = false
} finally {
// stop logcat
sh 'kill `cat logcat.pid`'
if (archiveLog) {
zip([
zipFile: 'android-logcat.zip',
archive: true,
glob: 'logcat.txt'
])
}
}
}
junit 'TestResults.Android.xml'
}
},
'.NET Framework Windows': {
rlmNode('windows && dotnet') {
unstash 'dotnet-source'
dir('Realm/packages') { unstash 'packages' }
dir('Tests/Realm.Tests') {
msbuild restore: true,
properties: [ RestoreConfigFile: "${env.WORKSPACE}/Tests/Test.NuGet.config", TargetFramework: 'net461' ] << props
dir("bin/${configuration}/net461") {
withEnv(["TMP=${env.WORKSPACE}\\temp"]) {
bat '''
mkdir "%TMP%"
Realm.Tests.exe --result=TestResults.Windows.xml --labels=After
'''
}
junit 'TestResults.Windows.xml'
}
}
}
},
'.NET Core macOS': NetCoreTest('macos && dotnet', 'netcoreapp3.1'),
'.NET Core Linux': NetCoreTest('docker', 'netcoreapp3.1'),
'.NET Core Windows': NetCoreTest('windows && dotnet', 'netcoreapp3.1'),
'.NET 5 macOS': NetCoreTest('macos && net5', 'net5.0'),
'.NET 5 Linux': NetCoreTest('docker', 'net5.0'),
'.NET 5 Windows': NetCoreTest('windows && dotnet', 'net5.0'),
'Weaver': {
rlmNode('dotnet && windows') {
unstash 'dotnet-source'
dir('Tests/Weaver/Realm.Fody.Tests') {
bat "dotnet run -f netcoreapp3.1 -c ${configuration} --result=TestResults.Weaver.xml --labels=After"
reportTests 'TestResults.Weaver.xml'
}
}
}
]
timeout(time: 30, unit: 'MINUTES') {
parallel jobs
}
}
def NetCoreTest(String nodeName, String targetFramework) {
return {
rlmNode(nodeName) {
unstash 'dotnet-source'
dir('Realm/packages') { unstash 'packages' }
def addNet5Framework = targetFramework == 'net5.0'
String script = """
cd ${env.WORKSPACE}/Tests/Realm.Tests
dotnet build -c ${configuration} -f ${targetFramework} -p:RestoreConfigFile=${env.WORKSPACE}/Tests/Test.NuGet.Config -p:UseRealmNupkgsWithVersion=${packageVersion} -p:AddNet5Framework=${addNet5Framework}
dotnet run -c ${configuration} -f ${targetFramework} --no-build -- --labels=After --result=${env.WORKSPACE}/TestResults.NetCore.xml
""".trim()
String appLocation = "${env.WORKSPACE}/Tests/TestApps/dotnet-integration-tests"
if (isUnix()) {
if (nodeName == 'docker') {
def test_runner_image = CreateDockerContainer(targetFramework)
withRealmCloud(version: '2020-12-04', appsToImport: ["dotnet-integration-tests": appLocation]) { networkName ->
test_runner_image.inside("--network=${networkName}") {
def appId = sh script: "cat ${appLocation}/app_id", returnStdout: true
script += " --baasurl http://mongodb-realm:9090 --baasappid ${appId.trim()}"
// see https://stackoverflow.com/a/53782505
sh """
export HOME=/tmp
${script}
"""
}
}
} else {
sh script
}
} else {
bat script
}
junit 'TestResults.NetCore.xml'
}
}
}
def msbuild(Map args = [:]) {
String invocation = "\"${tool 'msbuild'}\""
if ('project' in args) {
invocation += " ${args.project}"
}
if ('target' in args) {
invocation += " /t:${args.target}"
}
if ('properties' in args) {
for (property in mapToList(args.properties)) {
invocation += " /p:${property[0]}=\"${property[1]}\""
}
}
if (args['restore']) {
invocation += ' /restore'
}
if ('extraArguments' in args) {
invocation += " ${args.extraArguments}"
}
if (isUnix()) {
def env = [
"NUGET_PACKAGES=${env.HOME}/.nuget/packages-ci-${env.EXECUTOR_NUMBER}",
"NUGET_HTTP_CACHE_PATH=${env.HOME}/.nuget/v3-cache-ci-${env.EXECUTOR_NUMBER}"
]
withEnv(env) {
sh invocation
}
} else {
def env = [
"NUGET_PACKAGES=${env.userprofile}/.nuget/packages-ci-${env.EXECUTOR_NUMBER}",
"NUGET_HTTP_CACHE_PATH=${env.userprofile}/.nuget/v3-cache-ci-${env.EXECUTOR_NUMBER}"
]
withEnv(env) {
bat invocation
}
}
}
def reportTests(spec) {
xunit(
tools: [NUnit3(deleteOutputFiles: true, failIfNotNew: true, pattern: spec, skipNoTestFiles: false, stopProcessingIfError: true)],
thresholds: [ failed(unstableThreshold: '0') ]
)
}
def buildWrappersInDocker(String label, String image, String invocation) {
String uid = sh(script: 'id -u', returnStdout: true).trim()
String gid = sh(script: 'id -g', returnStdout: true).trim()
buildDockerEnv("ci/realm-dotnet:${label}", extra_args: "-f ${image}").inside("--mount 'type=bind,src=/tmp,dst=/tmp' -u ${uid}:${gid}") {
sh invocation
}
}
boolean shouldPublishPackage() {
return env.BRANCH_NAME == 'master'
}
def CreateDockerContainer(String targetFramework) {
def test_runner_image
switch(targetFramework) {
case 'netcoreapp3.1':
// In .netcore 3.1 using msbuild extras under linux fails because of missing Microsoft.WinFX.props. This is generated by a mismatching lower/upper case for a folder name.
// Renaming the folders in questions fixes the issue. This action is achieved through making a custom docker container. More info can be found at https://github.com/dotnet/sdk/issues/11108
test_runner_image = docker.build("netcoreapp3.1_custom", "./CustomNetcore31DockerImg")
break
case 'net5.0':
dockerImg = 'mcr.microsoft.com/dotnet/sdk:5.0'
test_runner_image = docker.image(dockerImg)
test_runner_image.pull()
break
default:
echo ".NET framework ${framework.ToString()} not supported by the pipeline, yet"
break
}
return test_runner_image
}
// Required due to JENKINS-27421
@NonCPS
List<List<?>> mapToList(Map map) {
return map.collect { it ->
[it.key, it.value]
}
}
@NonCPS
String getVersion(String name) {
return (name =~ /Realm.Fody.(.+).nupkg/)[0][1]
}