.github/workflows/android.yml

# Builds the Android app into an APK signed with the committed sideload key (docs/android.md). On a release the APK is
# attached to it. Runs only when the app or its workflow changes.
name: Android

on:
  pull_request:
    paths: [apps/android/**, .github/workflows/android.yml]
  push:
    branches: [main]
    paths: [apps/android/**, .github/workflows/android.yml]
  workflow_dispatch:
    inputs:
      tag:
        description: Release tag to attach the APK to, empty to only build
        type: string
        default: ""
  # ci.yml calls this after it creates a release (a release made with the default token starts no workflow of its own).
  workflow_call:
    inputs:
      tag:
        type: string
        default: ""

permissions:
  contents: write

jobs:
  build:
    runs-on: ubuntu-24.04
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 17
      - uses: gradle/actions/setup-gradle@v4
        with:
          gradle-version: 8.10.2
      - name: Build the APK
        run: |
          VERSION=$(node -p "require('./package.json').version")
          CODE=$(node -p "const [a,b,c]=require('./package.json').version.split('.').map(Number); a*10000+b*100+c")
          gradle -p apps/android assembleRelease -PversionName="$VERSION" -PversionCode="$CODE"
          cp apps/android/app/build/outputs/apk/release/app-release.apk lodesong-android.apk
      - uses: actions/upload-artifact@v7
        with:
          name: lodesong-android
          path: lodesong-android.apk
      - name: Attach to the release
        if: inputs.tag != ''
        env:
          GH_TOKEN: ${{ github.token }}
          TAG: ${{ inputs.tag }}
        run: gh release upload "$TAG" lodesong-android.apk --clobber