name: PyPi Release

on:
  push:
    tags:
      - "*"

permissions:
  contents: read

jobs:
  pypi-publish:
    if: startsWith(github.ref, 'refs/tags/')
    runs-on: ubuntu-latest
    environment: release
    permissions:
      id-token: write

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: "3.x"

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

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

      - name: Check metadata
        run: twine check dist/*

      - name: Publish package distributions to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1

  github-release:
    runs-on: ubuntu-latest
    name: GitHub release
    environment: release
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@v4

      - name: Get release version
        id: get_version
        uses: actions/github-script@v7
        with:
          script: core.setOutput('version', context.ref.replace("refs/tags/", ""))

      - name: Create GitHub release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          name: "v${{ steps.get_version.outputs.version }}"
          body: "Notes: https://python-cmake.github.io/pytest-cmake/release/release_notes.html"
