name: Deploy Quartz

on:
  push:
    branches:
      - master # 본인이 사용하는 메인 브랜치명으로 수정하세요

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

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

      - name: Install & Build
        run: |
          npm ci
          # 1. 빌드 전 확실히 초기화
          rm -rf public
          npx quartz build
          # 2. 'public' 폴더 안으로 들어가서 내용물만 압축 (폴더 자체 제외)
          cd public && tar -czf ../deploy.tar.gz . && cd ..

      - name: Deploy to GCE
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.GCE_HOST }}
          username: ${{ secrets.GCE_USERNAME }}
          key: ${{ secrets.GCE_SSH_KEY }}
          source: "deploy.tar.gz"
          target: "/home/dihwang/white-smith-blog"

      - name: Extract Archive on GCE
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.GCE_HOST }}
          username: ${{ secrets.GCE_USERNAME }}
          key: ${{ secrets.GCE_SSH_KEY }}
          script: |
            cd /home/dihwang/white-smith-blog/public
            # 3. 서버에 남아있는 유령 파일들 싹 제거 (매우 중요)
            find . -mindepth 1 -delete
            # 4. 압축 해제
            tar -xzf ../deploy.tar.gz
            rm ../deploy.tar.gz