name: Publish

on:
  push:
    tags:
      - v*

jobs:
  publish:
    name: Publish
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.3.0
          bundler-cache: true

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

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

      - name: Set up Gradle
        uses: gradle/actions/setup-gradle@v3

      - name: Decode keystore file
        uses: timheuer/base64-to-file@v1
        id: findroid_keystore
        with:
          fileName: 'findroid-keystore.jks'
          encodedString: ${{ secrets.FINDROID_KEYSTORE }}

      - name: Decode Play API credentials file
        uses: timheuer/base64-to-file@v1
        id: findroid_play_api_credentials
        with:
          fileName: 'findroid-play-api-credentials.json'
          encodedString: ${{ secrets.FINDROID_PLAY_API_CREDENTIALS }}

      - name: Build and publish
        run: bundle exec fastlane publish
        env:
          FINDROID_KEYSTORE: ${{ steps.findroid_keystore.outputs.filePath }}
          FINDROID_KEYSTORE_PASSWORD: ${{ secrets.FINDROID_KEYSTORE_PASSWORD }}
          FINDROID_KEY_ALIAS: ${{ secrets.FINDROID_KEY_ALIAS }}
          FINDROID_KEY_PASSWORD: ${{ secrets.FINDROID_KEY_PASSWORD }}
          FINDROID_PLAY_API_CREDENTIALS: ${{ steps.findroid_play_api_credentials.outputs.filePath }}

      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          draft: true
          files: |
            ./app/phone/build/outputs/apk/libre/release/findroid-${{ github.ref_name }}-libre-arm64-v8a.apk
            ./app/phone/build/outputs/apk/libre/release/findroid-${{ github.ref_name }}-libre-armeabi-v7a.apk
            ./app/phone/build/outputs/apk/libre/release/findroid-${{ github.ref_name }}-libre-x86_64.apk
            ./app/phone/build/outputs/apk/libre/release/findroid-${{ github.ref_name }}-libre-x86.apk
