You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nowinandroid/.github/workflows/Build.yaml

160 lines
6.3 KiB

name: "Build APKs + Emulator test (trigger: push to test)"
on:
push:
branches:
- testNIA
workflow_dispatch:
permissions:
contents: read
jobs:
build_and_upload:
name: Build APKs and test APKs
runs-on: ubuntu-latest
timeout-minutes: 60
outputs:
apks_artifact: apks
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Copy CI gradle.properties (if exists)
run: |
if [ -f .github/ci-gradle.properties ]; then
mkdir -p ~/.gradle
cp .github/ci-gradle.properties ~/.gradle/gradle.properties
fi
- name: Set up JDK 17
uses: actions/setup-java@v5
with:
distribution: 'zulu'
java-version: 17
- name: Setup Gradle (cache enabled)
uses: gradle/actions/setup-gradle@v4
with:
cache-encryption-key: ${{ secrets.GRADLE_ENCRYPTION_KEY }}
build-cache: true
- name: Build all APKs (assemble)
# This builds flavors / variants. It may take a while the first run.
run: ./gradlew :app:assemble -PminifyWithR8=false --no-daemon --no-parallel
- name: Find APKs and test APKs
id: find-apks
run: |
echo "Searching for APKs under app/build/outputs..."
find ./app/build/outputs -type f -name "*.apk" -print || true
# Collect all APKs (app & androidTest apks)
mkdir -p apk_collection
# find apks (exclude mapping & intermediates)
find ./app/build/outputs -type f -name "*.apk" -not -path "*/mapping/*" -not -path "*/outputs/mapping/*" -exec cp {} apk_collection/ \;
echo "Files copied to apk_collection:"
ls -lah apk_collection || true
if [ $(ls -A apk_collection | wc -l) -eq 0 ]; then
echo "No APKs found! failing."
exit 1
fi
echo "::set-output name=apk_dir::apk_collection"
- name: Upload APKs artifact
uses: actions/upload-artifact@v4
with:
name: apks
path: apk_collection/**
emulator_run:
name: Run emulator, install APKs, run instrumentation & record
needs: build_and_upload
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Download built APKs
uses: actions/download-artifact@v4
with:
name: apks
path: ./apks
- name: Enable KVM on runner (Linux)
if: runner.os == 'Linux'
run: |
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvm || true
ls -la /dev/kvm || true
- name: Prepare artifacts dir
run: mkdir -p artifacts
- name: Start emulator, install APKs, run tests and record (ReactiveCircus)
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: 30
arch: x86_64
force-avd-creation: true
disable-animations: true
emulator-options: -no-window -no-boot-anim -noaudio -gpu swiftshader_indirect
script: |
set -euo pipefail
echo "Waiting for emulator..."
adb wait-for-device
until adb -s emulator-5554 shell getprop sys.boot_completed | grep -m1 "1"; do sleep 1; done
echo "Emulator booted."
# Install all apks found in ./apks (app and test apks)
echo "Installing APKs..."
for f in ./apks/*.apk; do
echo "Installing $f"
adb install -r "$f" || (echo "install failed for $f, continuing" && true)
done
# Attempt to detect an instrumentation runner automatically
echo "Detecting installed instrumentation..."
INST_LIST="$(adb shell pm list instrumentation || true)"
echo "Instrumentation list: $INST_LIST"
# pick first instrumentation component (format: instrumentation: <name>/<runner> (target=...))
INST=$(echo "$INST_LIST" | sed -n 's/^instrumentation:\s*//p' | head -n1 | awk '{print $1}' | tr -d '\r')
if [ -z "$INST" ]; then
echo "No instrumentation found. Trying to detect test runner via package name fallback..."
# fallback: try to find any package that looks like demo or nowinandroid (best-effort)
PACKAGE=$(adb shell pm list packages | tr -d '\r' | grep -E 'nowinandroid|nia|nowin' | head -n1 | sed 's/package://g' || true)
echo "Detected package: $PACKAGE"
if [ -n "$PACKAGE" ]; then
# list instrumentation for that package specifically
INST=$(adb shell pm list instrumentation | tr -d '\r' | grep "$PACKAGE" | head -n1 | sed -n 's/^instrumentation:\s*//p' | awk '{print $1}' || true)
fi
fi
if [ -z "$INST" ]; then
echo "Couldn't auto-detect instrumentation runner. As fallback, will run a simple smoke launch."
# attempt to start main activity (best-effort)
adb shell monkey -p com.google.samples.apps.nowinandroid.dev -c android.intent.category.LAUNCHER 1 || true
else
echo "Running instrumentation: $INST"
# Start screenrecord in background on device (30s)
adb shell screenrecord --time-limit 30 /sdcard/test_run.mp4 &
# Run instrumentation - this will run tests already packaged in the test APK
adb shell am instrument -w "$INST" || true
# Give a moment for the recording to finish (screenrecord stops automatically after time-limit)
sleep 5
# Pull the recording
adb pull /sdcard/test_run.mp4 ./artifacts/test_run.mp4 || true
fi
# Collect memory and gfx info and logs
adb shell dumpsys meminfo > ./artifacts/dumpsys_meminfo.txt || true
adb shell dumpsys gfxinfo com.google.samples.apps.nowinandroid.dev > ./artifacts/gfxinfo.txt || true
adb logcat -d > ./artifacts/logcat.txt || true
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: emulator-artifacts
path: artifacts/**, apks/**