name: publish

on:
  push:
    branches:
      - main

jobs:
  publish:
    runs-on: docker
    env:
      SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
    steps:
      - uses: https://forge.melm.xyz/melmco/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0 # all history for all branches and tags\

      - uses: https://forge.melm.xyz/melmco/cache@v3
        id: cache
        with:
          path: |
            ~/.cache/go-build
            ~/go
          key: ${{ runner.os }}-go-

      - uses: actions/setup-go@v4
        with:
          go-version: '>=1.21.0'

      - name: Install Hugo
        run: |
          wget -O hugo.deb https://github.com/gohugoio/hugo/releases/download/v0.144.2/hugo_extended_0.144.2_linux-amd64.deb
          dpkg -i hugo.deb

      - uses: https://forge.melm.xyz/melmco/cache@v3
        with:
          path: |
            ~/.cache/go-build
            ~/go
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-

      - name: Build
        run: |
          hugo --minify

      - name: Deploy
        run: |
          eval $(ssh-agent -s)
          echo "$SSH_PRIVATE_KEY" | ssh-add -
          apt update && apt install -y rsync
          mkdir ~/.ssh
          ssh-keyscan your-host.com  > ~/.ssh/known_hosts
          rsync -atv --progress ./public/ forgejo@192.168.1.145:/var/www/melm.xyz
