on:
  workflow_dispatch:
    inputs:
      upload_repo:
        description: 'Upload repository as an artifact'
        type: boolean
        default: false
  push:
    branches:
      - master
    paths:
      - .github/workflows/*
      - config.yml
      - config-prod.yml
      - assets/**
      - content/**
      - data/**
      - i18n/**
      - layouts/**
      - static/**
  pull_request:
    branches:
      - master
    paths:
      - .github/workflows/*
      - config.yml
      - config-prod.yml
      - assets/**
      - content/**
      - data/**
      - i18n/**
      - layouts/**
      - static/**

name: Site Deployment

jobs:
  build:
    name: Build
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout ScratchAddons/website-v2 (deployment)
        uses: actions/checkout@v4
        if: github.event_name != 'pull_request'
        with:
          path: repo
          fetch-depth: 0
      - name: Checkout ScratchAddons/website-v2 (testing)
        uses: actions/checkout@v4
        if: github.event_name == 'pull_request'
        with:
          path: repo
      - name: Checkout ScratchAddons/website-v2-script
        uses: actions/checkout@v4
        with:
          repository: ScratchAddons/website-v2-script
          path: script
      - name: Checkout ScratchAddons/website-i18n
        uses: actions/checkout@v4
        with:
          repository: ScratchAddons/website-i18n
          path: i18n

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20.x
      - name: Cache dependencies
        uses: actions/cache@v4
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: ${{ runner.os }}-node-

      - name: Execute preparation script
        run: | 
          echo Running...
          cd repo
          bash ../script/sh/hugo-deploy-site.sh

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v3
        with:
          hugo-version: '0.133.0'
          extended: true
      - name: Build site
        run: |
          cd repo
          hugo --gc --minify --config config.yml,config-prod.yml

      - name: Upload GitHub Pages artifact
        uses: actions/upload-pages-artifact@v3
        if: (github.event_name != 'pull_request') && (github.repository_owner == 'ScratchAddons') && (github.ref_name == 'master')
        with:
          path: ./repo/public
      - name: Upload whole repo
        uses: actions/upload-artifact@v4
        if: (github.event_name == 'workflow_dispatch') && (inputs.upload_repo)
        with:
          name: repo
          path: |
            ./repo
            !./repo/.git

  deploy:
    name: Deployment
    runs-on: ubuntu-latest
    needs: build
    permissions:
      pages: write
      id-token: write
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    if: (github.event_name != 'pull_request') && (github.repository_owner == 'ScratchAddons') && (github.ref_name == 'master')
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
