name: Release Build

on:
  push:
    tags:
      - "v*"

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set env
        run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

      - name: Set up Java
        run: echo "JAVA_HOME=$JAVA_HOME_17_X64" >> $GITHUB_ENV

      - name: Build with Gradle
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: ./gradlew assembleRelease --no-daemon

      - name: Sign APK
        id: sign_apk
        uses: ilharp/sign-android-release@v1
        with:
          releaseDir: ./app/build/outputs/apk/release/
          signingKey: ${{ secrets.SIGNING_KEYSTORE }}
          keyStorePassword: ${{ secrets.SIGNING_KEYSTORE_PASSWORD }}
          keyAlias: ${{ secrets.SIGNING_KEY_ALIAS }}
          keyPassword: ${{ secrets.SIGNING_KEY_PASSWORD }}

      - name: Add version to APK
        run: mv ${{ steps.sign_apk.outputs.signedFile }} revanced-manager-${{ env.RELEASE_VERSION }}.apk

      - name: Publish release APK
        uses: "marvinpinto/action-automatic-releases@latest"
        with:
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          prerelease: false
          files: revanced-manager-${{ env.RELEASE_VERSION }}.apk
