name: Release

on:
  push:
    tags:
      - "v*"

jobs:
  build:
    name: Build distribution
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: "3.14-dev"
        allow-prereleases: true
    
    - name: Install build dependencies
      run: pip install hatch
    
    - name: Build package
      run: hatch build
    
    - name: Upload builds
      uses: actions/upload-artifact@v3
      with:
        name: dist
        path: dist/

  publish:
    name: Publish to PyPI
    needs: build
    runs-on: ubuntu-latest
    permissions:
      id-token: write  # for trusted publishing
    environment:
      name: pypi
      url: https://pypi.org/p/better-dedent
    
    steps:
    - name: Download builds
      uses: actions/download-artifact@v3
      with:
        name: dist
        path: dist/
    
    - name: Publish to PyPI
      uses: pypa/gh-action-pypi-publish@release/v1

  github-release:
    name: Create GitHub Release
    needs: publish
    runs-on: ubuntu-latest
    permissions:
      contents: write
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Download builds
      uses: actions/download-artifact@v3
      with:
        name: dist
        path: dist/
    
    - name: Create GitHub Release
      uses: softprops/action-gh-release@v1
      with:
        files: dist/*
        generate_release_notes: true