name: Publish App

on:
  push:
    branches-ignore:
      - '*'
      - '*/**'
    tags:
      - '*'
      - '*/**'

jobs:
  publish:

    runs-on: ubuntu-18.04

    steps:
      - uses: actions/checkout@v1

      - name: Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11

      - name: Decrypt files
        env:
          keystore: ${{ secrets.keystore }}
          google_json: ${{ secrets.google_json }}
          ssh_key: ${{ secrets.ssh_key }}
        run: |
          echo "$google_json" | base64 -d > google.json
          echo "$keystore" | base64 -d > kiwix-android.keystore
          echo "$ssh_key" | base64 -d > ssh_key
          chmod 600 ssh_key

      - name: Set tag variable
        run: echo "TAG=$(echo ${GITHUB_REF:10})" >> $GITHUB_ENV

      - name: Publish app to download.kiwix.org
        env:
          KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
          KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
          KEY_STORE_PASSWORD: ${{ secrets.KEY_STORE_PASSWORD }}
          UNIVERSAL_RELEASE_APK: app/build/outputs/apk/release/*universal*.apk
        run: |
          ./gradlew assembleRelease
          cp $UNIVERSAL_RELEASE_APK kiwix-${TAG}.apk
          scp -P 30022 -vrp -i ssh_key -o StrictHostKeyChecking=no kiwix-${TAG}.apk ci@master.download.kiwix.org:/data/download/release/kiwix-android/

      - name: Publish  versionInfo to download.kiwix.org
        run: |
          ./gradlew generateVersionCodeAndName
          scp -P 30022 -vrp -i ssh_key -o StrictHostKeyChecking=no VERSION_INFO ci@master.download.kiwix.org:/data/download/release/kiwix-android/

      - name: Publish to github releases
        uses: ncipollo/release-action@v1
        with:
          artifacts: "app/build/outputs/apk/release/**"
          token: ${{ secrets.GITHUB_TOKEN }}
          draft: true
          prerelease: true


      - name: Publish app to play store
        env:
          KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
          KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
          KEY_STORE_PASSWORD: ${{ secrets.KEY_STORE_PASSWORD }}
        run: ./gradlew publishPlayStoreBundle --scan
