## See https://github.com/flutter/gallery/blob/main/.github/workflows/release_deploy_play_store.yml

name: Release on Play Store
on:
  # Enable manual run
  workflow_dispatch:
    inputs:
      track:
        description: "Track"
        type: choice
        default: beta
        options: 
          - internal
          - alpha
          - beta
          - production

      is_draft:
        description: "Mark as draft"
        required: true
        default: true
        type: boolean

# Declare default permissions as read only.
permissions: read-all

jobs:
  fastlane-deploy-to-play-store:
    name: "Deploy to Google Play Store"
    runs-on: ubuntu-latest
    steps:

      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          submodules: recursive
          
      - name: Set FLUTTER_ROOT environment variable
        run: echo "FLUTTER_ROOT=$(pwd)/flutter/bin" >> $GITHUB_ENV
          
      - run: $FLUTTER_ROOT/flutter doctor -v

      - name: Setup signing config
        uses: ./.github/actions/setup-android-signing-config
        with:
          keystore-key-alias: ${{ secrets.KEYSTORE_KEY_ALIAS }}
          keystore-key-password: ${{ secrets.KEYSTORE_KEY_PASSWORD }}
          keystore-store-password: ${{ secrets.KEYSTORE_STORE_PASSWORD }}
          release-keystore: ${{ secrets.RELEASE_KEYSTORE }}
          release-keystore-passphrase: ${{ secrets.RELEASE_KEYSTORE_PASSPHRASE }}

      - name: Get packages and run codegen
        run: |
          bash install_dependencies_with_submodule.sh
          bash update_changelogs.sh
        working-directory: scripts

      - name: Setup Fastlane
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: "2.6"
          bundler-cache: true
          working-directory: android

      # Build and deploy with Fastlane
      - run: bundle exec fastlane publish_in_play_store is_draft:${{ github.event.inputs.is_draft }} track:${{ github.event.inputs.track }}
        env:
          PLAY_STORE_CREDENTIALS: ${{ secrets.PLAY_STORE_CREDENTIALS }}
        working-directory: android
  