# This workflow will upload a Python Package to PyPI when a release is created
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Upload Python Package

on:
  release:
    types: [published]

permissions:
  contents: read

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  release-build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-python@v6
        with:
          python-version: "3.14t"

      - name: Build release distributions
        run: |
          python -m pip install build
          python -m build

      # Smoke-test the built wheel against latest PyPI-resolved deps before publishing.
      # v0.3.1 shipped broken because uv.lock pinned a milo-cli version that tests passed
      # against, while the wheel's `>=` constraint resolved to a newer, incompatible milo
      # once released. This step installs the wheel into a fresh venv (no lockfile,
      # no cache) and verifies the CLI can start — the exact path an end user takes.
      - name: Smoke-test wheel with fresh deps
        run: |
          python -m venv /tmp/wheel-smoke-venv
          /tmp/wheel-smoke-venv/bin/python -m pip install --quiet --upgrade pip
          /tmp/wheel-smoke-venv/bin/python -m pip install --quiet dist/bengal-*.whl
          /tmp/wheel-smoke-venv/bin/bengal --version
          /tmp/wheel-smoke-venv/bin/bengal --help > /dev/null
          /tmp/wheel-smoke-venv/bin/bengal cache hash --help > /dev/null
          mkdir -p /tmp/bengal-wheel-smoke/content
          printf '%s\n' '[site]' 'title = "Wheel Smoke"' > /tmp/bengal-wheel-smoke/bengal.toml
          printf '%s\n' '---' 'title: Home' '---' '# Home' > /tmp/bengal-wheel-smoke/content/index.md
          /tmp/wheel-smoke-venv/bin/bengal cache inputs --source /tmp/bengal-wheel-smoke
          /tmp/wheel-smoke-venv/bin/bengal cache hash --source /tmp/bengal-wheel-smoke

      - name: Upload distributions
        uses: actions/upload-artifact@v7
        with:
          name: release-dists
          path: dist/

  pypi-publish:
    runs-on: ubuntu-latest
    needs:
      - release-build
    permissions:
      id-token: write

    environment:
      name: pypi
      url: https://pypi.org/p/bengal

    steps:
      - name: Retrieve release distributions
        uses: actions/download-artifact@v8
        with:
          name: release-dists
          path: dist/

      - name: Publish release distributions to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          packages-dir: dist/
