name: Publish on PyPI

on:
  release:
    types: [published]
    # published, unpublished, created, edited, deleted, or prereleased
  workflow_dispatch:

jobs:
  publish:

    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.10'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install ".[pypi]"

      - name: Build source distribution
        run: |
          python3 setup.py sdist bdist_wheel
          twine check dist/*

      - name: Publish distribution to Test PyPI
        uses: pypa/gh-action-pypi-publish@master
        with:
          password: ${{ secrets.test_pypi_password }}
          repository_url: https://test.pypi.org/legacy/
        continue-on-error: true

      - name: Publish distribution to PyPI
        if: github.event.action == 'published' && !github.event.release.prerelease
        uses: pypa/gh-action-pypi-publish@master
        with:
          password: ${{ secrets.pypi_password }}
