name: deploy

on:
  # Trigger this workflow when the "main" workflow has completed successfully
  # https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#workflow_run
  workflow_run:
    workflows:
      - main
    branches:
      - master
    types:
      - completed
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'

jobs:

  build_wheels:
    name: Build & Upload
    runs-on: ubuntu-22.04
    steps:
    - uses: actions/checkout@v3

    - name: Set up Python 3.x
      uses: actions/setup-python@v4
      with:
        python-version: '3.x'
        architecture: 'x64'

    - name: Upgrade pip
      run: |
        python3 -m pip install --upgrade pip

    - name: Install build
      run:
        pip3 install --upgrade build

    - name: Build wheel & sdist
      run: |
        python3 -m build --wheel --sdist

    - uses: actions/upload-artifact@v3
      with:
        name: built-packages
        path: |
          ./dist/*.whl
          ./dist/*.tar.gz

    - name: Publish to Pypi
      if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        password: ${{ secrets.PYPI_API_TOKEN }}
