# Builds and deploys the `dsc` documentation site to GitHub Pages.

name: docs

on:
  push:
    branches:
      - main
    paths:
      - "docs/**"
      - "mkdocs.yml"
      - "requirements.txt"
      - ".github/workflows/deploy-docs-to-ghpages.yml"
  workflow_dispatch:

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

concurrency:
  group: pages
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
      - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
        with:
          python-version: "3.x"
      - uses: actions/configure-pages@45bfe0192ca1faeb007ade9deae92b16b8254a0d # v6.0.0
      - run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
      - uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        with:
          key: zensical-${{ env.cache_id }}
          path: ~/.cache
          restore-keys: |
            zensical-
      - run: pip install -r requirements.txt
      - run: zensical build --clean

      # Drop installer proxies at the site root so users can do
      #   curl -LsSf https://pacharanero.github.io/dsc/install.sh | sh
      # instead of the full GitHub-releases URL. The scripts below simply
      # re-fetch cargo-dist's real installer for the latest release, so
      # they never go stale between docs deploys.
      - name: Add installer proxies
        run: |
          cat > site/install.sh <<'EOF'
          #!/bin/sh
          # dsc installer proxy — fetches the latest cargo-dist installer.
          # Source: https://github.com/pacharanero/dsc
          set -eu
          curl --proto '=https' --tlsv1.2 -LsSf \
            https://github.com/pacharanero/dsc/releases/latest/download/dsc-rs-installer.sh \
            | sh
          EOF
          chmod +x site/install.sh

          cat > site/install.ps1 <<'EOF'
          # dsc installer proxy — fetches the latest cargo-dist installer.
          # Source: https://github.com/pacharanero/dsc
          irm https://github.com/pacharanero/dsc/releases/latest/download/dsc-rs-installer.ps1 | iex
          EOF

      - uses: actions/upload-pages-artifact@fc324d3547104276b827a68afc52ff2a11cc49c9 # v5.0.0
        with:
          path: site

  deploy:
    needs: build
    runs-on: ubuntu-latest
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - id: deployment
        uses: actions/deploy-pages@cd2ce8fcbc39b97be8ca5fce6e763baed58fa128 # v5.0.0
