name: Build wheels and deploy

on:
  workflow_dispatch:
    inputs:
      confirm_ref:
        description: "Enter branch/tag name to deploy to PyPI, or leave blank to only build wheels."
        default: ""
      override_version:
        description: "Override version number:"
        default: ""


jobs:
  deploy_canary:
    name: "Will deploy to PyPI if ticked"
    runs-on: ubuntu-18.04
    if: endsWith(github.ref, format('/{0}', github.event.inputs.confirm_ref))
    steps:
      - name: "Built wheels will be deployed at the end."
        run: |
          echo "Built wheels will be deployed at the end."

  build_wheels:
    name: Build wheels on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-18.04, windows-latest, macos-latest]
    env:
      # Set up wheels matrix.  This is CPython 3.6--3.9 for all OS targets.
      CIBW_BUILD: "cp3[6-9]-*"
      # Environment variable understood by QuTiP's setup.py to produce a release
      # version of the wheels.
      CIBW_ENVIRONMENT: "CI_QUTIP_RELEASE=1"
      OVERRIDE_VERSION: ${{ github.event.inputs.override_version }}

    steps:
      - uses: actions/checkout@v2

      - uses: actions/setup-python@v2
        name: Install Python
        with:
          # This is about the build environment, not the released wheel version.
          python-version: '3.7'

      - name: Install cibuildwheel
        run: |
          # cibuildwheel does the heavy lifting for us. Originally tested on
          # 1.8.0, but should be fine at least up to any major new release.
          python -m pip install 'cibuildwheel<2.0'

      - name: Install Visual C++ for Python 2.7
        if: runner.os == 'Windows'
        run: |
          choco install vcpython27 -f -y

      - name: Build wheels
        shell: bash
        run: |
          # If the version override was specified, then write it the VERSION
          # file with it.
          if [[ ! -z "$OVERRIDE_VERSION" ]]; then echo "$OVERRIDE_VERSION" > VERSION; fi
          python -m cibuildwheel --output-dir wheelhouse

      - uses: actions/upload-artifact@v2
        with:
          name: wheels
          path: ./wheelhouse/*.whl

  deploy:
    name: "Deploy to PyPI"
    needs: build_wheels
    runs-on: ubuntu-18.04
    if: endsWith(github.ref, format('/{0}', github.event.inputs.confirm_ref))
    env:
      TWINE_USERNAME: __token__
      TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
      TWINE_NON_INTERACTIVE: 1
      TWINE_REPOSITORY: pypi

    steps:
      - uses: actions/download-artifact@v2
        name: Download wheels to local runner

      - name: Upload wheels to PyPI
        run: |
          python -m pip install "twine"
          python -m twine upload --verbose wheels/*.whl

Build wheels on GitHub Actions
Build wheels on GitHub Actions

Wheels are automatically built on all pushes to the default branch
(including PR merges), and can be manually triggered from the GitHub
Actions web interface.

Use Python package cibuildwheel to build wheels for Linux (manylinux*),
macOS and Windows.
