name: Baseline profile generation

on:
  # every day at 00:43
  schedule:
    - cron: '43 0 * * *'
  workflow_dispatch:

jobs:
  baseline-profile:
    runs-on: macos-latest
    timeout-minutes: 45
    env:
      TERM: dumb

    steps:
    - name: Checkout repository
      uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
      with:
        token: ${{ secrets.POST_RELEASE_GH_TOKEN }}
        fetch-depth: 0

    - name: Set up JDK
      uses: actions/setup-java@5ffc13f4174014e2d4d4572b3d74c3fa61aeb2c2 # v3.11.0
      with:
        distribution: temurin
        java-version: 18

    - name: Set up Git author
      shell: bash
      run: |
        git config user.name "GitHub Actions"
        git config user.email noreply@github.com

    - name: Setup Gradle caching
      uses: gradle/gradle-build-action@749f47bda3e44aa060e82d7b3ef7e40d953bd629 # v2.4.2
      with:
        gradle-home-cache-cleanup: true
        cache-read-only: true

    - name: Decrypt secrets
      run: |
        ./scripts/setup-age.sh
        ./scripts/signing-setup.sh "$ENCRYPT_KEY"
      env:
        ENCRYPT_KEY: ${{ secrets.ENCRYPT_KEY }}

    # This allows us to build most of what we need without the emulator running
    # and using resources
    - name: Build app and benchmark
      run: ./gradlew :benchmark:assembleBenchmark :android:assembleBenchmark

    - name: Clear unused Gradle Managed Devices
      run: ./gradlew cleanManagedDevices --unused-only

    - name: Run benchmark on Gradle Managed Device
      run: |
        ./gradlew api31BenchmarkAndroidTest \
          -Dorg.gradle.workers.max=1 \
          -Pandroid.testInstrumentationRunnerArguments.androidx.benchmark.enabledRules=BaselineProfile \
          -Pandroid.testoptions.manageddevices.emulator.gpu="swiftshader_indirect"

    # If we're on main branch, copy over the baseline profile and
    # commit it to the repository (if changed)
    - name: Commit baseline profile into main
      if: github.ref == 'refs/heads/main'
      run: |
        # Pull down any changes which may have been committed while this workflow has been running
        git pull
        # Sort the baseline profile, output to android/
        sort -o android/src/main/baseline-prof.txt benchmark/build/outputs/managed_device_android_test_additional_output/api31/BaselineProfileGenerator_generateBaselineProfile-baseline-prof.txt
        # If the baseline profile has changed, commit it
        if [[ $(git diff --stat android/src/main/baseline-prof.txt) != '' ]]; then
          git add android/src/main/baseline-prof.txt
          git commit -m "chore(android): refresh baseline profile" && git push
        fi

    # Upload the entire output folder and attach it to the CI run
    - name: Attach baseline profile
      uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
      with:
        name: Baseline profile output
        path: benchmark/build/outputs/managed_device_android_test_additional_output

    - name: Clean secrets
      if: always()
      run: scripts/signing-cleanup.sh
