name: Deploy to Server
on:
  push:
    branches: [master]
  workflow_dispatch: {}

jobs:
  build:
    runs-on:
      labels: mac-mini-2018

    env:
      APP_NAME: fucai-claim
      BUILD_NAME: fucai-claim-${{ github.run_number }}-${{ github.sha }}
      SSH_USER: root
      SSH_HOST: 123.60.153.169

    steps:
      - uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Install deps & Build site
        run: |
          corepack enable
          pnpm install --frozen-lockfile
          pnpm build

      - name: Archive dist
        run: |
          tar -czf ${{ env.BUILD_NAME }}.tar.gz -C dist .

      - name: Upload to server
        run: |
          scp ${{ env.BUILD_NAME }}.tar.gz ${{ env.SSH_USER }}@${{ env.SSH_HOST }}:/tmp/

      - name: Remote deploy
        run: |
          ssh ${{ env.SSH_USER }}@${{ env.SSH_HOST }} <<'SH'
          set -e
          cd /var/www/
          rm -rf ${{ env.APP_NAME }}
          mkdir -p ${{ env.APP_NAME }}
          tar -xzf /tmp/${{ env.BUILD_NAME }}.tar.gz -C ${{ env.APP_NAME }}
          rm /tmp/${{ env.BUILD_NAME }}.tar.gz
          SH
