name: Deploy

on:
  workflow_run:
    workflows: ["CI"]
    types: [completed]
    branches: [main]
  workflow_dispatch:

concurrency:
  group: deploy
  cancel-in-progress: true

env:
  VITE_SUPABASE_URL: ${{ secrets.VITE_SUPABASE_URL }}
  VITE_SUPABASE_ANON_KEY: ${{ secrets.VITE_SUPABASE_ANON_KEY }}
  VITE_DEPOSIT_TON_ADDRESS: ${{ secrets.VITE_DEPOSIT_TON_ADDRESS }}
  VITE_DEPOSIT_POLKADOT_ADDRESS: ${{ secrets.VITE_DEPOSIT_POLKADOT_ADDRESS }}

jobs:
  deploy:
    name: Deploy to VPS
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - run: npm ci

      - run: npm run build

      # VPS1 (telegram.pezkuwichain.io) is currently running Zagros testnet - deployed manually
      # Re-enable this step when testnet period is over
      # - name: Deploy to telegram.pezkuwichain.io
      #   uses: appleboy/scp-action@v1.0.0
      #   with:
      #     host: ${{ secrets.VPS1_HOST }}
      #     username: ${{ secrets.VPS1_USER }}
      #     key: ${{ secrets.VPS1_SSH_KEY }}
      #     source: 'dist/*'
      #     target: '/var/www/telegram.pezkuwichain.io'
      #     strip_components: 1

      - name: Deploy to telegram.pezkiwi.app
        uses: appleboy/scp-action@v1.0.0
        with:
          host: ${{ secrets.VPS2_HOST }}
          username: ${{ secrets.VPS2_USER }}
          key: ${{ secrets.VPS2_SSH_KEY }}
          source: 'dist/*'
          target: '/var/www/telegram.pezkiwi.app'
          strip_components: 1
