name: build-release

on:
  push:
    tags:
      - v*

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: recursive
      - run: |
          rm submodules/ExoPlayer/gradle/wrapper/gradle-wrapper.jar
      - uses: actions/setup-node@v2
        with:
          node-version: '14'
          cache: 'yarn'
      - name: Get subtracks version
        run: |
          echo "SUBTRACKS_VERSION=$(node -e "console.log(require('./package.json').version);")" >> $GITHUB_ENV
      - run: |
          yarn install
      - run: |
          yarn license
      - run: |
          scripts/prepare-exoplayer-ffmpeg.sh
      - uses: gradle/wrapper-validation-action@v1
      - name: Retrieve keystore file
        env:
          SUBTRACKS_KEYSTORE_BASE64: ${{ secrets.SUBTRACKS_KEYSTORE_BASE64 }}
        run: |
          echo "$SUBTRACKS_KEYSTORE_BASE64" | base64 --decode > android/app/subtracks.keystore
      - name: Build release APK
        env:
          SUBTRACKS_UPLOAD_KEY_PASSWORD: ${{ secrets.SUBTRACKS_UPLOAD_KEY_PASSWORD }}
          SUBTRACKS_UPLOAD_STORE_PASSWORD: ${{ secrets.SUBTRACKS_UPLOAD_STORE_PASSWORD }}
        run: >
          ./android/gradlew assembleRelease
          -p android
          -PSUBTRACKS_UPLOAD_STORE_FILE=subtracks.keystore
          -PSUBTRACKS_UPLOAD_KEY_ALIAS=subtracks
          -PSUBTRACKS_UPLOAD_KEY_PASSWORD="$SUBTRACKS_UPLOAD_KEY_PASSWORD"
          -PSUBTRACKS_UPLOAD_STORE_PASSWORD="$SUBTRACKS_UPLOAD_STORE_PASSWORD"
      - run: |
          mkdir bin
      - run: >
          mv android/app/build/outputs/apk/release/app-release.apk
          bin/subtracks-${SUBTRACKS_VERSION}.apk
      - name: Build release AAB
        env:
          SUBTRACKS_UPLOAD_KEY_PASSWORD: ${{ secrets.SUBTRACKS_UPLOAD_KEY_PASSWORD }}
          SUBTRACKS_UPLOAD_STORE_PASSWORD: ${{ secrets.SUBTRACKS_UPLOAD_STORE_PASSWORD }}
        run: >
          ./android/gradlew bundleRelease
          -p android
          -PSUBTRACKS_UPLOAD_STORE_FILE=subtracks.keystore
          -PSUBTRACKS_UPLOAD_KEY_ALIAS=subtracks
          -PSUBTRACKS_UPLOAD_KEY_PASSWORD="$SUBTRACKS_UPLOAD_KEY_PASSWORD"
          -PSUBTRACKS_UPLOAD_STORE_PASSWORD="$SUBTRACKS_UPLOAD_STORE_PASSWORD"
      - run: >
          mv android/app/build/outputs/bundle/release/app-release.aab
          bin/subtracks-${SUBTRACKS_VERSION}.aab
      - uses: actions/upload-artifact@v2
        with:
          name: subtracks-release
          path: bin/*
