---
name: release

on:
  release:
    types: [published]
  workflow_dispatch:

jobs:
  pypi:
    name: Publish to PyPI registry
    environment: release
    runs-on: ubuntu-22.04
    permissions:
      id-token: write

    env:
      FORCE_COLOR: 1
      PY_COLORS: 1
      TOXENV: pkg

    steps:
      - name: Switch to using Python 3.10 by default
        uses: actions/setup-python@v5
        with:
          python-version: "3.10"

      - name: Install tox
        run: python3 -m pip install --user "tox>=4.0.0"

      - name: Check out src from Git
        uses: actions/checkout@v4
        with:
          fetch-depth: 0 # needed by setuptools-scm
          submodules: true

      - name: Build dists
        run: python -m tox

      - name: Publish to pypi.org
        if: >- # "create" workflows run separately from "push" & "pull_request"
          github.event_name == 'release'
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.pypi_password }}
