-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
199 lines (176 loc) · 7.36 KB
/
build-android-demos.yml
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
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@v4
with:
sparse-checkout: demos/android
fetch-depth: 2 # Required for git diff in PRs
- name: Generate matrix
id: set-matrix
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
# Get list of changed files in demos/android/ directory
changed_files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} HEAD -- 'demos/android/*')
echo "Changed files:"
echo "$changed_files"
# Extract unique demo directories
matrix=$(echo "$changed_files" | grep -oE 'demos/android/[^/]*/MASTG-DEMO-[^/]+' | sort -u | head -c -1 | tr '\n' ' ' | sed 's/ /","/g')
# If no changes, set empty matrix
if [ -z "$matrix" ]; then
echo "matrix={\"demo\":[]}" >> $GITHUB_OUTPUT
else
echo "matrix={\"demo\":[\"$matrix\"]}" >> $GITHUB_OUTPUT
fi
else
# Default behavior: include all demos for master branch
matrix=$(echo demos/android/*/MASTG-DEMO-* | sed 's/ /","/g')
echo "matrix={\"demo\":[\"$matrix\"]}" >> $GITHUB_OUTPUT
fi
echo "Print matrix: $matrix"
- name: Print matrix
run: echo "${{ steps.set-matrix.outputs.matrix }}"
build-base-app:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Get last commit hash of MASTestApp-Android
id: get-mastestapp-hash
run: |
echo "MASTESTAPP_HASH=$(git ls-remote https://github.com/cpholguera/MASTestApp-Android.git HEAD | awk '{print $1}')" >> $GITHUB_ENV
echo "MASTESTAPP_HASH=$MASTESTAPP_HASH" >> $GITHUB_OUTPUT
- name: Check if already cached
id: cache-check
uses: actions/cache/restore@v4
with:
lookup-only: true
path: MASTestApp-Android/ # we're forced to write a path even if we don't need it
key: base-app-${{ env.MASTESTAPP_HASH }}
- name: Set up JDK 17
if: steps.cache-check.outputs.cache-hit != 'true'
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Setup Gradle
if: steps.cache-check.outputs.cache-hit != 'true'
uses: gradle/actions/setup-gradle@v4
with:
cache-read-only: false
- name: Clone MASTestApp-Android repository
if: steps.cache-check.outputs.cache-hit != 'true'
uses: actions/checkout@v4
with:
repository: cpholguera/MASTestApp-Android
path: MASTestApp-Android
ref: ${{ env.MASTESTAPP_HASH }}
- name: Build base app
if: steps.cache-check.outputs.cache-hit != 'true'
run: |
cd MASTestApp-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 failed"
exit 1
)
echo "Build succeeded"
- name: Saving cache
if: steps.cache-check.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
path: MASTestApp-Android/
key: ${{ steps.cache-check.outputs.cache-primary-key }}
build:
needs: [generate-matrix, build-base-app]
runs-on: ubuntu-latest
timeout-minutes: 30 # Increase this value as needed
strategy:
matrix: ${{ fromJson(needs.generate-matrix.outputs.matrix) }}
max-parallel: 3 # Limit the number of parallel jobs
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
sparse-checkout: demos/android
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4
with:
cache-read-only: true
- name: Restore cache
id: cache-base-app
uses: actions/cache/restore@v4
with:
path: MASTestApp-Android/
key: base-app-${{ env.MASTESTAPP_HASH }}
- name: Replace files and build APK
run: |
demo="${{ matrix.demo }}"
[ -d "$demo" ] || (
echo "Demo directory not found: $demo"
exit 1
)
echo "Processing $demo"
cp -f "$demo/MastgTest.kt" MASTestApp-Android/app/src/main/java/org/owasp/mastestapp/MastgTest.kt 2>/dev/null \
&& echo "Copied MastgTest.kt for $demo" \
|| echo "No MastgTest.kt found for $demo"
cp -f "$demo/MastgTestWebView.kt" MASTestApp-Android/app/src/main/java/org/owasp/mastestapp/MastgTestWebView.kt 2>/dev/null \
&& echo "Copied MastgTestWebView.kt for $demo" \
|| echo "No MastgTestWebView.kt found for $demo"
cp -f "$demo/AndroidManifest.xml" MASTestApp-Android/app/src/main/AndroidManifest.xml 2>/dev/null \
&& echo "Copied AndroidManifest.xml for $demo" \
|| echo "No AndroidManifest.xml found for $demo"
cp -f "$demo/filepaths.xml" MASTestApp-Android/app/src/main/res/xml/filepaths.xml 2>/dev/null \
&& echo "Copied filepaths.xml for $demo" \
|| echo "No filepaths.xml found for $demo"
cp -f "$demo/network_security_config.xml" MASTestApp-Android/app/src/main/res/xml/network_security_config.xml 2>/dev/null \
&& echo "Copied network_security_config.xml for $demo" \
|| echo "No network_security_config.xml found for $demo"
cp -f "$demo/backup_rules.xml" MASTestApp-Android/app/src/main/res/xml/backup_rules.xml 2>/dev/null \
&& echo "Copied backup_rules.xml for $demo" \
|| echo "No backup_rules.xml found for $demo"
cp -f "$demo/data_extraction_rules.xml" MASTestApp-Android/app/src/main/res/xml/data_extraction_rules.xml 2>/dev/null \
&& echo "Copied data_extraction_rules.xml for $demo" \
|| echo "No data_extraction_rules.xml found for $demo"
echo "Building APK for $demo"
cd MASTestApp-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 failed for $demo"
exit 1
)
cd ..
echo "Build succeeded for $demo"
apk_filename="$(basename "$demo").apk"
mv MASTestApp-Android/app/build/outputs/apk/debug/app-debug.apk "$apk_filename" || (
echo "APK not found for $demo"
exit 1
)
echo "APK for $demo moved to $apk_filename"
echo "APK_NAME=$apk_filename" >> $GITHUB_ENV
- name: Upload APK
uses: actions/upload-artifact@v4
with:
name: ${{ env.APK_NAME }}
path: "${{ env.APK_NAME }}"