name: Release

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
  release:
    types: [ created ]

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
  cancel-in-progress: true

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-python@v6
        with:
          python-version: '3.12'

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

      - name: Build package
        shell: bash
        run: |
          python -m build
          twine check --strict dist/*

      - name: Upload artifacts
        uses: actions/upload-artifact@v6
        with:
          path: ./dist/*
          retention-days: 5

  publish:
    name: Publish on PyPI
    needs: [build]
    runs-on: ubuntu-latest
    # release on every tag
    if: github.event_name == 'release' && startsWith(github.ref, 'refs/tags/')
    steps:
      - uses: actions/download-artifact@v7
        with:
          name: artifact
          path: dist

      - name: Upload Wheels to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          user: __token__
          password: ${{ secrets.PYPI_API_TOKEN }}
          skip_existing: true
          # repository_url: https://test.pypi.org/legacy/  # To test
