name: Release to PyPI
on:
  push:
    tags: ["*"]

permissions:
  contents: read

env:
  dists-artifact-name: python-package-distributions

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
        with:
          fetch-depth: 0
          persist-credentials: false
      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0
        with:
          enable-cache: false
          cache-dependency-glob: "pyproject.toml"
          github-token: ${{ secrets.GITHUB_TOKEN }}
      - name: Build package
        run: uv build --python 3.14 --python-preference only-managed --sdist --wheel . --out-dir dist
      - name: Store the distribution packages
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
        with:
          name: ${{ env.dists-artifact-name }}
          path: dist/*

  release:
    needs:
      - build
    runs-on: ubuntu-latest
    environment:
      name: release
      url: https://pypi.org/project/python-discovery/${{ github.ref_name }}
    permissions:
      id-token: write
    steps:
      - name: Download all the dists
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
        with:
          name: ${{ env.dists-artifact-name }}
          path: dist/
      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # v1.14.0
        with:
          attestations: true
