name: Build and Deploy to gh-pages branch

on:
  push:
    branches:
      - main  # Set a branch to deploy
  pull_request:

permissions:
  contents: write

jobs:
  deploy:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v7
        with:
          submodules: true  # Fetch Hugo themes (true OR recursive)
          fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod

      - name: Read Hugo version
        id: hugo
        run: echo "version=$(cat .hugo-version)" >> "$GITHUB_OUTPUT"

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v3
        with:
          hugo-version: ${{ steps.hugo.outputs.version }}
          extended: true

      - name: Setup Node.js
        uses: actions/setup-node@v7
        with:
          node-version: '22'
      
      - name: NPM Install
        run: npm install

      # A disabled htmltest step used to sit here. `if: false` skips a step but
      # does not stop GitHub building a container action's image during job
      # setup, so every deploy still depended on Docker Hub being reachable -
      # and failed when it was not. Re-add it as a separate job if link
      # checking is wanted again; .htmltest.yml is still in the repo.

      - name: Build
        run: hugo --source exampleSite --minify --cleanDestinationDir --enableGitInfo --gc --printPathWarnings --templateMetrics

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v4
        if: github.ref == 'refs/heads/main'
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./exampleSite/public
