name: Python Release

on:
  push:
    tags:
      - python-v*

jobs:
  create_macos_and_windows_wheels:
    name: Wheels for Python ${{ matrix.python-version }} / ${{ matrix.os }}
    strategy:
      matrix:
        os: [macos-11, windows-2019]
        python-version: ['3.7', '3.8', '3.9', '3.10']
        architecture: [x86, x64]
        exclude:
        - os: macos-11
          architecture: x86
        - os: windows-2019
          # TODO: Re-enable windows 32bits
          architecture: x86
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
          architecture: ${{ matrix.architecture }}
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - name: Install Tox
        run: pip install tox
      - name: Build wheel
        working-directory: ./bindings/python
        run: tox -e build-wheel
      - uses: actions/upload-artifact@v2
        with:
          name: Distribution Artifacts
          path: bindings/python/dist/

  create_wheels_manylinux:
    name: Wheels for Python ${{ matrix.PYTHON_IMPLEMENTATION_ABI }} / Linux
    strategy:
      fail-fast: false
      matrix:
        # List of the language-implementation API pairs to publish wheels for
        # The list of supported is obtainable by running `docker run quay.io/pypa/manylinux2014_x86_64 ls /opt/python`
        PYTHON_IMPLEMENTATION_ABI: [cp37-cp37m, cp38-cp38, cp39-cp39, cp310-cp310]
    runs-on: ubuntu-20.04
    container: quay.io/pypa/manylinux2014_x86_64  # Builds wheels on CentOS 7 (supported until 2024)
    env:
      # Variable needed for PyO3 to properly identify the python interpreter
      PYTHON_SYS_EXECUTABLE: /opt/python/${{ matrix.PYTHON_IMPLEMENTATION_ABI }}/bin/python
    steps:
      - uses: actions/checkout@v2
      - name: Install/Update OpenSSL
        run: |
          retryCount=0
          # yum install seems to be flakey (due to network timeouts)
          # retry up to 5 times with a 10s sleep in case of failure
          until yum install openssl-devel --assumeyes --noplugins; do
            # For some reason the install has failed
            if [ ${retryCount} -eq 5 ]; then
                false
            else
              retryCount=$((${retryCount}+1))
            fi
            sleep 10
          done
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - name: Install Tox
        run: ${{ env.PYTHON_SYS_EXECUTABLE }} -m pip install tox
      - name: Build wheel
        working-directory: ./bindings/python
        run: |
          ${{ env.PYTHON_SYS_EXECUTABLE }} -m tox -e build-wheel
          # Ensure that the wheel is tagged as manylinux2014 platform
          auditwheel repair \
            --wheel-dir=./dist \
            --plat manylinux2014_x86_64 \
            ./dist/jsonschema_rs-*-${{ matrix.PYTHON_IMPLEMENTATION_ABI }}-linux_x86_64.whl
          # Remove `linux_x86_64` tagged wheels as they are not supported by https://pypi.org
          # Example https://github.com/Stranger6667/jsonschema-rs/runs/766075274
          rm ./dist/jsonschema_rs-*-${{ matrix.PYTHON_IMPLEMENTATION_ABI }}-linux_x86_64.whl
      - uses: actions/upload-artifact@v2
        with:
          name: Distribution Artifacts
          path: bindings/python/dist/

  create_source_dist:
    name: Create sdist package
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: 3.7
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - name: Install Tox
        run: pip install tox
      - name: Build sdist
        working-directory: ./bindings/python
        run: tox -e build-sdist
      - uses: actions/upload-artifact@v2
        with:
          name: Distribution Artifacts
          path: bindings/python/dist/

  upload_to_pypi:
    needs:
    - create_macos_and_windows_wheels
    - create_wheels_manylinux
    - create_source_dist
    name: Upload Artifacts to PyPi
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/download-artifact@v2
      with:
        name: Distribution Artifacts
        path: bindings/python/dist/
    - name: Publish distribution package to PyPI
      uses: pypa/gh-action-pypi-publish@v1.2.2
      with:
        user: ${{ secrets.PYPI_USERNAME }}
        password: ${{ secrets.PYPI_PASSWORD }}
        packages_dir: bindings/python/dist/
