name: Deployment to VPS

on:
  push:
    branches:
      - main

  workflow_dispatch:

concurrency:
  group: "pages"
  cancel-in-progress: false

defaults:
  run:
    shell: bash

jobs:
  deploy:
    runs-on: ubuntu-latest
    env:
      HUGO_VERSION: 0.154.5
    steps:
      - name: Install Hugo CLI
        run: |
          wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \
          && sudo dpkg -i ${{ runner.temp }}/hugo.deb

      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0

      - name: Build with Hugo
        env:
          HUGO_CACHEDIR: ${{ runner.temp }}/hugo_cache
          HUGO_ENVIRONMENT: production
          TZ: UTC
        run: |
          hugo \
            --gc \
            --minify \

      - name: Setup SSH
        uses: webfactory/ssh-agent@v0.9.0
        with:
          ssh-private-key: ${{ secrets.DEPLOY_SSH_KEY }}

      - name: Deploy to VPS
        env: 
          SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
        run: |
          ssh-keyscan -H hazemkrimi.tech >> ~/.ssh/known_hosts
          rsync -avz --delete public/* deploy@hazemkrimi.tech:/var/www/hazemkrimi.tech
