name: Build and upload to PyPI

on:
  push:
    branches:
      - release

jobs:
  build_wheels:
    name: Build wheels on ${{ matrix.os }} for ${{ matrix.arch }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            arch: x86_64
            build: "*-manylinux_*"
          - os: ubuntu-latest
            arch: x86_64
            build: "*-musllinux_*"
          - os: ubuntu-latest
            arch: i686
            build: "*-manylinux_*"
          - os: ubuntu-latest
            arch: i686
            build: "*-musllinux_*"

          - os: ubuntu-latest
            arch: aarch64
            build: "*-manylinux_*"
          - os: ubuntu-latest
            arch: aarch64
            build: "*-musllinux_*"
          - os: ubuntu-latest
            arch: s390x
            build: "*-manylinux_*"
          - os: ubuntu-latest
            arch: s390x
            build: "*-musllinux_*"

          - os: windows-latest
            arch: AMD64
          - os: windows-latest
            arch: x86

          - os: macos-latest
            arch: x86_64
          - os: macos-latest
            arch: arm64

    steps:
      - uses: actions/checkout@v4

      - name: Generate build name for artifact upload
        id: build-name
        if: runner.os == 'Linux'
        run: |
          name=""
          if [[ -n "${{ matrix.build }}" ]]; then
            name="-$(echo ${{ matrix.build }} | tr -d *-_)"
          fi
          echo "name: ${name}"
          echo "name=${name}" >> $GITHUB_OUTPUT

      # https://cibuildwheel.readthedocs.io/en/stable/faq/#emulation
      # offers this stanza plus “CIBW_ARCHS_LINUX” below:
      - name: Set up QEMU
        if: runner.os == 'Linux'
        uses: docker/setup-qemu-action@v3
        with:
          platforms: all

      - name: Build wheels
        uses: pypa/cibuildwheel@v3.3.1
        env:
          CIBW_ARCHS: ${{ matrix.arch }}
          CIBW_BUILD: ${{ matrix.build }}
          CIBW_SKIP: pp*
          CIBW_TEST_COMMAND: python -m unittest discover ephem
          CIBW_TEST_REQUIRES: tzdata
          CIBW_TEST_SKIP: "*-macosx_arm64 *-macosx_universal2:arm64"

      - uses: actions/upload-artifact@v4
        with:
          name: wheels-${{ matrix.os }}-${{ matrix.arch }}${{ steps.build-name.outputs.name }}
          path: ./wheelhouse/*.whl
          overwrite: true

  build_wheels_python2:
    name: Build wheels on ${{ matrix.os }} (Python 2.7)
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-13]

    steps:
      - uses: actions/checkout@v4

      # https://cibuildwheel.readthedocs.io/en/stable/faq/#emulation
      # offers this stanza plus “CIBW_ARCHS_LINUX” below:
      - name: Set up QEMU
        if: runner.os == 'Linux'
        uses: docker/setup-qemu-action@v3
        with:
          platforms: all

      - name: Build wheels
        uses: pypa/cibuildwheel@v1.12.0
        env:
          CIBW_ARCHS_LINUX: auto aarch64 s390x
          CIBW_BUILD: cp27-*
          CIBW_SKIP: pp*
          CIBW_TEST_COMMAND: python -m unittest discover ephem
          CIBW_TEST_REQUIRES: tzdata

      - uses: actions/upload-artifact@v4
        with:
          name: wheels-2.7-${{ matrix.os }}
          path: ./wheelhouse/*.whl
          overwrite: true

  build_sdist:
    name: Build source distribution
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        name: Install Python
        with:
          python-version: '3.12'

      - name: Install dependencies
        run: python -m pip install build

      - name: Build sdist
        run: python -m build --sdist

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

  upload_pypi:
    needs: [build_wheels, build_wheels_python2, build_sdist]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: wheels-*
          path: dist
          merge-multiple: true

      - name: List files to upload
        run: ls -1 dist/

      - uses: pypa/gh-action-pypi-publish@release/v1
        with:
          skip-existing: true
          user: __token__
          password: ${{ secrets.pypi_password }}
          # To test: repository_url: https://test.pypi.org/legacy/
