name: Python sdist/wheels (& publish)

on:
  push:
    branches:
      - master
  pull_request:
  release:
    types: [created]

jobs:
  build_osxwin:

    strategy:
      matrix:
        python-version: [3.5, 3.6, 3.7, 3.8]
        os: [macos-latest, windows-latest]

    runs-on: ${{ matrix.os }}

    steps:
    - uses: actions/checkout@v2
    - uses: ilammy/msvc-dev-cmd@v1
      if: runner.os == 'Windows'
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v1
      with:
        python-version: ${{ matrix.python-version }}
    - name: Python dependencies
      run: |
        pip install wheel
    - name: Pytanque wheel
      working-directory: petanque
      run: |
        python ./setup.py bdist_wheel
    - uses: actions/upload-artifact@v1
      with:
        name: pytanque-wheels
        path: petanque/dist/

  build_linux:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v1
      with:
        python-version: 3.8
    - name: Build manylinux Python wheels
      uses: aguinet/python-wheels-manylinux-build@v0.2.2-manylinux2010_x86_64
      with:
        python-versions: 'cp35-cp35m  cp36-cp36m  cp37-cp37m  cp38-cp38'
        build-requirements: ''
        system-packages: ''
        package-path: 'petanque'
        pip-wheel-args: ''
    - name: Gather wheels
      run: |
        mkdir wheelhouse_final
        cp wheelhouse/*manylinux*.whl wheelhouse_final
    - uses: actions/upload-artifact@v1
      with:
        name: pytanque-wheels
        path: wheelhouse_final/

  publish:
    if: github.event.release
    runs-on: ubuntu-latest
    needs: [build_linux, build_osxwin]

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v1
      with:
        python-version: 3.8
    - name: Install dependencies
      run: |
        pip install twine
    - name: Build arybo and pytanque sdist
      run: |
        python ./setup.py sdist
        cd petanque && python ./setup.py sdist
    - uses: actions/download-artifact@v1
      with:
        name: pytanque-wheels
        path: wheelhouse
    - name: Publish to PyPI
      env:
        TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
        TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
      run: |
        twine upload wheelhouse/*.whl dist/* petanque/dist/*
