# GMD regression tests on CI.
#
# This mirrors the smoke workflow's emulator setup but keeps PR execution
# opt-in through regression-label.yml. Nightly scheduled runs execute the
# Phase 1 full aggregate; label/manual/reusable calls execute the core tier.
name: Regression
permissions:
  contents: read

on:
  workflow_dispatch:
  workflow_call:
  schedule:
    - cron: '17 8 * * *'

jobs:
  regression:
    name: GMD Regression Tests
    runs-on: ubuntu-latest
    timeout-minutes: 90
    concurrency:
      group: regression-${{ github.ref }}
      cancel-in-progress: true
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Set up JDK 17
        uses: actions/setup-java@v5
        with:
          java-version: '17'
          distribution: 'temurin'

      - name: Set up Gradle
        uses: gradle/actions/setup-gradle@v6
        with:
          cache-read-only: ${{ github.ref != 'refs/heads/master' }}
          cache-cleanup: on-success

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Enable KVM
        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

      - name: Run Regression Check
        if: github.event_name != 'schedule'
        run: ./gradlew regressionCheck -Pandroid.testoptions.manageddevices.emulator.gpu=swiftshader_indirect

      - name: Run Nightly Regression Checks
        if: github.event_name == 'schedule'
        run: ./gradlew regressionFullCheck -Pandroid.testoptions.manageddevices.emulator.gpu=swiftshader_indirect

      - name: Upload Managed Device Reports
        if: failure()
        uses: actions/upload-artifact@v7
        with:
          name: gmd-regression-test-reports
          path: |
            app/build/reports/androidTests/managedDevice/
            app/build/outputs/androidTest-results/managedDevice/
            app/build/outputs/managed_device_android_test_additional_output/
          if-no-files-found: warn
          retention-days: 7
