forked from google/ground-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudbuild.yaml
More file actions
196 lines (170 loc) · 6.87 KB
/
Copy pathcloudbuild.yaml
File metadata and controls
196 lines (170 loc) · 6.87 KB
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
#
# 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 --> --> Authorize Gcloud --> Build test apks --> Instrumented tests -->
#
steps:
- name: 'gcr.io/cloud-builders/gsutil'
id: ©_build_cache 'Copying build cache'
waitFor: [ '-' ]
# we use rsync and not cp so that this step doesn't fail the first time it's run
args: [ 'rsync', 'gs://${_CACHE_BUCKET}/', './' ]
- name: 'gcr.io/$PROJECT_ID/android:base'
id: &extract_build_cache 'Extracting tar'
waitFor:
- *copy_build_cache
args:
- '-c'
- |
tar zxf cache.tgz || echo "No cache found."
- name: 'gcr.io/$PROJECT_ID/android:28'
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 > gnd/google-services.json
- name: 'gcr.io/$PROJECT_ID/android:28'
id: &assemble_debug 'Assemble debug apks'
waitFor:
- *extract_build_cache
- *config_google_services
args:
- '-c'
- |
./gradlew -PdisablePreDex assembleStaging assembleStagingUnitTest assembleStagingAndroidTest -PtestBuildType=staging
# Run code quality checks
- name: 'gcr.io/$PROJECT_ID/android:28'
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
- name: 'gcr.io/$PROJECT_ID/android:28'
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
- name: 'gcr.io/$PROJECT_ID/android:28'
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:28'
id: &instrumented_tests 'Run instrumented tests'
waitFor:
- *authenticate_gcloud
entrypoint: 'bash'
args:
- '-c'
- |
gcloud --quiet beta firebase test android run \
--type instrumentation --num-uniform-shards=1 \
--app gnd/build/outputs/apk/staging/*.apk \
--test gnd/build/outputs/apk/androidTest/staging/*.apk \
--device model=Pixel2,version=28,locale=en,orientation=portrait \
--results-bucket ${_ARTIFACT_BUCKET} \
--results-dir "$BRANCH_NAME-$BUILD_ID/reports" \
--timeout 20m
- name: 'gcr.io/cloud-builders/gsutil'
id: &save_reports 'Save reports to GCS'
waitFor:
- *unit_tests
- *instrumented_tests
args: [ 'cp', '-r',
'gnd/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
# Check for updates
./gradlew -PdisablePreDex dependencyUpdates
# Gradle Dependencies Status
if [[ -f gnd/build/reports/dependencyUpdates/report.json ]]; then
python cloud-builder/generate_dependency_health_svg.py gnd/build/reports/dependencyUpdates/report.json dependency.svg
# Copy artifacts for Github badge
gsutil cp dependency.svg gs://${_CACHE_BUCKET}/dependency.svg
gsutil cp gnd/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: &compress_cache 'Compress gradle build cache'
waitFor:
- *update_status
args:
- '-c'
- |
tar zcf cache.tgz .gradle/caches .gradle/wrapper
- name: 'gcr.io/cloud-builders/gsutil'
id: &save_cache 'Save gradle cache to GCS'
waitFor:
- *compress_cache
args: [ 'cp', 'cache.tgz', 'gs://${_CACHE_BUCKET}/cache.tgz' ]
options:
env:
- 'TERM=dumb'
- 'GRADLE_USER_HOME=/workspace/.gradle'
logging: GCS_ONLY
machineType: 'N1_HIGHCPU_8'
timeout: 1800s