name: Deploy to GitHub Pages

on:
  workflow_dispatch:
  push:
    branches:
      - develop

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: Set up Node.js
        uses: actions/setup-node@v4
        env:
          NODE_AUTH_TOKEN: ${{ github.token }}
        with:
          node-version: 20.x

      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set auth token
        run: echo '//npm.pkg.github.com/:_authToken=${{ secrets.GH_PAT }}' >> .npmrc

      - name: Install dependencies
        run: npm ci

      - name: Build statics
        run: npm run build
        env:
          VITE_ANALYTICS_WEBSITE_ID: ${{ vars.ANALYTICS_WEBSITE_ID }}

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          name: github-pages
          path: dist/
          retention-days: 3

  # https://github.com/actions/deploy-pages?tab=readme-ov-file#usage
  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    needs: build
    permissions:
      pages: write
      id-token: write
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

  notify:
    name: Send Discord workflow notification
    runs-on: ubuntu-latest
    needs: deploy
    steps:
      - name: Send notification
        uses: annybs/action-notify-discord@v1
        if: ${{ always() }}
        with:
          repository: ${{ github.repository }}
          result: ${{ needs.deploy.result }}
          run-id: ${{ github.run_id }}
          run-number: ${{ github.run_number }}
          webhook-url: ${{ secrets.DISCORD_WEBHOOK }}
          workflow: ${{ github.workflow }}