name: Build Release

on:
  push:
    branches:
      - master
    paths:
      - 'CHANGELOG.md'
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Remove old apk
        run: |
          rm -rf ./release/*

      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: 21

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

      - name: Build With Gradle
        run: |
          chmod +x gradlew
          ./gradlew assemblerelease --build-cache --parallel  --warning-mode all
      - name: Get VerName
        run: |
          echo "ver_name=$(grep 'versionName' release/output-metadata.json | cut -d\" -f4)" >> $GITHUB_ENV
      - name: Upload App To Artifact
        uses: actions/upload-artifact@v4
        with:
          name: TTS_${{ env.ver_name }}
          path: ${{ github.workspace }}/release/*
      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          name: TTS_${{ env.ver_name }}
          tag_name: ${{ env.ver_name }}
          body_path: ${{ github.workspace }}/CHANGELOG.md
          draft: false
          prerelease: false
          files: ${{ github.workspace }}/release/*.apk
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Git Push to release
        run: |
          cd ./release
          git init
          git checkout -b release
          git config --global user.name "github-actions[bot]"
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git remote add origin "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}"
          git add *.apk
          git add *.json
          git commit -m "${{ env.ver_name }}"
          git push -f -u origin release
