This repository has been archived by the owner on Mar 13, 2023. It is now read-only.
forked from haipham-aula/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
206 lines (174 loc) · 6.95 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
import groovy.json.JsonSlurperClassic
def runPipeline() {
try {
ansiColor('xterm') {
runStages();
}
} catch(err) {
echo "Error: ${err}"
currentBuild.result = "FAILED"
}
}
def pullDockerImage(imageName) {
def result = sh(script: "docker pull ${imageName}", returnStatus: true)
if (result != 0) {
throw new Exception("Failed to pull image[${imageName}]")
}
}
def buildDockerfile(dockerfilePath = "Dockerfile", imageName) {
def buildCmd = "docker build -f ${dockerfilePath} -t ${imageName} ."
echo "${buildCmd}"
def result = sh(script: buildCmd, returnStatus: true)
if (result != 0) {
throw new Exception("Failed to build image[${imageName}] from '${dockerfilePath}'")
}
}
def runCmdOnDockerImage(imageName, cmd, run_opts = '') {
def result = sh(script: "docker run ${run_opts} -i ${imageName} sh -c '${cmd}'", returnStatus: true)
if(result != 0) {
throw new Exception("Failed to run cmd[${cmd}] on image[${imageName}]")
}
}
def calculateGithubInfo() {
return [
branch: env.BRANCH_NAME,
sha: sh(returnStdout: true, script: 'git rev-parse HEAD').trim(),
tag: null,
isPR: "${env.CHANGE_URL}".contains('/pull/')
]
}
def getParallelInstrumentationTests(testDir, parallelCount, imageName) {
def integrationTests = [:]
def testCount = sh(script: "ls ${testDir} | wc -l", returnStdout: true).trim().toInteger()
def testPerParallel = testCount.intdiv(parallelCount) + 1
def ignoredTests = 'CatalystNativeJavaToJSReturnValuesTestCase|CatalystUIManagerTestCase|CatalystMeasureLayoutTest|CatalystNativeJavaToJSArgumentsTestCase|CatalystNativeJSToJavaParametersTestCase|ReactScrollViewTestCase|ReactHorizontalScrollViewTestCase|ViewRenderingTestCase';
for (def x = 0; (x*testPerParallel) < testCount; x++) {
def offset = x
integrationTests["android integration tests: ${offset}"] = {
run: {
runCmdOnDockerImage(imageName, "bash /app/ContainerShip/scripts/run-android-docker-instrumentation-tests.sh --offset=${offset} --count=${testPerParallel} --ignore=\"${ignoredTests}\"", '--privileged --rm')
}
}
}
return integrationTests
}
def runStages() {
def buildInfo = [
image: [
name: "facebook/react-native",
tag: null
],
scm: [
branch: null,
sha: null,
tag: null,
isPR: false
]
]
node {
def jsDockerBuild, androidDockerBuild
def jsTag, androidTag, jsImageName, androidImageName, parallelInstrumentationTests
try {
stage('Setup') {
parallel(
'pull images': {
pullDockerImage('containership/android-base:latest')
}
)
}
stage('Build') {
checkout scm
def githubInfo = calculateGithubInfo()
buildInfo.scm.branch = githubInfo.branch
buildInfo.scm.sha = githubInfo.sha
buildInfo.scm.tag = githubInfo.tag
buildInfo.scm.isPR = githubInfo.isPR
buildInfo.image.tag = "${buildInfo.scm.sha}-${env.BUILD_TAG.replace(" ", "-").replace("/", "-").replace("%2F", "-")}"
jsTag = "${buildInfo.image.tag}"
androidTag = "${buildInfo.image.tag}"
jsImageName = "${buildInfo.image.name}-js:${jsTag}"
androidImageName = "${buildInfo.image.name}-android:${androidTag}"
parallelInstrumentationTests = getParallelInstrumentationTests('./ReactAndroid/src/androidTest/java/com/facebook/react/tests', 3, androidImageName)
parallel(
'javascript build': {
jsDockerBuild = docker.build("${jsImageName}", "-f ContainerShip/Dockerfile.javascript .")
},
'android build': {
androidDockerBuild = docker.build("${androidImageName}", "-f ContainerShip/Dockerfile.android .")
}
)
}
stage('Tests JS') {
try {
parallel(
'javascript flow': {
runCmdOnDockerImage(jsImageName, 'yarn run flow -- check', '--rm')
},
'javascript tests': {
runCmdOnDockerImage(jsImageName, 'yarn test --maxWorkers=4', '--rm')
},
'documentation tests': {
runCmdOnDockerImage(jsImageName, 'cd website && yarn test', '--rm')
},
'documentation generation': {
runCmdOnDockerImage(jsImageName, 'cd website && node ./server/generate.js', '--rm')
}
)
} catch(e) {
currentBuild.result = "FAILED"
echo "Test JS Stage Error: ${e}"
}
}
stage('Tests Android') {
try {
parallel(
'android unit tests': {
runCmdOnDockerImage(androidImageName, 'bash /app/ContainerShip/scripts/run-android-docker-unit-tests.sh', '--privileged --rm')
},
'android e2e tests': {
runCmdOnDockerImage(androidImageName, 'bash /app/ContainerShip/scripts/run-ci-e2e-tests.sh --android --js', '--privileged --rm')
}
)
} catch(e) {
currentBuild.result = "FAILED"
echo "Tests Android Stage Error: ${e}"
}
}
stage('Tests Android Instrumentation') {
// run all tests in parallel
try {
parallel(parallelInstrumentationTests)
} catch(e) {
currentBuild.result = "FAILED"
echo "Tests Android Instrumentation Stage Error: ${e}"
}
}
stage('Cleanup') {
cleanupImage(jsDockerBuild)
cleanupImage(androidDockerBuild)
}
} catch(err) {
cleanupImage(jsDockerBuild)
cleanupImage(androidDockerBuild)
throw err
}
}
}
def isMasterBranch() {
return env.GIT_BRANCH == 'master'
}
def gitCommit() {
return sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
}
def cleanupImage(image) {
if (image) {
try {
sh "docker ps -a | awk '{ print \$1,\$2 }' | grep ${image.id} | awk '{print \$1 }' | xargs -I {} docker rm {}"
sh "docker rmi -f ${image.id}"
} catch(e) {
echo "Error cleaning up ${image.id}"
echo "${e}"
}
}
}
runPipeline()