name: github-pages

on:
  workflow_dispatch:
  push:
    branches:
      - master

# Taken from https://github.com/marketplace/actions/hugo-setup#%EF%B8%8F-workflow-for-autoprefixer-and-postcss-cli
# Both builds have to be one worflow as otherwise one publish will overwrite the other
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Setup Hugo
        uses: peaceiris/actions-hugo@75d2e84710de30f6ff7268e08f310b60ef14033f # v3.0.0
        with:
          hugo-version: '0.125.3'
          extended: true

      - name: Setup Node
        uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
        with:
          node-version: '22.5.1'

      - name: Cache dependencies
        uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          submodules: recursive
          fetch-depth: 0

      - name: Setup Pages
        id: pages
        uses: actions/configure-pages@983d7736d9b0ae728b81ab479565c72886d7745b # v5.0.0 - use this after https://github.com/DefectDojo/django-DefectDojo/pull/11329
      
      - name: Install dependencies
        run: cd docs && npm ci
      
      - name: Build production website
        env:
          HUGO_ENVIRONMENT: production
          HUGO_ENV: production
        run: cd docs && hugo --minify --gc --config config/production/hugo.toml

      - name: Deploy
        uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4.0.0
        with: # publishes to the `gh-pages` branch by default
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./docs/public
          cname: docs.defectdojo.com