# .github/workflows/preview.yml
name: Deploy Github Pages

on:
  push:
    branches:
      - main
    paths:
      - "docs/**"
      - ".github/workflows/publish-page.yml"
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
      - closed
    paths:
      - "docs/**"
      - ".github/workflows/publish-page.yml"

concurrency: ci-${{ github.ref }}

jobs:
  publish:
    permissions:
      contents: write
      pull-requests: write

    # Do not run this on forks:
    if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == 'grafana/tanka'

    runs-on: ubuntu-24.04
    steps:
      - name: Checkout
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          persist-credentials: false

      - name: Install pnpm
        uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
        with:
          version: 10.15.1
      - uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0
        with:
          node-version: 20
          cache: 'pnpm'
          cache-dependency-path: 'docs'

      - name: Install and Build
        working-directory: docs
        env:
          # Main: https://tanka.dev/
          # PRs: https://tanka.dev/pr-preview/pr-{number}/
          PATH_PREFIX: "${{ github.event_name == 'pull_request' && format('/pr-preview/pr-{0}/', github.event.number) || '' }}"
        run: |
          pnpm install
          pnpm build
          if [ -d "./public" ]; then
            touch ./public/.nojekyll
          fi
          if [ -d "./dist" ]; then
            touch ./dist/.nojekyll
          fi

      - name: Deploy main
        if: github.event_name != 'pull_request'
        uses: JamesIves/github-pages-deploy-action@6c2d9db40f9296374acc17b90404b6e8864128c8 # v4.7.3
        with:
          clean-exclude: pr-preview/
          folder: ./docs/dist/

      - name: Deploy preview
        if: github.event_name == 'pull_request'
        uses: rossjrw/pr-preview-action@9f77b1d057b494e662c50b8ca40ecc63f21e0887 # v1.6.2
        with:
          deploy-repository: ${{ github.event.pull_request.head.repo.full_name }}
          source-dir: ./docs/dist/
