name: Release build - official

on:
  push:
    tags:
      - "*"

jobs:
  prepare:
    name: Prepare build environment
    runs-on: self-hosted
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
          clean: false
      - name: Set executable permissions to gradlew
        run: chmod +x ./gradlew
      - name: Setup Python
        uses: actions/setup-python@v2
      - name: Install packages
        uses: BSFishy/pip-action@v1
        with:
          packages: |
            python-dotenv
            pycryptodome
            mysql-connector-python
            requests
      - name: Write signing passwords
        env:
          DB_HOST: ${{ secrets.DB_HOST }}
          DB_USER: ${{ secrets.DB_USER }}
          DB_PASS: ${{ secrets.DB_PASS }}
          DB_NAME: ${{ secrets.DB_NAME }}
        run: python $GITHUB_WORKSPACE/.github/utils/sign.py $GITHUB_WORKSPACE commit
  build:
    name: Build APK
    runs-on: self-hosted
    needs:
      - prepare
    outputs:
      androidHome: ${{ env.ANDROID_HOME }}
      androidSdkRoot: ${{ env.ANDROID_SDK_ROOT }}
    steps:
      - name: Setup JDK 11
        uses: actions/setup-java@v2
        with:
          distribution: 'temurin'
          java-version: '11'
      - name: Setup Android SDK
        uses: android-actions/setup-android@v2
      - name: Clean build artifacts
        run: |
          rm -rf app/release/*
          rm -rf app/build/outputs/apk/*
          rm -rf app/build/outputs/bundle/*
      - name: Assemble official release with Gradle
        uses: gradle/gradle-build-action@v2
        with:
          arguments: assembleOfficialRelease
  sign:
    name: Sign APK
    runs-on: self-hosted
    needs:
      - build
    outputs:
      signedReleaseFile: ${{ steps.artifacts.outputs.signedReleaseFile }}
      signedReleaseFileRelative: ${{ steps.artifacts.outputs.signedReleaseFileRelative }}
    steps:
      - name: Sign build artifacts
        id: sign_app
        uses: r0adkll/sign-android-release@v1
        with:
          releaseDirectory: app/release
          signingKeyBase64: ${{ secrets.KEY_STORE }}
          alias: ${{ secrets.KEY_ALIAS }}
          keyStorePassword: ${{ secrets.KEY_STORE_PASSWORD }}
          keyPassword: ${{ secrets.KEY_ALIAS_PASSWORD }}
        env:
          ANDROID_HOME: ${{ needs.build.outputs.androidHome }}
          ANDROID_SDK_ROOT: ${{ needs.build.outputs.androidSdkRoot }}
          BUILD_TOOLS_VERSION: "30.0.2"
      - name: Rename signed artifacts
        id: artifacts
        run: python $GITHUB_WORKSPACE/.github/utils/rename_artifacts.py $GITHUB_WORKSPACE
  publish:
    name: Publish APK
    runs-on: self-hosted
    needs:
      - sign
    steps:
      - name: Setup Python
        uses: actions/setup-python@v2

      - name: Extract changelogs
        id: changelog
        run: python $GITHUB_WORKSPACE/.github/utils/extract_changelogs.py $GITHUB_WORKSPACE

      - name: Upload APK to SFTP
        uses: easingthemes/ssh-deploy@v2.1.6
        env:
          REMOTE_HOST: ${{ secrets.SSH_IP }}
          REMOTE_USER: ${{ secrets.SSH_USERNAME }}
          SSH_PRIVATE_KEY: ${{ secrets.SSH_KEY }}
          SOURCE: ${{ needs.sign.outputs.signedReleaseFileRelative }}
          TARGET: ${{ secrets.SSH_PATH_RELEASE }}
      - name: Save version metadata
        env:
          DB_HOST: ${{ secrets.DB_HOST }}
          DB_USER: ${{ secrets.DB_USER }}
          DB_PASS: ${{ secrets.DB_PASS }}
          DB_NAME: ${{ secrets.DB_NAME }}
          APK_SERVER_RELEASE: ${{ secrets.APK_SERVER_RELEASE }}
          APK_SERVER_NIGHTLY: ${{ secrets.APK_SERVER_NIGHTLY }}
        run: python $GITHUB_WORKSPACE/.github/utils/save_version.py $GITHUB_WORKSPACE

      - name: Distribute to App Distribution
        uses: wzieba/Firebase-Distribution-Github-Action@v1
        with:
          appId: ${{ secrets.FIREBASE_APP_ID }}
          token: ${{ secrets.FIREBASE_TOKEN }}
          groups: ${{ secrets.FIREBASE_GROUPS_RELEASE }}
          file: ${{ needs.sign.outputs.signedReleaseFile }}
          releaseNotesFile: ${{ steps.changelog.outputs.changelogPlainTitledFile }}
      - name: Release on GitHub
        uses: softprops/action-gh-release@v1
        with:
          name: ${{ steps.changelog.outputs.changelogTitle }}
          body_path: ${{ steps.changelog.outputs.changelogMarkdownFile }}
          files: ${{ needs.sign.outputs.signedReleaseFile }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Post Discord webhook
        env:
          APK_FILE: ${{ needs.sign.outputs.signedReleaseFile }}
          APK_SERVER_RELEASE: ${{ secrets.APK_SERVER_RELEASE }}
          APK_SERVER_NIGHTLY: ${{ secrets.APK_SERVER_NIGHTLY }}
          WEBHOOK_RELEASE: ${{ secrets.WEBHOOK_RELEASE }}
          WEBHOOK_TESTING: ${{ secrets.WEBHOOK_TESTING }}
        run: python $GITHUB_WORKSPACE/.github/utils/webhook_discord.py $GITHUB_WORKSPACE

      - name: Upload workflow artifact
        uses: actions/upload-artifact@v2
        if: true
        with:
          name: ${{ steps.changelog.outputs.appVersionName }}
          path: |
            app/release/whatsnew*/
            app/release/*.apk
            app/release/*.aab
            app/release/*.json
            app/release/*.txt
