name: Android IME # Types on real Gboard in an Android emulator into a test page built from # the workspace editor, see `packages/editor/android/README.md`. # # Each job prepares a fresh emulator for its row's language and runs that # row's device test file. A job is red when a prep step failed or a test # failed, an editor assertion included, or as an infrastructure failure when # the SDK install failed twice, the emulator never booted, adb lost the # device or Gboard's Japanese dictionary never loaded on the retry too. A # failed SDK install is retried once. # # The `main` ruleset requires the `Android IME` job, so every pull request # starts this workflow. The emulator jobs run only when `changes` matches a # path in its filter. Otherwise `Android IME` passes without them. on: pull_request: schedule: - cron: "17 3 * * *" workflow_dispatch: concurrency: group: android-ime-${{ github.ref }} cancel-in-progress: true permissions: contents: read jobs: changes: runs-on: ubuntu-latest timeout-minutes: 5 permissions: contents: read pull-requests: read outputs: android: ${{ github.event_name != 'pull_request' || (!(startsWith(github.head_ref, 'changeset-release/') && github.event.pull_request.head.repo.full_name == github.repository) && steps.filter.outputs.android == 'true') }} steps: # Version Packages pull requests, from `changeset-release/main` and `changeset-release/next`, only bump # versions and changelogs. Other pull requests that touch `packages/editor/package.json` still run. - uses: dorny/paths-filter@v4 id: filter if: github.event_name == 'pull_request' && !(startsWith(github.head_ref, 'changeset-release/') && github.event.pull_request.head.repo.full_name == github.repository) with: filters: | android: - packages/editor/src/engine/react/components/editable.tsx - packages/editor/src/engine/react/hooks/android-input-manager/** - packages/editor/src/engine/react/hooks/use-track-user-input.ts - packages/editor/src/engine/dom/plugin/dom-editor.ts - packages/editor/src/behaviors/behavior.abstract.input.ts - packages/editor/src/editor/Editable.tsx - packages/editor/package.json - packages/editor/android/** - .github/scripts/** - packages/editor/vitest.android.config.ts - packages/thumbjar/** - .github/workflows/android-ime.yml emulator: name: Android IME (${{ matrix.suite.file }}) needs: changes if: needs.changes.outputs.android == 'true' runs-on: ubuntu-latest # Backstops only: `thumbjar run` bounds its own work and logs the reason when it stops the work, see # the time budget in `packages/editor/android/README.md`. The job's cap is the sum of every # step's cap, so a hang in every step still leaves time for `Result` and the artifact upload, and # `budget.test.ts` keeps it so. timeout-minutes: 331 env: TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }} TURBO_TEAM: ${{ vars.TURBO_TEAM }} strategy: fail-fast: false matrix: # One row per device test file CI runs, on a device prepared for its # language. `tests/en-textarea.test.ts` is a control run by hand. suite: - {lang: ja, file: tests/ja.test.ts, runMinutes: 90} - {lang: en, file: tests/en.test.ts, runMinutes: 90} - {lang: ja, file: tests/ja.enter.test.ts, runMinutes: 90} - {lang: en, file: tests/en.enter.test.ts, runMinutes: 90} - {lang: en, file: tests/en.insert.break.test.ts, runMinutes: 124} - {lang: en, file: tests/en.insert.text.test.ts, runMinutes: 90} api: [36] steps: - name: Name the suite id: suite timeout-minutes: 1 run: echo "name=$(basename '${{ matrix.suite.file }}' .test.ts)" >> "$GITHUB_OUTPUT" - uses: actions/checkout@v7 timeout-minutes: 3 - name: KVM probe timeout-minutes: 1 run: | ls -l /dev/kvm || echo "NO /dev/kvm" grep -cE 'vmx|svm' /proc/cpuinfo || true - name: Enable KVM group perms timeout-minutes: 1 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 - uses: pnpm/action-setup@v6 timeout-minutes: 2 - uses: actions/setup-node@v6 timeout-minutes: 3 with: node-version: 24 - name: Install project dependencies timeout-minutes: 8 run: pnpm install - name: Build the test page timeout-minutes: 10 run: | pnpm exec turbo build --filter='@portabletext/editor...' pnpm --filter @portabletext/editor build:android node -e "console.log('EDITOR_VERSION', require('./packages/editor/package.json').version)" # The jar comes from the actions cache when an earlier run saved it, under the editor package's cache # directory and a key that changes with its URL and hash, see the jar cache in # `packages/editor/android/README.md`. A failed restore or save is a miss, which downloads as before. - name: Restore the uiautomator2 server jar id: u2-jar-restore continue-on-error: true timeout-minutes: 1 uses: actions/cache/restore@v6 with: key: u2-jar-${{ hashFiles('packages/thumbjar/src/device/u2-jar.json') }} path: packages/editor/node_modules/.cache/thumbjar/u2.jar # A restored jar whose hash differs is downloaded again. - name: Fetch the uiautomator2 server jar id: u2-jar-fetch timeout-minutes: 3 run: pnpm --filter @portabletext/editor exec thumbjar fetch-u2-jar # Saves from any ref: a pull request's entry is read only by that pull request's runs, and main's by every branch. - name: Save the uiautomator2 server jar if: steps.u2-jar-fetch.outcome == 'success' && steps.u2-jar-restore.outputs.cache-hit != 'true' continue-on-error: true timeout-minutes: 1 uses: actions/cache/save@v6 with: key: u2-jar-${{ hashFiles('packages/thumbjar/src/device/u2-jar.json') }} path: packages/editor/node_modules/.cache/thumbjar/u2.jar # The pinned emulator and the available system image come from the actions cache, see the SDK cache # in `packages/editor/android/README.md`. A failed lookup or restore installs as on a miss; # a failed save only skips the cache. - name: Look up the SDK cache key id: sdk-key continue-on-error: true timeout-minutes: 1 working-directory: packages/editor run: | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --list 2> /dev/null | node ../../.github/scripts/emulator-runs.mjs cache-revisions "$ANDROID_HOME" "${{ matrix.api }}" "$RUNNER_TEMP/android-sdk-cache.tsv" - name: Restore the SDK cache id: sdk-restore if: steps.sdk-key.outputs.key != '' continue-on-error: true timeout-minutes: 5 uses: actions/cache/restore@v6 with: key: ${{ steps.sdk-key.outputs.key }} path: ${{ steps.sdk-key.outputs.paths }} # A restored package whose files differ from the manifest saved with it is deleted, so the install # downloads it. A partial restore is checked too. - name: Check the SDK cache id: sdk-check if: steps.sdk-key.outputs.key != '' continue-on-error: true timeout-minutes: 1 working-directory: packages/editor env: SDK_CACHE_KEY: ${{ steps.sdk-key.outputs.key }} run: node ../../.github/scripts/emulator-runs.mjs cache-check "$ANDROID_HOME" "${{ matrix.api }}" "$RUNNER_TEMP/android-sdk-cache.tsv" "$SDK_CACHE_KEY" # A restore the check could not vouch for never reaches the install, so this step may fail the job. - name: Clear an unchecked SDK cache if: steps.sdk-check.outcome == 'failure' timeout-minutes: 1 run: rm -rf "$ANDROID_HOME/emulator" "$ANDROID_HOME/system-images/android-${{ matrix.api }}/google_apis_playstore/x86_64" # `emulator/` holds the unregistered pin, so the emulators that `sdkmanager` # and the runner action install land in the unused `emulator-2`. - name: Install the emulator and its system image id: install1 continue-on-error: true timeout-minutes: 8 working-directory: packages/editor run: | node ../../.github/scripts/emulator-runs.mjs install-start "$ANDROID_HOME" "${{ matrix.api }}" node ../../.github/scripts/emulator-runs.mjs install-emulator "$ANDROID_HOME" >> android/out/sdk-install-1.log 2> >(tee -a android/out/sdk-install-1.log >&2) yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses > /dev/null "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --install platform-tools 'platforms;android-${{ matrix.api }}' 'system-images;android-${{ matrix.api }};google_apis_playstore;x86_64' >> android/out/sdk-install-1.log 2> >(tee -a android/out/sdk-install-1.log >&2) - name: Install the emulator and its system image (retry) id: install2 if: steps.install1.outcome == 'failure' continue-on-error: true timeout-minutes: 8 working-directory: packages/editor run: | node ../../.github/scripts/emulator-runs.mjs install-retry "$ANDROID_HOME" "${{ matrix.api }}" node ../../.github/scripts/emulator-runs.mjs install-emulator "$ANDROID_HOME" >> android/out/sdk-install-2.log 2> >(tee -a android/out/sdk-install-2.log >&2) yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses > /dev/null "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --install platform-tools 'platforms;android-${{ matrix.api }}' 'system-images;android-${{ matrix.api }};google_apis_playstore;x86_64' >> android/out/sdk-install-2.log 2> >(tee -a android/out/sdk-install-2.log >&2) # Only runs on main save, the nightly schedule and a manual dispatch on main, so pull requests read # its entry and add none. - name: Record the SDK cache manifest id: sdk-manifest if: github.ref == 'refs/heads/main' && steps.sdk-key.outputs.key != '' && steps.sdk-restore.outputs.cache-hit != 'true' && (steps.install1.outcome == 'success' || steps.install2.outcome == 'success') continue-on-error: true timeout-minutes: 1 working-directory: packages/editor env: SDK_CACHE_KEY: ${{ steps.sdk-key.outputs.key }} run: node ../../.github/scripts/emulator-runs.mjs cache-manifest "$ANDROID_HOME" "${{ matrix.api }}" "$RUNNER_TEMP/android-sdk-cache.tsv" "$SDK_CACHE_KEY" - name: Save the SDK cache if: steps.sdk-manifest.outcome == 'success' continue-on-error: true timeout-minutes: 5 uses: actions/cache/save@v6 with: key: ${{ steps.sdk-key.outputs.key }} path: ${{ steps.sdk-key.outputs.paths }} - name: Record start if: steps.install1.outcome == 'success' || steps.install2.outcome == 'success' timeout-minutes: 1 working-directory: packages/editor run: mkdir -p android/out && echo "EMULATOR_START at $(date +%s)" | tee android/out/timing.txt # The emulator process sometimes vanishes at a Chrome force-stop with nothing on the record. This samples # host memory and the emulator's process until the capture step, so the next loss says when and why. - name: Start the host sampler if: steps.install1.outcome == 'success' || steps.install2.outcome == 'success' timeout-minutes: 3 continue-on-error: true working-directory: packages/editor run: | timeout --kill-after=5 110 bash ../../.github/scripts/host-core.sh prepare android/out || true setsid nohup bash ../../.github/scripts/host-sampler.sh android/out/host.log > /dev/null 2>&1 < /dev/null & sleep 1 echo started # Boot, device prep and the tests, see the budget in `packages/editor/android/README.md`. - name: Run on emulator id: run1 if: steps.install1.outcome == 'success' || steps.install2.outcome == 'success' continue-on-error: true timeout-minutes: ${{ matrix.suite.runMinutes }} uses: reactivecircus/android-emulator-runner@v2 with: api-level: ${{ matrix.api }} target: google_apis_playstore arch: x86_64 profile: pixel_6 disable-animations: true ram-size: 4096M cores: 4 heap-size: 512M emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim emulator-boot-timeout: 360 working-directory: packages/editor pre-emulator-launch-script: echo "### Attempt 1" >> "$GITHUB_STEP_SUMMARY" script: pnpm exec thumbjar run ${{ matrix.suite.file }} --keyboard ${{ matrix.suite.lang }} --page-dir android/dist --config vitest.android.config.ts --project device --out-dir android/out # The emulator sometimes never comes online, adb loses it after boot, or # Gboard's Japanese dictionary never loads on that boot. Retry once on a # fresh emulator then, never after any other failure on a reachable # device. - name: Decide the retry id: retry if: steps.run1.outcome == 'failure' timeout-minutes: 1 working-directory: packages/editor run: node ../../.github/scripts/emulator-runs.mjs retry android/out/timing.txt - name: Run on emulator (retry) id: run2 if: steps.run1.outcome == 'failure' && steps.retry.outputs.retry == 'yes' continue-on-error: true timeout-minutes: ${{ matrix.suite.runMinutes }} uses: reactivecircus/android-emulator-runner@v2 with: api-level: ${{ matrix.api }} target: google_apis_playstore arch: x86_64 profile: pixel_6 disable-animations: true ram-size: 4096M cores: 4 heap-size: 512M emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim emulator-boot-timeout: 360 working-directory: packages/editor pre-emulator-launch-script: echo "### Attempt 2" >> "$GITHUB_STEP_SUMMARY" script: pnpm exec thumbjar run ${{ matrix.suite.file }} --keyboard ${{ matrix.suite.lang }} --page-dir android/dist --config vitest.android.config.ts --project device --out-dir android/out - name: Boot time if: always() timeout-minutes: 1 working-directory: packages/editor run: | node ../../.github/scripts/emulator-runs.mjs boot-seconds android/out/timing.txt cat android/out/phases.log || true # Stops the sampler and reads the kernel log, disk, memory and the emulator's crash data while the host # still holds them, then names each emulator exit the sampler saw in the job summary. - name: Capture host evidence if: always() timeout-minutes: 7 continue-on-error: true working-directory: packages/editor run: | bash ../../.github/scripts/host-capture.sh android/out node ../../.github/scripts/emulator-runs.mjs host-evidence android/out || true # The run steps continue on error so the retry can follow. This step # turns the job red when the last run failed, and names a failure of the # emulator apart from a test result. - name: Result env: INSTALL1_OUTCOME: ${{ steps.install1.outcome }} INSTALL2_OUTCOME: ${{ steps.install2.outcome }} JOB_STATUS: ${{ job.status }} if: always() timeout-minutes: 1 working-directory: packages/editor run: node ../../.github/scripts/emulator-runs.mjs result android/out/timing.txt "${{ steps.run2.outcome != 'skipped' && steps.run2.outcome || steps.run1.outcome }}" "$ANDROID_HOME" - uses: actions/upload-artifact@v7 if: always() timeout-minutes: 5 with: name: android-ime-${{ steps.suite.outputs.name }}-api${{ matrix.api }}-${{ github.run_attempt }} path: packages/editor/android/out/ result: name: Android IME needs: [changes, emulator] if: always() runs-on: ubuntu-latest timeout-minutes: 5 env: CHANGES_RESULT: ${{ needs.changes.result }} ANDROID_CHANGED: ${{ needs.changes.outputs.android }} EMULATOR_RESULT: ${{ needs.emulator.result }} steps: - name: Check the emulator jobs run: | echo "changes: $CHANGES_RESULT, android: $ANDROID_CHANGED, emulator: $EMULATOR_RESULT" test "$CHANGES_RESULT" = success if [ "$ANDROID_CHANGED" = true ]; then test "$EMULATOR_RESULT" = success else test "$ANDROID_CHANGED" = false test "$EMULATOR_RESULT" = skipped fi