name: Deploy GCP on Tagging

on:
  push:
    tags:
      - '*'  # Runs when any tag is pushed

jobs:
  release:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Google Cloud SDK
        uses: google-github-actions/auth@v1
        with:
          credentials_json: ${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}

      - name: Configure gsutil
        run: gcloud auth activate-service-account --key-file <(echo '${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}')

      - name: Download file from GCS
        run: gsutil -q cp gs://${{ secrets.GCP_PROJECT_ID_PROD }}-apt-packages/encrypted_gpg.kms encrypted_gpg.kms

      - name: Decrypt file using KMS
        run: |
          gcloud kms decrypt \
            --key gpg \
            --keyring gpg \
            --location global \
            --plaintext-file private.pgp \
            --ciphertext-file encrypted_gpg.kms

      - name: Import GPG
        run: |
          gpg --import private.pgp

      - name: Set up Java
        uses: actions/setup-java@3a4f6e1af504cf6a31855fa899c6aa5355ba6c12
        timeout-minutes: 1
        with:
          distribution: 'temurin'
          java-version: 17
      - name: Set up Gradle
        uses: gradle/gradle-build-action@ac2d340dc04d9e1113182899e983b5400c17cda1
        timeout-minutes: 10
        with:
          gradle-home-cache-cleanup: true
      - name: Set Env
        shell: bash
        run: |
          echo "home=${HOME}" >> "$GITHUB_ENV"
          
      - name: Export Signing Key
        env:
          # The upload key must be exported using `base64 -w 0 <filename.jks>` for use
          # as a Github Secrets value; if the key is exported with standard wrapping,
          # it will fail to import correctly.
          # NOTE: This is the upload signing key, which may be replaced at will, not
          # the application signing key which is escrowed by Google and may only be
          # replaced once a year (and has a bunch of additional hassles associated with
          # replacing it.)
          SIGNING_KEYSTORE_BASE_64: ${{ secrets.UPLOAD_KEYSTORE_BASE_64 }}
          SIGNING_KEY_PATH: ${{ format('{0}/release.jks', env.home) }}
        shell: bash
        run: |
          echo ${SIGNING_KEYSTORE_BASE_64} | base64 --decode > ${SIGNING_KEY_PATH}
          
      - name: Get APK
        timeout-minutes: 25
        env:
          ORG_GRADLE_PROJECT_ZCASH_SUPPORT_EMAIL_ADDRESS: ${{ vars.SUPPORT_EMAIL_ADDRESS }}
          # TODO [#1033]: Use token-based authorization on Google Play for automated deployment
          # TODO [#1033]: https://github.com/Electric-Coin-Company/zashi-android/issues/1033
          # Note that these properties are not currently used due to #1033
          # ORG_GRADLE_PROJECT_ZCASH_GOOGLE_PLAY_SERVICE_ACCOUNT: ${{ secrets.GOOGLE_PLAY_SERVICE_ACCOUNT }}
          # ORG_GRADLE_PROJECT_ZCASH_GOOGLE_PLAY_SERVICE_KEY_FILE_PATH: ${{ steps.auth_google_play.outputs.credentials_file_path }}
          ORG_GRADLE_PROJECT_ZCASH_GOOGLE_PLAY_SERVICE_ACCOUNT_KEY: ${{ secrets.GOOGLE_PLAY_SERVICE_ACCOUNT_KEY }}
          ORG_GRADLE_PROJECT_ZCASH_GOOGLE_PLAY_PUBLISHER_API_KEY: ${{ secrets.GOOGLE_PLAY_PUBLISHER_API_KEY }}
          ORG_GRADLE_PROJECT_ZCASH_GOOGLE_PLAY_DEPLOY_TRACK: internal
          ORG_GRADLE_PROJECT_ZCASH_GOOGLE_PLAY_DEPLOY_STATUS: completed
          ORG_GRADLE_PROJECT_ZCASH_RELEASE_KEYSTORE_PATH: ${{ format('{0}/release.jks', env.home) }}
          ORG_GRADLE_PROJECT_ZCASH_RELEASE_KEYSTORE_PASSWORD: ${{ secrets.UPLOAD_KEYSTORE_PASSWORD }}
          ORG_GRADLE_PROJECT_ZCASH_RELEASE_KEY_ALIAS: ${{ secrets.UPLOAD_KEY_ALIAS }}
          ORG_GRADLE_PROJECT_ZCASH_RELEASE_KEY_ALIAS_PASSWORD: ${{ secrets.UPLOAD_KEY_ALIAS_PASSWORD }}
          ORG_GRADLE_PROJECT_ZCASH_COINBASE_APP_ID: ${{ secrets.COINBASE_APP_ID }}
          ORG_GRADLE_PROJECT_ZCASH_FLEXA_KEY: ${{ secrets.FLEXA_PUBLISHABLE_KEY }}
        run: |
          ./gradlew :app:assembleZcashmainnetStoreDebug :app:bundleZcashmainnetStoreRelease :app:packageZcashmainnetStoreReleaseUniversalApk
          
      - name: Collect Artifacts
        timeout-minutes: 1
        run: |
          ls -la app/build/outputs/apk/*

