name: Release

on:
  push:
    tags:
      - 'v*.*.*'

permissions:
  contents: write

jobs:
  build:
    name: Build Distribution
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Install uv
      uses: astral-sh/setup-uv@v5
      with:
        version: "latest"

    - name: Set up Python
      run: uv python install 3.13

    - name: Build package
      run: |
        uv build

    - name: Store distribution packages
      uses: actions/upload-artifact@v4
      with:
        name: python-package-distributions
        path: dist/

  publish-to-pypi:
    name: Publish to PyPI
    needs: build
    runs-on: ubuntu-latest
    environment:
      name: pypi
      url: https://pypi.org/p/schema-sentinel
    permissions:
      id-token: write

    steps:
    - name: Download distribution packages
      uses: actions/download-artifact@v4
      with:
        name: python-package-distributions
        path: dist/

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

  github-release:
    name: Create GitHub Release
    needs: publish-to-pypi
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Extract release notes
      id: extract-release-notes
      run: |
        version=${GITHUB_REF#refs/tags/v}
        echo "version=$version" >> $GITHUB_OUTPUT

    - name: Download distribution packages
      uses: actions/download-artifact@v4
      with:
        name: python-package-distributions
        path: dist/

    - name: Create GitHub Release
      env:
        GITHUB_TOKEN: ${{ github.token }}
      run: |
        gh release create '${{ github.ref_name }}' \
          --repo '${{ github.repository }}' \
          --notes "Release ${{ steps.extract-release-notes.outputs.version }}" \
          dist/*
