name: Generate Baseline Profiles

on:
  push:
    tags:
      - 'b[0-9]+.[0-9]+.[0-9]+'


jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 120

    steps:
      - name: Enable KVM group perms
        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
          ls /dev/kvm

      - name: Checkout
        uses: actions/checkout@v4

      - name: Validate Gradle Wrapper
        uses: gradle/wrapper-validation-action@v3

      - name: Copy CI gradle.properties
        run: mkdir -p ~/.gradle ; cp .github/ci-gradle.properties ~/.gradle/gradle.properties

      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: 21

      - name: Install GMD image for baseline profile generation
        run: yes | "$ANDROID_HOME"/cmdline-tools/latest/bin/sdkmanager "system-images;android-33;aosp_atd;x86_64"

      - name: Accept Android licenses
        run: yes | "$ANDROID_HOME"/cmdline-tools/latest/bin/sdkmanager --licenses || true

      - name: Build release variant including baseline profile generation
        run: ./gradlew generateBaselineProfile
          -Pandroid.testInstrumentationRunnerArguments.androidx.benchmark.enabledRules=BaselineProfile
          -Pandroid.testoptions.manageddevices.emulator.gpu="swiftshader_indirect"
          -Pandroid.experimental.testOptions.managedDevices.emulator.showKernelLogging=true
          -Pandroid.experimental.androidTest.numManagedDeviceShards=1
          -Pandroid.experimental.testOptions.managedDevices.maxConcurrentDevices=1

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v7.0.6
        with:
          commit-message: Update baseline
          committer: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
          author: ${{ github.actor }} <${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com>
          signoff: false
          branch: generate-baseline
          base: develop
          delete-branch: true
          title: 'Generate Baseline'
          body: |
            Generate of all build flavor
            - Foss Reliant
            - Play Store
          labels: |
            report
            baseline