name: Build and Deploy Zola Site

on:
  push:
    branches:
      - main  # Or whichever branch you want to deploy from

jobs:
  #----------------------------------------------------
  # JOB 1: Build the static site with Zola
  #----------------------------------------------------
  build:
    runs-on: ubuntu-latest
    steps:
      # Step 1: Check out the main website repository's code
      - name: Checkout Website Source
        uses: actions/checkout@v4
        with:
          path: main-site

      # Step 2: Check out the theme repository into the correct folder
      # Replace 'YourUsername/YourThemeRepo' with your actual Gitea theme repository
      - name: Checkout Theme
        uses: actions/checkout@v4
        with:
          repository: finmoon/moon-anemone
          path: main-site/themes/anemone
          token: ${{ secrets.ANEMONE_TOKEN }}

      - name: Install Zola
        uses: taiki-e/install-action@v2
        with:
          tool: zola


      # Step 4: Run the build command
      # Zola will generate the site in the 'main-site/public' directory
      - name: Build Site
        run: |
          cd main-site
          zola -V
          zola build

      # Step 5: Upload the 'public' directory as an artifact
      # This makes the build output available to other jobs (like our deploy job)
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: zola-public
          path: main-site/public/

  #----------------------------------------------------
  # JOB 2: Deploy the built site to the web server
  #----------------------------------------------------
  deploy:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Download artifact
        uses: actions/download-artifact@v3
        with:
          name: zola-public

      # This debugging step is very useful to confirm the file structure
      - name: List files in workspace
        run: ls -laR

      - name: SCP files to server
        uses: appleboy/scp-action@master
        working-directory: ./zola-public
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          source: "."
          target: "/var/www/html/finmoon"
          rm: true