-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
285 lines (227 loc) · 9.2 KB
/
Copy pathbuild-android-demos.yml
File metadata and controls
285 lines (227 loc) · 9.2 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
name: Build All Android Demos
on:
workflow_dispatch:
push:
branches:
- master
paths:
- 'demos/android/**'
pull_request:
branches:
- master
paths:
- 'demos/**'
- '.github/workflows/build-android-demos.yml'
jobs:
generate-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
sparse-checkout: demos/android
fetch-depth: 0
- name: Generate matrix
id: set-matrix
shell: bash
run: |
set -euo pipefail
if [ "${{ github.event_name }}" = "pull_request" ]; then
changed_files="$(git diff --name-only "${{ github.event.pull_request.base.sha }}" HEAD -- 'demos/android/**')"
echo "Changed files:"
echo "$changed_files"
demos="$(echo "$changed_files" \
| grep -oE '^demos/android/[^/]+/MASTG-DEMO-[^/]+' \
| sort -u || true)"
else
demos="$(find demos/android -mindepth 2 -maxdepth 2 -type d -name 'MASTG-DEMO-*' | sort)"
fi
matrix="$(printf '%s\n' "$demos" \
| jq -R 'select(length > 0)' \
| jq -sc '{demo: .}')"
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"
echo "Print matrix: $matrix"
- name: Print matrix
run: echo '${{ steps.set-matrix.outputs.matrix }}'
build-base-app:
runs-on: ubuntu-latest
timeout-minutes: 10
outputs:
mastestapp_hash: ${{ steps.get-mastestapp-hash.outputs.mastestapp_hash }}
steps:
- name: Get last commit hash of mas-app-android
id: get-mastestapp-hash
shell: bash
run: |
set -euo pipefail
hash="$(git ls-remote https://github.com/cpholguera/mas-app-android.git HEAD | awk '{print $1}')"
echo "mastestapp_hash=$hash" >> "$GITHUB_OUTPUT"
echo "MASTESTAPP_HASH=$hash" >> "$GITHUB_ENV"
- name: Check if already cached
id: cache-check
uses: actions/cache/restore@v5
with:
lookup-only: true
path: mas-app-android/
key: base-app-${{ steps.get-mastestapp-hash.outputs.mastestapp_hash }}
- name: Set up JDK 17
if: steps.cache-check.outputs.cache-hit != 'true'
uses: actions/setup-java@v5
with:
java-version: '17'
distribution: 'temurin'
- name: Setup Gradle
if: steps.cache-check.outputs.cache-hit != 'true'
uses: gradle/actions/setup-gradle@v6
with:
cache-read-only: false
- name: Accept Android SDK licenses
if: steps.cache-check.outputs.cache-hit != 'true'
run: yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses
- name: Clone mas-app-android repository
if: steps.cache-check.outputs.cache-hit != 'true'
uses: actions/checkout@v6
with:
repository: cpholguera/mas-app-android
path: mas-app-android
ref: ${{ steps.get-mastestapp-hash.outputs.mastestapp_hash }}
- name: Build base app
if: steps.cache-check.outputs.cache-hit != 'true'
shell: bash
run: |
set -euo pipefail
cd mas-app-android
grep -q 'org.gradle.caching=true' gradle.properties \
|| echo -en "\norg.gradle.caching=true\norg.gradle.configuration-cache=true\n" >> gradle.properties
./gradlew assembleDebug --stacktrace
echo "Build succeeded"
- name: Saving cache
if: steps.cache-check.outputs.cache-hit != 'true'
uses: actions/cache/save@v5
with:
path: mas-app-android/
key: base-app-${{ steps.get-mastestapp-hash.outputs.mastestapp_hash }}
build:
needs:
- generate-matrix
- build-base-app
if: ${{ needs.generate-matrix.outputs.matrix != '{"demo":[]}' }}
runs-on: ubuntu-latest
timeout-minutes: 30
strategy:
matrix: ${{ fromJson(needs.generate-matrix.outputs.matrix) }}
max-parallel: 3
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
sparse-checkout: demos/android
- name: Set up JDK 17
uses: actions/setup-java@v5
with:
java-version: '17'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v6
with:
cache-read-only: true
- name: Accept Android SDK licenses
run: yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses
- name: Restore base app cache
id: cache-base-app
uses: actions/cache/restore@v5
with:
path: mas-app-android/
key: base-app-${{ needs.build-base-app.outputs.mastestapp_hash }}
fail-on-cache-miss: true
- name: Replace files and build APK
shell: bash
run: |
set -euo pipefail
demo="${{ matrix.demo }}"
[ -d "$demo" ] || {
echo "Demo directory not found: $demo"
exit 1
}
echo "Processing $demo"
copy_if_exists() {
local source_file="$1"
local target_file="$2"
local label="$3"
if [ -f "$source_file" ]; then
cp -f "$source_file" "$target_file"
echo "Copied $label for $demo"
else
echo "No $label found for $demo"
fi
}
copy_if_exists "$demo/MastgTest.kt" mas-app-android/app/src/main/java/org/owasp/mastestapp/MastgTest.kt MastgTest.kt
copy_if_exists "$demo/MainActivity.kt" mas-app-android/app/src/main/java/org/owasp/mastestapp/MainActivity.kt MainActivity.kt
copy_if_exists "$demo/MastgTestWebView.kt" mas-app-android/app/src/main/java/org/owasp/mastestapp/MastgTestWebView.kt MastgTestWebView.kt
copy_if_exists "$demo/AndroidManifest.xml" mas-app-android/app/src/main/AndroidManifest.xml AndroidManifest.xml
copy_if_exists "$demo/filepaths.xml" mas-app-android/app/src/main/res/xml/filepaths.xml filepaths.xml
copy_if_exists "$demo/network_security_config.xml" mas-app-android/app/src/main/res/xml/network_security_config.xml network_security_config.xml
copy_if_exists "$demo/backup_rules.xml" mas-app-android/app/src/main/res/xml/backup_rules.xml backup_rules.xml
copy_if_exists "$demo/data_extraction_rules.xml" mas-app-android/app/src/main/res/xml/data_extraction_rules.xml data_extraction_rules.xml
copy_if_exists "$demo/proguard-rules.pro" mas-app-android/app/proguard-rules.pro proguard-rules.pro
if find "$demo" -maxdepth 1 -name "*.proto" -print -quit 2>/dev/null | grep -q .; then
mkdir -p mas-app-android/app/src/main/proto
find "$demo" -maxdepth 1 -name "*.proto" -print0 2>/dev/null \
| while IFS= read -r -d '' proto_file; do
cp -f "$proto_file" mas-app-android/app/src/main/proto/
echo "Copied $(basename "$proto_file") for $demo"
done
else
echo "No .proto files found for $demo"
fi
if [ -f "$demo/CMakeLists.txt" ]; then
mkdir -p mas-app-android/app/src/main/cpp
cp -f "$demo/CMakeLists.txt" mas-app-android/app/src/main/cpp/CMakeLists.txt
echo "Copied CMakeLists.txt for $demo"
find "$demo" -maxdepth 1 -name "*.cpp" -print0 2>/dev/null \
| while IFS= read -r -d '' cpp_file; do
cp -f "$cpp_file" mas-app-android/app/src/main/cpp/
echo "Copied $(basename "$cpp_file") for $demo"
done
else
echo "No CMakeLists.txt found for $demo"
fi
insert_block() {
local kind="$1"
local upper="${kind^^}"
local file="$demo/build.gradle.kts.$kind"
local target="mas-app-android/app/build.gradle.kts"
if [ -f "$file" ]; then
sed -i '/\/\/ ADD_'"$upper"'_HERE/{
r '"$file"'
d
}' "$target"
echo "Replaced $kind in build.gradle.kts for $demo"
else
echo "No build.gradle.kts.$kind found for $demo, skipping $kind replacement"
fi
}
insert_block plugins
insert_block sections
insert_block android
insert_block libs
insert_block build
echo "Building APK for $demo"
cd mas-app-android
grep -q 'org.gradle.caching=true' gradle.properties \
|| echo -en "\norg.gradle.caching=true\norg.gradle.configuration-cache=true\n" >> gradle.properties
./gradlew assembleDebug --stacktrace
cd ..
echo "Build succeeded for $demo"
apk_filename="$(basename "$demo").apk"
mv mas-app-android/app/build/outputs/apk/debug/app-debug.apk "$apk_filename"
echo "APK for $demo moved to $apk_filename"
echo "APK_NAME=$apk_filename" >> "$GITHUB_ENV"
- name: Upload APK
uses: actions/upload-artifact@v6
with:
name: ${{ env.APK_NAME }}
path: ${{ env.APK_NAME }}
if-no-files-found: error