name: Website Preview

on:
  workflow_dispatch:
    inputs:
      pull-request-number:
        description: "Pull request number to preview"
        required: true
        type: string

  pull_request_target:
    types: [closed]
    branches: [main]
    paths:
      - website/**
      - docs/**

env:
  # PREVIEW_DEPLOY_KEY is present in the secrets
  PREVIEW_EXTERNAL_REPOSITORY: libretime/libretime.github.io
  PREVIEW_EXTERNAL_REPOSITORY_BRANCH: gh-pages
  PREVIEW_URL: https://libretime.github.io
  PREVIEW_BASE_URL: /

jobs:
  deploy:
    if: github.event_name == 'workflow_dispatch'
    name: Deploy
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Checkout pull request
        run: hub pr checkout ${{ github.event.inputs.pull-request-number }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: "16"
          cache: yarn
          cache-dependency-path: website/yarn.lock

      - name: Install
        working-directory: website
        run: yarn install --frozen-lockfile

      - name: Build
        working-directory: website
        run: yarn build
        env:
          URL: ${{ env.PREVIEW_URL }}
          BASE_URL: ${{ env.PREVIEW_BASE_URL }}pr-${{ github.event.inputs.pull-request-number }}/

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          external_repository: ${{ env.PREVIEW_EXTERNAL_REPOSITORY }}
          deploy_key: ${{ secrets.PREVIEW_DEPLOY_KEY }}
          publish_dir: website/build
          destination_dir: pr-${{ github.event.inputs.pull-request-number }}
          full_commit_message: "deploy pr-${{ github.event.inputs.pull-request-number }}"
          keep_files: true

      - name: Find deployment comment
        uses: peter-evans/find-comment@v2
        id: find-comment
        with:
          issue-number: ${{ github.event.inputs.pull-request-number }}
          comment-author: github-actions[bot]
          body-includes: Website preview deployment

      - name: Notify deployment succeeded
        if: ${{ success() }}
        uses: peter-evans/create-or-update-comment@v2
        with:
          issue-number: ${{ github.event.inputs.pull-request-number }}
          comment-id: ${{ steps.find-comment.outputs.comment-id }}
          edit-mode: replace
          body: |
            **:rocket: Website preview deployment succeeded!**

            Website preview: ${{ env.PREVIEW_URL }}${{ env.PREVIEW_BASE_URL }}pr-${{ github.event.inputs.pull-request-number }}/
            New docs preview: ${{ env.PREVIEW_URL }}${{ env.PREVIEW_BASE_URL }}pr-${{ github.event.inputs.pull-request-number }}/docs/next/
            Workflow: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}

      - name: Notify deployment failed
        if: ${{ failure() }}
        uses: peter-evans/create-or-update-comment@v2
        with:
          issue-number: ${{ github.event.inputs.pull-request-number }}
          comment-id: ${{ steps.find-comment.outputs.comment-id }}
          edit-mode: replace
          body: |
            **:boom: Website preview deployment failed!**

            Workflow: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}

  clean:
    if: github.event_name == 'pull_request_target' && github.event.action == 'closed'
    name: Clean
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
        with:
          repository: ${{ env.PREVIEW_EXTERNAL_REPOSITORY }}
          ref: ${{ env.PREVIEW_EXTERNAL_REPOSITORY_BRANCH }}
          ssh-key: ${{ secrets.PREVIEW_DEPLOY_KEY }}

      - name: Remove files
        run: rm -fR pr-${{ github.event.pull_request.number }}

      - uses: endbug/add-and-commit@v9
        with:
          message: "clean pr-${{ github.event.pull_request.number }}"
