name: main

on:
  push:
    branches: [main]

jobs:
  main:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: 构建镜像
        run: |
          docker build -t react-app .
          docker save react-app | gzip > app.tar.gz

      - name: 复制到服务器并运行
        uses: appleboy/scp-action@v0.1.5
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          key: ${{ secrets.SSH_KEY }}
          source: "app.tar.gz"
          target: "/tmp/"

      - name: 部署
        uses: appleboy/ssh-action@v1.0.3
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          key: ${{ secrets.SSH_KEY }}
          script: |
            gunzip -c /tmp/app.tar.gz | docker load
            docker stop react-app 2>/dev/null || true
            docker rm react-app 2>/dev/null || true
            docker run -d --name react-app -p 8080:80 --restart always react-app
            rm -f /tmp/app.tar.gz
