forked from realm/realm-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
240 lines (216 loc) · 9.07 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
#!groovy
import groovy.json.JsonOutput
def buildSuccess = false
def rosContainer
try {
node('android') {
timeout(time: 90, unit: 'MINUTES') {
// Allocate a custom workspace to avoid having % in the path (it breaks ld)
ws('/tmp/realm-java') {
stage('SCM') {
checkout([
$class: 'GitSCM',
branches: scm.branches,
gitTool: 'native git',
extensions: scm.extensions + [
[$class: 'CleanCheckout'],
[$class: 'SubmoduleOption', recursiveSubmodules: true]
],
userRemoteConfigs: scm.userRemoteConfigs
])
}
// Toggles for PR vs. Master builds.
// For PR's, we just build for arm-v7a and run unit tests for the ObjectServer variant
// A full build is done on `master`.
// TODO Once Android emulators are available on all nodes, we can switch to x86 builds
// on PR's for even more throughput.
def ABIs = ""
def instrumentationTestTarget = "connectedAndroidTest"
if (!['master'].contains(env.BRANCH_NAME)) {
ABIs = "armeabi-v7a"
instrumentationTestTarget = "connectedObjectServerDebugAndroidTest" // Run in debug more for better error reporting
}
def buildEnv
def rosEnv
stage('Docker build') {
// Docker image for build
buildEnv = docker.build 'realm-java:snapshot'
// Docker image for testing Realm Object Server
def dependProperties = readProperties file: 'dependencies.list'
def rosDeVersion = dependProperties["REALM_OBJECT_SERVER_DE_VERSION"]
rosEnv = docker.build 'ros:snapshot', "--build-arg ROS_DE_VERSION=${rosDeVersion} tools/sync_test_server"
}
rosContainer = rosEnv.run()
try {
buildEnv.inside("-e HOME=/tmp " +
"-e _JAVA_OPTIONS=-Duser.home=/tmp " +
"--privileged " +
"-v /dev/bus/usb:/dev/bus/usb " +
"-v ${env.HOME}/gradle-cache:/tmp/.gradle " +
"-v ${env.HOME}/.android:/tmp/.android " +
"-v ${env.HOME}/ccache:/tmp/.ccache " +
"-e REALM_CORE_DOWNLOAD_DIR=/tmp/.gradle " +
"--network container:${rosContainer.id}") {
stage('JVM tests') {
try {
withCredentials([[$class: 'FileBinding', credentialsId: 'c0cc8f9e-c3f1-4e22-b22f-6568392e26ae', variable: 'S3CFG']]) {
sh "chmod +x gradlew && ./gradlew assemble check javadoc -Ps3cfg=${env.S3CFG} -PbuildTargetABIs=${ABIs}"
}
} finally {
storeJunitResults 'realm/realm-annotations-processor/build/test-results/test/TEST-*.xml'
storeJunitResults 'examples/unitTestExample/build/test-results/**/TEST-*.xml'
step([$class: 'LintPublisher'])
}
}
stage('Static code analysis') {
try {
gradle('realm', 'findbugs pmd checkstyle')
} finally {
publishHTML(target: [allowMissing: false, alwaysLinkToLastBuild: false, keepAll: true, reportDir: 'realm/realm-library/build/findbugs', reportFiles: 'findbugs-output.html', reportName: 'Findbugs issues'])
publishHTML(target: [allowMissing: false, alwaysLinkToLastBuild: false, keepAll: true, reportDir: 'realm/realm-library/build/reports/pmd', reportFiles: 'pmd.html', reportName: 'PMD Issues'])
step([$class: 'CheckStylePublisher',
canComputeNew: false,
defaultEncoding: '',
healthy: '',
pattern: 'realm/realm-library/build/reports/checkstyle/checkstyle.xml',
unHealthy: ''
])
}
}
stage('Run instrumented tests') {
lock("${env.NODE_NAME}-android") {
boolean archiveLog = true
String backgroundPid
try {
backgroundPid = startLogCatCollector()
forwardAdbPorts()
gradle('realm', "${instrumentationTestTarget}")
archiveLog = false;
} finally {
stopLogCatCollector(backgroundPid, archiveLog)
storeJunitResults 'realm/realm-library/build/outputs/androidTest-results/connected/**/TEST-*.xml'
}
}
}
// TODO: add support for running monkey on the example apps
if (env.BRANCH_NAME == 'master') {
stage('Collect metrics') {
collectAarMetrics()
}
stage('Publish to OJO') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'bintray', passwordVariable: 'BINTRAY_KEY', usernameVariable: 'BINTRAY_USER']]) {
sh "chmod +x gradlew && ./gradlew -PbintrayUser=${env.BINTRAY_USER} -PbintrayKey=${env.BINTRAY_KEY} assemble ojoUpload --stacktrace"
}
}
}
}
} finally {
archiveRosLog(rosContainer.id)
sh "docker logs ${rosContainer.id}"
rosContainer.stop()
}
}
}
currentBuild.rawBuild.setResult(Result.SUCCESS)
buildSuccess = true
}
} catch(Exception e) {
currentBuild.rawBuild.setResult(Result.FAILURE)
buildSuccess = false
throw e
} finally {
if (['master', 'releases'].contains(env.BRANCH_NAME) && !buildSuccess) {
node {
withCredentials([[$class: 'StringBinding', credentialsId: 'slack-java-url', variable: 'SLACK_URL']]) {
def payload = JsonOutput.toJson([
username: 'Mr. Jenkins',
icon_emoji: ':jenkins:',
attachments: [[
'title': "The ${env.BRANCH_NAME} branch is broken!",
'text': "<${env.BUILD_URL}|Click here> to check the build.",
'color': "danger"
]]
])
sh "curl -X POST --data-urlencode \'payload=${payload}\' ${env.SLACK_URL}"
}
}
}
}
def forwardAdbPorts() {
sh ''' adb reverse tcp:9080 tcp:9080 && adb reverse tcp:9443 tcp:9443 &&
adb reverse tcp:8888 tcp:8888
'''
}
def String startLogCatCollector() {
sh '''adb logcat -c
adb logcat -v time > "logcat.txt" &
echo $! > pid
'''
return readFile("pid").trim()
}
def stopLogCatCollector(String backgroundPid, boolean archiveLog) {
sh "kill ${backgroundPid}"
if (archiveLog) {
zip([
'zipFile': 'logcat.zip',
'archive': true,
'glob' : 'logcat.txt'
])
}
sh 'rm logcat.txt'
}
def archiveRosLog(String id) {
sh "docker cp ${id}:/tmp/ros-testing-server.log ./ros.log"
zip([
'zipFile': 'roslog.zip',
'archive': true,
'glob' : 'ros.log'
])
sh 'rm ros.log'
}
def sendMetrics(String metricName, String metricValue, Map<String, String> tags) {
def tagsString = getTagsString(tags)
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '5b8ad2d9-61a4-43b5-b4df-b8ff6b1f16fa', passwordVariable: 'influx_pass', usernameVariable: 'influx_user']]) {
sh "curl -i -XPOST 'https://greatscott-pinheads-70.c.influxdb.com:8086/write?db=realm' --data-binary '${metricName},${tagsString} value=${metricValue}i' --user '${env.influx_user}:${env.influx_pass}'"
}
}
@NonCPS
def getTagsString(Map<String, String> tags) {
return tags.collect { k,v -> "$k=$v" }.join(',')
}
def storeJunitResults(String path) {
step([
$class: 'JUnitResultArchiver',
testResults: path
])
}
def collectAarMetrics() {
def flavors = ['base', 'objectServer']
for (def i = 0; i < flavors.size(); i++) {
def flavor = flavors[i]
sh """set -xe
cd realm/realm-library/build/outputs/aar
unzip realm-android-library-${flavor}-release.aar -d unzipped${flavor}
find \$ANDROID_HOME -name dx | sort -r | head -n 1 > dx
\$(cat dx) --dex --output=temp${flavor}.dex unzipped${flavor}/classes.jar
cat temp${flavor}.dex | head -c 92 | tail -c 4 | hexdump -e '1/4 \"%d\"' > methods${flavor}
"""
def methods = readFile("realm/realm-library/build/outputs/aar/methods${flavor}")
sendMetrics('methods', methods, ['flavor':flavor])
def aarFile = findFiles(glob: "realm/realm-library/build/outputs/aar/realm-android-library-${flavor}-release.aar")[0]
sendMetrics('aar_size', aarFile.length as String, ['flavor':flavor])
def soFiles = findFiles(glob: "realm/realm-library/build/outputs/aar/unzipped${flavor}/jni/*/librealm-jni.so")
for (def j = 0; j < soFiles.size(); j++) {
def soFile = soFiles[j]
def abiName = soFile.path.tokenize('/')[-2]
def libSize = soFile.length as String
sendMetrics('abi_size', libSize, ['flavor':flavor, 'type':abiName])
}
}
}
def gradle(String commands) {
sh "chmod +x gradlew && ./gradlew ${commands} --stacktrace"
}
def gradle(String relativePath, String commands) {
sh "cd ${relativePath} && chmod +x gradlew && ./gradlew ${commands} --stacktrace"
}