name: Publish to PyPI

on:
  push:
    tags:
      - 'v*' # Trigger this workflow for any tag push that starts with v

permissions:
  contents: read
  id-token: write # Allow the workflow to request an ID token

jobs:
  build-and-publish:
    runs-on: ubuntu-latest
    steps:
      - name: Check out the repository
        uses: actions/checkout@v4
        with:
          # Fetch all history and tags
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.12' # Ensure this matches your project's requirements

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install build setuptools-scm twine

      - name: Build package using build
        run: |
          python -m build

      - name: Check package
        run: |
          twine check dist/*

      - name: Publish the package to PyPI
        uses: pypa/gh-action-pypi-publish@v1.8.11
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}