name: Pages

# Publishes the website in site/ to GitHub Pages, with the current dist/ files.
# Runs when the site changes, and after each release so the demos use the new version.

on:
  push:
    branches: [main]
    paths:
      - 'site/**'
      - '.github/workflows/pages.yml'
  workflow_run:
    workflows: [Release]
    types: [completed]
  workflow_dispatch:

permissions:
  contents: read
  pages: write
  id-token: write

jobs:
  deploy:
    if: github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success'
    # On the job, not the workflow: a run skipped by the "if" above (the Release
    # run for a "Release vX" commit) then never takes the group, so it can't
    # cancel the real deploy. Deploys queue instead of cancelling each other.
    concurrency:
      group: pages
      cancel-in-progress: false
    runs-on: ubuntu-latest
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url || steps.retry.outputs.page_url }}
    steps:
      - uses: actions/checkout@v7
        with:
          ref: main
      - name: Assemble the site
        run: npm run site
      - uses: actions/configure-pages@v6
      - uses: actions/upload-pages-artifact@v5
        with:
          path: _site
      # Right after the upload, the deploy sometimes finds no artifact yet
      # ("No artifacts named github-pages"). Re-running the job doesn't help
      # (it then finds two), so wait and try once more in this run.
      - id: deployment
        uses: actions/deploy-pages@v5
        continue-on-error: true
      - name: Wait before retrying the deploy
        if: steps.deployment.outcome == 'failure'
        run: sleep 30
      - id: retry
        if: steps.deployment.outcome == 'failure'
        uses: actions/deploy-pages@v5
