Skip to content

build-android-demos.yml: simplify shell scripts and cache Android app build #276

build-android-demos.yml: simplify shell scripts and cache Android app build

build-android-demos.yml: simplify shell scripts and cache Android app build #276

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
- name: Generate matrix with demos
id: set-matrix
run: |
matrix="$(echo demos/android/*/MASTG-DEMO-* | tr ' ' ',')"
echo "matrix={\"demo\":[$matrix]}" >> $GITHUB_OUTPUT
- name: Print matrix
run: echo "${{ steps.set-matrix.outputs.matrix }}"
build-base-app:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Clone MASTestApp-Android repository
run: git clone https://github.com/cpholguera/MASTestApp-Android.git
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Build APK
run: |
cd MASTestApp-Android
./gradlew assembleDebug --stacktrace || (
echo "Build failed"
exit 1
)
echo "Build succeeded"
- name: Cache build output
uses: actions/cache@v4
with:
path: MASTestApp-Android/
key: base-build-${{ github.sha }}
build:
needs: [generate-matrix, build-base-app]
runs-on: ubuntu-latest
timeout-minutes: 60 # 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
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Restore base app build
uses: actions/cache@v4
with:
path: MASTestApp-Android/
key: base-build-${{ runner.os }}-${{ github.sha }}
- 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/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"
cd MASTestApp-Android
echo "Building APK for $demo"
./gradlew assembleDebug --stacktrace || (
echo "Build failed for $demo"
exit 1
)
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.apk" >> $GITHUB_ENV
- name: List generated APK
run: |
echo "Listing generated APK in demos/android directory:"
ls -l "${{ env.APK_NAME }}" || echo "No APK found."
- name: Upload APK
uses: actions/upload-artifact@v4
with:
name: ${{ env.APK_NAME }}
path: "${{ env.APK_NAME }}"