forked from google/ground-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudbuild.yaml
255 lines (225 loc) · 9.03 KB
/
cloudbuild.yaml
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
#
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Loads the Docker image from the container registry and then runs the following steps:
# Workflow:
# --------
#
# Copy cache --> Extract tar --> -------> Code quality -------> Unit tests ------------------------>
# \ / \
# > Build debug apks Save reports to GCS --> Update CI status --> Compress gradle cache --> Save gradle cache to GCS
# / \ /
# Fetch google-services.json --> (master only)-----> Authorize Gcloud --> Instrumented tests ------>
#
# Notes:
# ------
# ${_PUSH_TO_MASTER} is true when the commit is pushed to master branch
# i.e. it happens after GitHub merges an approved PR with master.
steps:
- name: 'gcr.io/cloud-builders/gsutil'
id: ©_build_cache 'Copying build cache'
waitFor: [ '-' ]
entrypoint: 'bash'
args:
- '-c'
- |
#### Calculate md5sum and save to a file.
# 1. List all files except hidden dirs.
# 2. Filter by filename ending with .gradle
# 3. Convert continuous spaces into a single space
# 4. Pick 5th column containing file size. This causes a collision if only a number is flipped (eg "v2.4" -> "v2.5"). Look for a better alternative.
# 5. Calculate md5sum (for mac, use md5)
# 6. Strip ending " -" if present
ls -lR | grep -e "\.gradle$" | tr -s " " | cut -d " " -f5 | md5sum | sed 's/ -//' > hashsum
echo "Hashsum: " $(< hashsum)
gsutil cp "gs://${_CACHE_BUCKET}/$(< hashsum)-cache.tgz" ./ || echo "No cache found."
- name: 'gcr.io/$PROJECT_ID/android:base'
id: &extract_build_cache 'Extracting tar'
waitFor:
- *copy_build_cache
args:
- '-c'
- |
tar zxf $(< hashsum)-cache.tgz || echo "No cache found."
- name: 'gcr.io/$PROJECT_ID/android:30'
id: &config_google_services 'Load debug google-services.json'
waitFor: [ '-' ]
entrypoint: 'bash'
args:
- '-c'
- |
gcloud secrets versions access latest --secret=$_GOOGLE_SERVICES_JSON --format='get(payload.data)' | tr '_-' '/+' | base64 -d > ground/google-services.json
- name: 'gcr.io/$PROJECT_ID/android:30'
id: &assemble_debug 'Assemble debug apks'
waitFor:
- *extract_build_cache
- *config_google_services
args:
- '-c'
- |
./gradlew -PdisablePreDex assembleStaging assembleStagingUnitTest -PtestBuildType=staging
# TODO(#1547): Re-enable once instrumentation tests are fixed.
# if [[ "${_PUSH_TO_MASTER}" ]]; then
# ./gradlew -PdisablePreDex assembleStagingAndroidTest -PtestBuildType=staging
# fi
# Run code quality checks
- name: 'gcr.io/$PROJECT_ID/android:30'
id: &code_quality 'Run code quality checks'
waitFor:
- *assemble_debug
args:
- '-c'
- |
./gradlew -PdisablePreDex checkCode 2> check-logs.txt || echo "fail" > build-status.txt
cat check-logs.txt
if [[ "${_PUSH_TO_MASTER}" ]]; then
./gradlew -PdisablePreDex dependencyUpdates
fi
- name: 'gcr.io/$PROJECT_ID/android:30'
id: &unit_tests 'Run unit tests'
waitFor:
- *code_quality
args:
- '-c'
- |
./gradlew -PdisablePreDex testStagingUnitTest 2> unit-test-logs.txt || echo "fail" > build-status.txt
cat unit-test-logs.txt
# TODO: Add a check for _PUSH_TO_MASTER
./gradlew jacocoTestStagingUnitTestReport
- name: 'gcr.io/$PROJECT_ID/android:30'
id: &authenticate_gcloud 'Authorize gcloud'
waitFor:
- *assemble_debug
entrypoint: 'bash'
args:
- '-c'
- |
gcloud secrets versions access latest --secret=$_SECRET_NAME --format='get(payload.data)' | tr '_-' '/+' | base64 -d > client-secret.json
gcloud auth activate-service-account --key-file client-secret.json
# For more details: https://cloud.google.com/sdk/gcloud/reference/alpha/firebase/test/android/run
- name: 'gcr.io/$PROJECT_ID/android:30'
id: &instrumented_tests 'Run instrumented tests'
waitFor:
- *authenticate_gcloud
entrypoint: 'bash'
args:
- '-c'
- |
echo "Skipping"
# TODO(#1547): Re-enable once instrumentation tests are fixed.
# if [[ "${_PUSH_TO_MASTER}" ]]; then
# gcloud --quiet beta firebase test android run \
# --type instrumentation --num-uniform-shards=1 \
# --app ground/build/outputs/apk/staging/*.apk \
# --test ground/build/outputs/apk/androidTest/staging/*.apk \
# --device model=Pixel2,version=30,locale=en,orientation=portrait \
# --results-bucket ${_ARTIFACT_BUCKET} \
# --results-dir "$BRANCH_NAME-$BUILD_ID/reports" \
# --environment-variables coverage=true,coverageFile=/sdcard/coverage.ec \
# --directories-to-pull /sdcard \
# --timeout 20m
# else
# echo "Skipping"
# fi
- name: 'gcr.io/cloud-builders/gsutil'
id: &save_reports 'Save reports to GCS'
waitFor:
- *unit_tests
- *instrumented_tests
args: [ 'cp', '-r',
'ground/build/reports',
'gs://${_ARTIFACT_BUCKET}/$BRANCH_NAME-$BUILD_ID/reports' ]
# Update status badge and fail build if errors were found in "build" step
- name: 'gcr.io/cloud-builders/gsutil'
id: &update_status 'Update build status'
waitFor:
- *save_reports
entrypoint: 'bash'
args:
- '-c'
- |
# Display artifact bucket link
echo "Reports uploaded to https://console.cloud.google.com/storage/browser/${_ARTIFACT_BUCKET}/$BRANCH_NAME-$BUILD_ID/"
# Update build status if running on master branch
if [[ "${_PUSH_TO_MASTER}" ]]; then
# Gradle Dependencies Status
if [[ -f ground/build/reports/dependencyUpdates/report.json ]]; then
python cloud-builder/generate_dependency_health_svg.py ground/build/reports/dependencyUpdates/report.json dependency.svg
# Copy artifacts for Github badge
gsutil cp dependency.svg gs://${_CACHE_BUCKET}/dependency.svg
gsutil cp ground/build/reports/dependencyUpdates/report.txt gs://${_CACHE_BUCKET}/dependency.txt
# Makes files publicly readable
gsutil acl ch -u AllUsers:R gs://${_CACHE_BUCKET}/dependency.svg
gsutil acl ch -u AllUsers:R gs://${_CACHE_BUCKET}/dependency.txt
fi
# Build Status
if [ -f build-status.txt ] && [ $(< build-status.txt) == "fail" ]; then
gsutil cp cloud-builder/failure.svg gs://${_CACHE_BUCKET}/status.svg
else
gsutil cp cloud-builder/success.svg gs://${_CACHE_BUCKET}/status.svg
fi
# Make file publicly readable
gsutil acl ch -u AllUsers:R gs://${_CACHE_BUCKET}/status.svg
fi
# Delayed build fail
if [ -f build-status.txt ] && [ $(< build-status.txt) == "fail" ]; then
cat check-logs.txt
cat unit-test-logs.txt
exit 1
fi
- name: 'gcr.io/$PROJECT_ID/android:base'
id: &upload_code_coverage 'Upload code coverage stats'
waitFor:
- *update_status
args:
- '-c'
- |
# TODO: Add a check for _PUSH_TO_MASTER
curl -Os https://uploader.codecov.io/latest/linux/codecov
chmod +x codecov
./codecov -t ${_CODECOV_TOKEN} -R ground/build/jacoco
- name: 'gcr.io/$PROJECT_ID/android:base'
id: &compress_cache 'Compress gradle build cache'
waitFor:
- *update_status
args:
- '-c'
- |
if [ -f $(< hashsum)-cache.tgz ]; then
echo "Cache already exists, skipping build cache"
else
echo "Building new cache"
tar zcf cache.tgz .gradle/caches .gradle/wrapper
fi
- name: 'gcr.io/cloud-builders/gsutil'
id: &save_cache 'Save gradle cache to GCS'
entrypoint: 'bash'
waitFor:
- *compress_cache
args:
- '-c'
- |
if [ -f $(< hashsum)-cache.tgz ]; then
echo "Cache already exists, skipping save cache"
else
gsutil cp cache.tgz gs://${_CACHE_BUCKET}/$(< hashsum)-cache.tgz
fi
options:
env:
- 'TERM=dumb'
- 'GRADLE_USER_HOME=/workspace/.gradle'
logging: GCS_ONLY
machineType: 'N1_HIGHCPU_8'
timeout: 1800s