name: Nightly packages

on:
  pull_request: # this shall test only the part of workflow before publishing
    branches: [main, "release/*"]
    types: [opened, reopened, ready_for_review, synchronize]
    paths:
      - ".github/workflows/release-nightly.yml"
  schedule:
    - cron: "0 0 * * 0" # on Sundays
  workflow_dispatch: {}

defaults:
  run:
    shell: bash

jobs:
  releasing-nightly:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-python@v6
        with:
          python-version: "3.10"

      - name: Install dependencies
        run: python -m pip install --user --upgrade setuptools wheel packaging
      - name: Build package
        env:
          CONVERT_VERSION2NIGHTLY: "1"
        run: python setup.py sdist bdist_wheel

      # We do this, since failures on test.pypi aren't that bad
      - name: Publish to Test PyPI
        if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
        uses: pypa/gh-action-pypi-publish@v1.13.0
        with:
          user: __token__
          password: ${{ secrets.test_pypi_password }}
          repository_url: https://test.pypi.org/legacy/

      - name: Publish distribution 📦 to PyPI
        if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
        uses: pypa/gh-action-pypi-publish@v1.13.0
        with:
          user: __token__
          password: ${{ secrets.pypi_password }}
