name: Build and Deploy

on:
  push:
    branches:
      - main
  schedule:
    - cron: '0 0 * * *'
      timezone: 'Europe/Berlin'
  workflow_dispatch:

jobs:
  build-and-deploy:
    runs-on: self-hosted

    container:
      image: ghcr.io/gohugoio/hugo:latest
      options: --user root
      env:
        HUGO_ENVIRONMENT: production

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        # with:
        #   submodules: true  # Required if your theme is a Git submodule
        #   fetch-depth: 0    # Hugo uses full history for .GitInfo and lastmod

      - name: Install git and git-lfs
        run: |
          apk add --no-cache git git-lfs
          git lfs install

      - name: Pull LFS objects
        run: git lfs pull

      - name: Install Bootstrap/Sass
        run: |
          npm install bootstrap sass
          mkdir -p themes/bootstrap-theme/assets/scss/bootstrap
          cp -r node_modules/bootstrap/scss/. themes/bootstrap-theme/assets/scss/bootstrap

      - name: Update modules
        run: hugo mod get -u

      - name: Build site
        env:
          HUGO_GIT_BRANCH: ${{ gitea.ref_name }}
        run: hugo --minify

      - name: Install rsync and openssh-client
        run: |
          apk add --no-cache rsync openssh-client
          # Hugo's official image is Alpine-based; use apk.
          # If a Debian-based Hugo image is used, swap to:
          # apt-get update && apt-get install -y rsync openssh-client

      - name: Set up SSH key
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          echo "${{ secrets.DEPLOY_KNOWN_HOSTS }}" > ~/.ssh/known_hosts
          chmod 644 ~/.ssh/known_hosts

      - name: Deploy via rsync
        run: |
          rsync -avz --delete \
            -e "ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=yes" \
            public/ \
            ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:/${{ secrets.DEPLOY_SUBDIR }}
