name: Create release
on:
  push:
    tags:

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Fetch tags
        run: git fetch --tags -p
      - uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'
      - name: Setup Android SDK
        uses: android-actions/setup-android@v3
      - name: Configure gradle...
        run: mkdir -p ~/.gradle && echo "org.gradle.daemon=false\nignoreGitVersion=false\nsystemProp.org.gradle.internal.http.connectionTimeout=180000\nsystemProp.org.gradle.internal.http.socketTimeout=180000" >> ~/.gradle/gradle.properties
      - name: Generate APK
        run: ./gradlew :androidApp:assembleGithubConfigRelease
      - name: Get Key
        run: wget ${{ secrets.KEY_URL }}
      - name: Zippalign
        run: $ANDROID_HOME/build-tools/31.0.0/zipalign -f -v 4 androidApp/build/outputs/apk/githubConfig/release/androidApp-githubConfig-release-unsigned.apk androidApp/build/outputs/apk/githubConfig/release/android-prod-released-ziped.apk
      - name: Sigh
        run: $ANDROID_HOME/build-tools/31.0.0/apksigner sign -v --out signed.apk --ks ./key --ks-key-alias ${{ secrets.KEY_ALIAS }} --ks-pass pass:${{ secrets.KEYSTORE_PASSWORD }} --v1-signing-enabled true --v2-signing-enabled true androidApp/build/outputs/apk/githubConfig/release/android-prod-released-ziped.apk
      - name: Verify
        run: $ANDROID_HOME/build-tools/31.0.0/apksigner verify signed.apk
      - name: Release
        uses: https://gitea.com/actions/release-action@main
        with:
          files: signed.apk
          api_key: ${{ secrets.API_KEY }}
      - name: Send mail
        uses: https://github.com/dawidd6/action-send-mail@v4
        with:
          connection_url: ${{ secrets.MAIL_CONNECTION }}
          to: ${{ secrets.MAIL_TO }}
          from: ${{ secrets.MAIL_FROM }}
          subject: Mapping file
          priority: high
          convert_markdown: true
          body: Nouveu fichier de mapping
          attachments: androidApp/build/outputs/mapping/githubConfigRelease/mapping.txt