name: CD

on:
  workflow_dispatch:
  schedule:
    - cron: 0 0 * * 5
  release:
    types:
      - published

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

env:
  FORCE_COLOR: 3

jobs:
  build_sdist:
    name: Build SDist
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Build SDist
        run: pipx run build --sdist

      - uses: actions/upload-artifact@v4
        with:
          name: cibw-sdist
          path: dist/*.tar.gz


  build_wheels:
    uses: ./.github/workflows/reuseable_cibuildwheel.yml
    secrets: inherit
    with:
      auto: true
      extra: true


  check_dist:
    name: Check dist
    needs: [build_wheels, build_sdist]
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: cibw-*
          path: dist
          merge-multiple: true

      - run: pipx run twine check --strict dist/*


  upload_all:
    needs: [build_wheels, build_sdist, check_dist]
    environment:
      name: pypi
      url: https://pypi.org/project/{{ repo_name }}
    permissions:
      id-token: write
    runs-on: ubuntu-latest
    if: github.event_name == 'release' && github.event.action == 'published'

    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: cibw-*
          path: dist
          merge-multiple: true

      - uses: pypa/gh-action-pypi-publish@release/v1
        env:
          PYPI_API_TOKEN: {{ '${{ secrets.PYPI_API_TOKEN }}' }}
        with:
          password: {{ '${{ secrets.PYPI_API_TOKEN }}' }}
