name: "\U0001F680 Deploy mobile staging OTA update"

on:
  workflow_dispatch:

jobs:
  extract_branch_name:
    name: "\U0001F3D7 Extract branch name"
    runs-on: ubuntu-latest
    outputs:
      branch: ${{steps.extract_branch.outputs.branch}}
    steps:
      - name: "\U0001F3D7 Extract branch name"
        shell: bash
        run: echo "branch=$(echo ${GITHUB_REF##*/} | tr '[A-Z]' '[a-z]')" >> "$GITHUB_OUTPUT"
        id: extract_branch

  deploy_staging_ota:
    name: "\U0001F680 Deploy staging OTA update"
    needs: [extract_branch_name]
    runs-on: ubuntu-latest
    env:
      working-directory: ./apps/mobile
    steps:
      - uses: actions/checkout@v6
        with:
          ref: ${{ github.event_name == 'workflow_dispatch' && 'staging' || github.sha }}
          fetch-depth: 0
      - uses: actions/setup-node@v6
        with:
          node-version: 24
      - uses: pnpm/action-setup@v4
      - name: "\U0001F3D7 Expose git commit data"
        uses: jcputney/git-commit-data-action@v2
      - name: "\U0001F4E6 Install Packages"
        run: pnpm install --frozen-lockfile
        working-directory: ${{ env.working-directory }}
      - name: Generate dotenv file
        working-directory: ${{ env.working-directory }}
        env:
          environ: '{"EXPO_PUBLIC_NODE_ENV": "staging"}'
        run: |
          ../../.github/scripts/create-env.sh "$environ"
      - uses: expo/expo-github-action@v8
        with:
          expo-cache: true
          expo-version: latest
          eas-version: latest
          token: ${{ secrets.EXPO_TOKEN }}
      - name: "\U0001F680 Deploy staging OTA update"
        run: |
          eas update -p ios --branch staging --message "${{ env.GIT_COMMIT_MESSAGE }} ${{ needs.extract_branch_name.outputs.branch }}" --non-interactive
          eas update -p android --branch staging --message "${{ env.GIT_COMMIT_MESSAGE }} ${{ needs.extract_branch_name.outputs.branch }}" --non-interactive
        working-directory: ${{ env.working-directory }}
