name: App / Publish

on:
  push:
    tags:
      - v*

jobs:
  publish:
    name: Publish
    runs-on: ubuntu-22.04
    if: ${{ contains(github.repository_owner, 'jellyfin') }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
      - name: Setup Java
        uses: actions/setup-java@99b8673ff64fbf99d8d325f52d9a5bdedb8483e9 # v4.2.1
        with:
          distribution: temurin
          java-version: 17
      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@dbbdc275be76ac10734476cc723d82dfe7ec6eda # v3.4.2
      - name: Set JELLYFIN_VERSION
        run: echo "JELLYFIN_VERSION=$(echo ${GITHUB_REF#refs/tags/v} | tr / -)" >> $GITHUB_ENV
      - name: Assemble release files
        run: ./gradlew assemble bundleProprietaryRelease versionTxt
      - name: Sign libre APK
        id: libreSign
        uses: r0adkll/sign-android-release@349ebdef58775b1e0d8099458af0816dc79b6407 # tag=v1
        env:
          BUILD_TOOLS_VERSION: "34.0.0"
        with:
          releaseDirectory: app/build/outputs/apk/libre/release
          signingKeyBase64: ${{ secrets.KEYSTORE }}
          keyStorePassword: ${{ secrets.KEYSTORE_PASSWORD }}
          alias: ${{ secrets.KEY_ALIAS }}
          keyPassword: ${{ secrets.KEY_PASSWORD }}
      - name: Sign proprietary APK
        id: proprietarySign
        uses: r0adkll/sign-android-release@349ebdef58775b1e0d8099458af0816dc79b6407 # tag=v1
        env:
          BUILD_TOOLS_VERSION: "34.0.0"
        with:
          releaseDirectory: app/build/outputs/apk/proprietary/release
          signingKeyBase64: ${{ secrets.KEYSTORE }}
          keyStorePassword: ${{ secrets.KEYSTORE_PASSWORD }}
          alias: ${{ secrets.KEY_ALIAS }}
          keyPassword: ${{ secrets.KEY_PASSWORD }}
      - name: Sign proprietary app bundle
        id: proprietaryBundleSign
        uses: r0adkll/sign-android-release@349ebdef58775b1e0d8099458af0816dc79b6407 # tag=v1
        env:
          BUILD_TOOLS_VERSION: "34.0.0"
        with:
          releaseDirectory: app/build/outputs/bundle/proprietaryRelease
          signingKeyBase64: ${{ secrets.KEYSTORE }}
          keyStorePassword: ${{ secrets.KEYSTORE_PASSWORD }}
          alias: ${{ secrets.KEY_ALIAS }}
          keyPassword: ${{ secrets.KEY_PASSWORD }}
      - name: Prepare release archive
        run: |
          mkdir -p build/jellyfin-publish
          mv app/build/outputs/apk/*/*/jellyfin-android-*-libre-debug.apk build/jellyfin-publish/
          mv app/build/outputs/apk/*/*/jellyfin-android-*-proprietary-debug.apk build/jellyfin-publish/
          mv app/build/outputs/apk/*/*/jellyfin-android-*-libre-release-unsigned.apk build/jellyfin-publish/
          mv app/build/outputs/apk/*/*/jellyfin-android-*-proprietary-release-unsigned.apk build/jellyfin-publish/
          mv ${{ steps.libreSign.outputs.signedReleaseFile }} build/jellyfin-publish/jellyfin-android-v${{ env.JELLYFIN_VERSION }}-libre-release.apk
          mv ${{ steps.proprietarySign.outputs.signedReleaseFile }} build/jellyfin-publish/jellyfin-android-v${{ env.JELLYFIN_VERSION }}-proprietary-release.apk
          mv ${{ steps.proprietaryBundleSign.outputs.signedReleaseFile }} build/jellyfin-publish/jellyfin-android-v${{ env.JELLYFIN_VERSION }}-proprietary-release.aab
          mv app/build/version.txt build/jellyfin-publish/
      - name: Upload release archive to GitHub release
        uses: alexellis/upload-assets@13926a61cdb2cb35f5fdef1c06b8b591523236d3 # 0.4.1
        env:
          GITHUB_TOKEN: ${{ secrets.JF_BOT_TOKEN }}
        with:
          asset_paths: '["build/jellyfin-publish/*"]'
      - name: Upload release archive to repo.jellyfin.org
        uses: burnett01/rsync-deployments@796cf0d5e4b535745ce49d7429f77cf39e25ef39 # 7.0.1
        with:
          switches: -vrptz
          path: build/jellyfin-publish/
          remote_path: /srv/incoming/android/v${{ env.JELLYFIN_VERSION }}/
          remote_host: ${{ secrets.REPO_HOST }}
          remote_user: ${{ secrets.REPO_USER }}
          remote_key: ${{ secrets.REPO_KEY }}
      - name: Update repo.jellyfin.org symlinks
        uses: appleboy/ssh-action@029f5b4aeeeb58fdfe1410a5d17f967dacf36262 # v1.0.3
        with:
          host: ${{ secrets.REPO_HOST }}
          username: ${{ secrets.REPO_USER }}
          key: ${{ secrets.REPO_KEY }}
          envs: JELLYFIN_VERSION
          script_stop: true
          script: |
            if [ -d "/srv/repository/main/client/android/versions/v${{ env.JELLYFIN_VERSION }}" ] && [ -n "${{ env.JELLYFIN_VERSION }}" ]; then
                sudo rm -r /srv/repository/main/client/android/versions/v${{ env.JELLYFIN_VERSION }}
            fi
            sudo mv /srv/incoming/android/v${{ env.JELLYFIN_VERSION }} /srv/repository/main/client/android/versions/v${{ env.JELLYFIN_VERSION }}
            cd /srv/repository/main/client/android;
            sudo rm -rf *.apk version.txt;
            sudo ln -s versions/v${JELLYFIN_VERSION}/jellyfin-android-v${JELLYFIN_VERSION}-*.apk .;
            sudo ln -s versions/v${JELLYFIN_VERSION}/version.txt .;
