name: 'Build and Deploy Vue3'

on:
  push:
    branches:
      - deploy

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Install pnpm
        run: |
          npm install -g pnpm

      - name: Install dependencies
        run: |
          pnpm install --ignore-scripts
          pnpm rebuild

      - name: 替换域名
        run: |
          find . -type f -exec sed -i 's|apiservertest.s1f.ren|cloudapi.007yjs.com|g' {} +   

      - name: Build project
        run: |
          pnpm build

      - name: Compress artifacts
        run: |
          tar -czf dist.tar.gz -C ./dist .

      - name: Save artifact
        uses: actions/upload-artifact@v3
        with:
          name: vue3-build
          path: dist.tar.gz

  deploy:
    needs: build
    runs-on: ninBo
    steps:
      - name: Download Artifact
        uses: https://gitea.s1f.ren/actions/download-artifact@v3
        with:
          name: vue3-build

      - name: Set up SSH
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.PUBLICT_PRIVATE_KEY }}" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan -H ${{ vars.WEB_SERVICE_SERVER_IP }} >> ~/.ssh/known_hosts

      - name: Deploy to server
        run: |
          DEPLOY_DIR="/home/www/web-online/admin.007yjs.com/"
          ssh ${{ vars.ROOT_USER_NAME }}@${{ vars.WEB_SERVICE_SERVER_IP }} "mkdir -p $DEPLOY_DIR"
          scp -o StrictHostKeyChecking=no dist.tar.gz ${{ vars.ROOT_USER_NAME }}@${{ vars.WEB_SERVICE_SERVER_IP }}:$DEPLOY_DIR
          ssh ${{ vars.ROOT_USER_NAME }}@${{ vars.WEB_SERVICE_SERVER_IP }} "cd $DEPLOY_DIR && tar -xzf dist.tar.gz && rm -f dist.tar.gz"
