name: 自动构建并部署Astro博客

# 触发条件：main分支有代码推送时执行
on:
  push:
    branches: [ main ]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      # 1. 拉取仓库最新代码
      - name: 检出代码
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      # 2. 安装Node.js 20（Astro运行环境）
      - name: 安装Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22
        env:
          NODEJS_ORG_MIRROR: https://npmmirror.com/mirrors/node

      # 3. 安装依赖 + 打包Astro项目（生成dist目录）
      - name: 安装依赖并构建
        run: |
          npm install
          npm run build

      # 4. 把打包好的dist文件同步到服务器Apache目录
      - name: 同步到服务器
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.SERVER_IP }}
          username: ${{ secrets.SERVER_USER }}
          port: ${{ secrets.SERVER_PORT }}
          key: ${{ secrets.SERVER_SSH_KEY }}
          source: "dist/"
          target: "/var/www/blog/"
          overwrite: true
          rm: true