name: Sign

on:
  push:
    branches:
      - master

jobs:
  sign:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 17

      - name: Setup Gradle
        uses: gradle/gradle-build-action@v3

      - name: Setup Android SDK
        uses: android-actions/setup-android@v3

      - name: Restore keystore
        env:
          MEALIENT_KEY_STORE: ${{ secrets.MEALIENT_KEY_STORE }}
          MEALIENT_KEY_STORE_PASSWORD: ${{ secrets.MEALIENT_KEY_STORE_PASSWORD }}
          MEALIENT_KEY_ALIAS: ${{ secrets.MEALIENT_KEY_ALIAS }}
          MEALIENT_KEY_PASSWORD: ${{ secrets.MEALIENT_KEY_PASSWORD }}
        run: |
          echo "$MEALIENT_KEY_STORE" | base64 -d > app/keystore.jks
          echo "storeFile=keystore.jks" > keystore.properties
          echo "storePassword=$MEALIENT_KEY_STORE_PASSWORD" >> keystore.properties
          echo "keyAlias=$MEALIENT_KEY_ALIAS" >> keystore.properties
          echo "keyPassword=$MEALIENT_KEY_PASSWORD" >> keystore.properties

      - name: APK
        run: |
          ./gradlew build
          cp app/build/outputs/apk/release/*.apk mealient-release.apk

      - name: Bundle
        run: |
          ./gradlew bundle
          cp app/build/outputs/bundle/release/*.aab mealient-release.aab

      - name: Upload release build
        uses: actions/upload-artifact@v4
        with:
          name: Release build
          path: |
            mealient-release.apk
            mealient-release.aab

      - name: SonarCloud
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        run: ./gradlew sonar