# This workflow will generate the static page under `pr` subdirectory inside the `gh-pages` branch

# This workflow will run every time there's a PR opened, reopened, synchronize, or closed

name: Deploy PR previews

on:
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
      - closed

concurrency: preview-${{ github.ref }}

env:
  PREVIEW_BRANCH: gh-pages
jobs:
  deploy-preview:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: latest


      - name: Install and Build
        run: |
          npm install
          npm run ghpages:build

      # Reference: https://github.com/rossjrw/pr-preview-action
      - name: Deploy preview
        uses: rossjrw/pr-preview-action@v1
        id: preview-step
        with:
          source-dir: ./ghpages/
          umbrella-dir: pr
          preview-branch: ${{ env.PREVIEW_BRANCH }}
          comment: false

      - uses: marocchino/sticky-pull-request-comment@v2
        if: steps.preview-step.outputs.deployment-action == 'deploy'
        with:
          header: pr-preview
          message: |
            [PR Preview Action](https://github.com/rossjrw/pr-preview-action) ${{ steps.preview-step.outputs.action-version }}
            :---:
            | <p></p> :rocket: View preview at <br> ${{ steps.preview-step.outputs.preview-url }}demo/ <br><br>
            | <h6>Built to branch [`${{ env.PREVIEW_BRANCH }}`](${{ github.server_url }}/${{ github.repository }}/tree/${{ env.PREVIEW_BRANCH }}) at ${{ steps.preview-step.outputs.action-start-time }}. <br> Preview will be ready when the [GitHub Pages deployment](${{ github.server_url }}/${{ github.repository }}/deployments) is complete. <br><br> </h6>

      - uses: marocchino/sticky-pull-request-comment@v2
        if: steps.preview-step.outputs.deployment-action == 'remove'
        with:
          header: pr-preview
          message: |
            [PR Preview Action](https://github.com/rossjrw/pr-preview-action) ${{ steps.preview-step.outputs.action-version }}
            :---:
            Preview removed because the pull request was closed.
            ${{ steps.preview-step.outputs.action-start-time }}
