# SPDX-FileCopyrightText: 2022 yuzu Emulator Project
# SPDX-License-Identifier: GPL-3.0-or-later

name: 'suyu-android-build'

on:
  push:
    tags: [ "*" ]

jobs:
  android:
    runs-on: ubuntu-latest
    if: ${{ github.repository == 'suyu-emu/suyu-android' }}
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive
          fetch-depth: 0
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
      - name: Set up cache
        uses: actions/cache@v3
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
            ~/.ccache
          key: ${{ runner.os }}-android-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-android-
      - name: Query tag name
        uses: olegtarasov/get-tag@v2.1.2
        id: tagName
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y ccache apksigner glslang-dev glslang-tools
      - name: Build
        run: ./.ci/scripts/android/build.sh
        env:
          ANDROID_KEYSTORE_B64: ${{ secrets.ANDROID_KEYSTORE_B64 }}
          ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }}
          ANDROID_KEYSTORE_PASS: ${{ secrets.ANDROID_KEYSTORE_PASS }}
      - name: Copy artifacts
        run: ./.ci/scripts/android/upload.sh
      - name: Upload
        uses: actions/upload-artifact@v3
        with:
          name: android
          path: artifacts/
  # release steps
  release-android:
    runs-on: ubuntu-latest
    needs: [android]
    if: ${{ startsWith(github.ref, 'refs/tags/') }}
    permissions:
      contents: write
    steps:
      - uses: actions/download-artifact@v3
      - name: Query tag name
        uses: olegtarasov/get-tag@v2.1.2
        id: tagName
      - name: Create release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.tagName.outputs.tag }}
          release_name: ${{ steps.tagName.outputs.tag }}
          draft: false
          prerelease: false
      - name: Upload artifacts
        uses: alexellis/upload-assets@0.2.3
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          asset_paths: '["./**/*.apk","./**/*.aab"]'
