forked from kmadel/aws-device-farm-sample-app-for-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
203 lines (194 loc) · 8.76 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
import groovy.json.JsonSlurper
def uploadAppArn
def devicePoolArn
stage 'Acquire Build Node'
node('docker') {
//build Android app in Docker container
stage 'Build App'
//checkout AWS Device Farm Sample Android App from GitHub
git 'https://github.com/kmadel/aws-device-farm-sample-app-for-android.git'
//check for debug.keystore and create if doesn't exist
def keystoreExists = fileExists 'app/debug.keystore'
if(!keystoreExists) {
sh 'keytool -genkey -v -keystore app/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US"'
}
//tell docker to pull the android skd image,
//start that as a container,
//and then run the proceeding block of workflow steps
docker.image('kmadel/android-sdk:24.3.3').inside {
sh './gradlew assembleDebug assembleDebugAndroidTest'
}
//stash successful build
dir('app/build/outputs/apk/') {
stash includes: '*.apk', name: 'app-test-pkg'
}
}
//add checkpoint so if upload fails, don't need to rebuild
checkpoint 'successfully built app and test package'
//just need a generic linux node for aws steps
node('python-pip'){
stage 'Set Up Device Farm Project'
sh 'pwd'
//create a new Device Farm project if projectArn paramter is not specified
if(!projectArn) {
writeFile file: 'create-project.json', text: '{"name": "Jenkins Workflow AWS CLI Device Farm Demo"}'
wrap(
[$class: 'AmazonAwsCliBuildWrapper',
credentialsId: 'AWS-DEVICE-FARM-CREDS',
defaultRegion: 'us-west-2']) {
sh 'aws devicefarm create-project --cli-input-json file://create-project.json > createProjectOutput'
}
//get project arn from output
def createProjectOutput = readFile('createProjectOutput')
def jsonSlurper = new JsonSlurper()
def projectObj = jsonSlurper.parseText(createProjectOutput)
projectArn = projectObj.project.arn
//null jsonSluper to avoid thread issues
jsonSlurper = null
projectObj = null
}
//create device pool with just once device for demo purposes - Samsung Galaxy S6 (Verizon)
wrap([$class: 'AmazonAwsCliBuildWrapper',
credentialsId: 'AWS-DEVICE-FARM-CREDS',
defaultRegion: 'us-west-2']) {
sh """
aws devicefarm create-device-pool --project-arn ${projectArn} --name android-device-pool --rules '{"attribute": "ARN", "operator": "IN", "value": "[\\"arn:aws:devicefarm:us-west-2::device:9E515A6205C14AC0B6DCDBF3FC75BC3E\\"]"}' > createDevicePoolOutput
"""
}
//get project arn from output to list device pools
def createDevicePoolOutput = readFile('createDevicePoolOutput')
jsonSlurper = new JsonSlurper()
def devicePoolObj = jsonSlurper.parseText(createDevicePoolOutput)
devicePoolArn = devicePoolObj.devicePool.arn
//null jsonSluper to avoid thread issues
jsonSlurper = null
devicePoolObj = null
stage 'Create Uploads'
unstash 'app-test-pkg'
parallel(
uploadApp: {
wrap([$class: 'AmazonAwsCliBuildWrapper',
credentialsId: 'AWS-DEVICE-FARM-CREDS',
defaultRegion: 'us-west-2']) {
sh "aws devicefarm create-upload --project-arn ${projectArn} --name app-debug.apk --type ANDROID_APP > createUploadAppOutput"
}
//get project arn from output to list device pools
def createUploadAppOutput = readFile('createUploadAppOutput')
echo createUploadAppOutput
jsonSlurper = new JsonSlurper()
def createUploadAppObj = jsonSlurper.parseText(createUploadAppOutput)
//null jsonSluper to avoid thread issues
jsonSlurper = null
uploadAppArn = createUploadAppObj.upload.arn
uploadAppUrl = createUploadAppObj.upload.url
createUploadAppObj = null
//upload app with s3 from createUpload
sh "curl -T app-debug.apk '${uploadAppUrl}'"
waitUntil {
//wait until upload is complete
wrap([$class: 'AmazonAwsCliBuildWrapper',
credentialsId: 'AWS-DEVICE-FARM-CREDS',
defaultRegion: 'us-west-2']) {
sh "aws devicefarm get-upload --arn ${uploadAppArn} > getUploadAppOutput"
}
def getUploadAppOutput = readFile('getUploadAppOutput')
echo getUploadAppOutput
jsonSlurper = new JsonSlurper()
def getUploadAppObj = jsonSlurper.parseText(getUploadAppOutput)
def uploadStatus = getUploadAppObj.upload.status
//null jsonSluper to avoid thread issues
jsonSlurper = null
getUploadAppObj = null
if(uploadStatus == 'FAILED') {
error 'Upload App Failed'
}
uploadStatus == 'SUCCEEDED'
}
}, uploadTests: {
wrap([$class: 'AmazonAwsCliBuildWrapper',
credentialsId: 'AWS-DEVICE-FARM-CREDS',
defaultRegion: 'us-west-2']) {
sh "aws devicefarm create-upload --project-arn ${projectArn} --name app-debug-androidTest-unaligned.apk --type INSTRUMENTATION_TEST_PACKAGE > createUploadTestOutput"
}
//get project arn from output to list device pools
def createUploadTestOutput = readFile('createUploadTestOutput')
echo createUploadTestOutput
jsonSlurper = new JsonSlurper()
def createUploadTestObj = jsonSlurper.parseText(createUploadTestOutput)
//null jsonSluper to avoid thread issues
jsonSlurper = null
uploadTestArn = createUploadTestObj.upload.arn
uploadTestUrl = createUploadTestObj.upload.url
createUploadTestObj = null
//upload app with s3 from createUpload
sh "curl -T app-debug-androidTest-unaligned.apk '${uploadTestUrl}'"
waitUntil {
//wait until upload is complete
wrap([$class: 'AmazonAwsCliBuildWrapper',
credentialsId: 'AWS-DEVICE-FARM-CREDS',
defaultRegion: 'us-west-2']) {
sh "aws devicefarm get-upload --arn ${uploadTestArn} > getUploadTestOutput"
}
def getUploadTestOutput = readFile('getUploadAppOutput')
echo getUploadTestOutput
jsonSlurper = new JsonSlurper()
def getUploadTestObj = jsonSlurper.parseText(getUploadTestOutput)
uploadStatus = getUploadTestObj.upload.status
//null jsonSluper to avoid thread issues
jsonSlurper = null
getUploadTestObj = null
if(uploadStatus == 'FAILED') {
error 'Upload Test Failed'
}
uploadStatus == 'SUCCEEDED'
}
}, failFast: true
)
stage 'Schedule Test Run'
wrap(
[$class: 'AmazonAwsCliBuildWrapper',
credentialsId: 'AWS-DEVICE-FARM-CREDS',
defaultRegion: 'us-west-2']) {
sh "aws devicefarm schedule-run --project-arn ${projectArn} --app-arn ${uploadAppArn} --device-pool-arn ${devicePoolArn} --test type=INSTRUMENTATION,testPackageArn=${uploadTestArn} > scheduleRunOutput"
}
stash includes: 'scheduleRunOutput', name: 'scheduleRunOutput'
}
checkpoint 'successfully uploaded app and test pkg'
stage 'Get Test Results'
//instrumentation jobs take a while, so sleep job before polling for run results
sleep time: 14, unit: 'MINUTES'
//just need a generic linux node for aws steps
node('python-pip'){
unstash 'scheduleRunOutput'
def scheduleRunOutput = readFile('scheduleRunOutput')
echo scheduleRunOutput
jsonSlurper = new JsonSlurper()
def scheduleRunObj = jsonSlurper.parseText(scheduleRunOutput)
//null jsonSluper to avoid thread issues
jsonSlurper = null
runArn = scheduleRunObj.run.arn
scheduleRunObj = null
def getRunOutput
def runResult
waitUntil {
//wait until upload is complete
wrap([$class: 'AmazonAwsCliBuildWrapper',
credentialsId: 'AWS-DEVICE-FARM-CREDS',
defaultRegion: 'us-west-2']) {
sh "aws devicefarm get-run --arn ${runArn} > getRunOutput"
}
getRunOutput = readFile('getRunOutput')
jsonSlurper = new JsonSlurper()
def getRunObj = jsonSlurper.parseText(getRunOutput)
runStatus = getRunObj.run.status
runResult = getRunObj.run.result
//null jsonSluper to avoid thread issues
jsonSlurper = null
getRunObj = null
runStatus == 'COMPLETED'
}
echo getRunOutput
if(runResult != 'PASSED') {
error "Test Run ${runResult} - see output above for details"
}
}