forked from realm/realm-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
164 lines (144 loc) · 6.12 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
#!groovy
import groovy.json.JsonOutput
def buildSuccess = false
try {
node('android') {
// Allocate a custom workspace to avoid having % in the path (it breaks ld)
ws('/tmp/realm-java') {
stage 'SCM'
checkout scm
// Make sure not to delete the folder that Jenkins allocates to store scripts
sh 'git clean -ffdx -e .????????'
// Update submodule for object-store
sh 'git submodule sync'
sh 'git submodule update --init --force'
stage 'Docker build'
def buildEnv = docker.build 'realm-java:snapshot'
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") {
stage 'JVM tests'
try {
gradle 'assemble check javadoc'
} 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'
boolean archiveLog = true
String backgroundPid
try {
backgroundPid = startLogCatCollector()
gradle('realm', 'connectedUnitTests')
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"
}
}
}
}
}
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 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 sendMetrics(String metric, String value) {
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 '${metric} value=${value}i' --user '${env.influx_user}:${env.influx_pass}'"
}
}
def sendTaggedMetric(String metric, String value, String tagName, String tagValue) {
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 '${metric},${tagName}=${tagValue} value=${value}i' --user '${env.influx_user}:${env.influx_pass}'"
}
}
def storeJunitResults(String path) {
step([
$class: 'JUnitResultArchiver',
testResults: path
])
}
def collectAarMetrics() {
sh '''set -xe
cd realm/realm-library/build/outputs/aar
unzip realm-android-library-release.aar -d unzipped
find $ANDROID_HOME -name dx | sort -r | head -n 1 > dx
$(cat dx) --dex --output=temp.dex unzipped/classes.jar
cat temp.dex | head -c 92 | tail -c 4 | hexdump -e '1/4 "%d"' > methods
'''
sendMetrics('methods', readFile('realm/realm-library/build/outputs/aar/methods'))
def aarFile = findFiles(glob: 'realm/realm-library/build/outputs/aar/realm-android-library-release.aar')[0]
sendMetrics('aar_size', aarFile.length as String)
def soFiles = findFiles(glob: 'realm/realm-library/build/outputs/aar/unzipped/jni/*/librealm-jni.so')
for (int i = 0; i < soFiles.length; i++) {
def abiName = soFiles[i].path.tokenize('/')[-2]
def libSize = soFiles[i].length as String
sendTaggedMetric('abi_size', libSize, '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"
}