name: Python Package Tests and Deploy

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
  release:
    types: [published]
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    container: ghcr.io/gridtools/gridtools-base:${{ matrix.compiler }}
    strategy:
        matrix:
            python-version: ["3.10"]
            compiler: [gcc-10]
            build_type: [release]
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
    - name: install nox
      run: |
        python -m pip install --upgrade pip setuptools
        python -m pip install nox
    - name: run tests
      working-directory: ./.python_package
      run: nox -s test_src
    - name: build and test wheel
      working-directory: ./.python_package
      run: nox -s build_wheel test_wheel_with_python-${{ matrix.python-version }}
    - name: archive wheel
      uses: actions/upload-artifact@v4
      with:
        name: gridtools-cpp-wheel
        path: .python_package/.nox/.cache/dist/gridtools_cpp-*.whl
  publish-pypi:
    name: Publish Python distribution to pypi.org
    runs-on: ubuntu-latest
    needs: build
    if: ${{ github.event_name == 'workflow_dispatch' }} # the action was triggered manually
    environment:
      name: pypi
      url: https://pypi.org/project/gridtools-cpp
    permissions:
      id-token: write
    steps:
    - name: download wheel
      uses: actions/download-artifact@v4
      with:
        name: gridtools-cpp-wheel
        path: .python_package/.nox/.cache/dist
    - name: Publish distribution to PyPI
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        packages-dir: .python_package/.nox/.cache/dist   
  publish-test-pypi:
    name: Publish Python distribution to test.pypi.org
    runs-on: ubuntu-latest
    needs: build
    if: ${{ github.event_name == 'release' }} # triggered by releasing on github, test first before manually triggering the deployment to PyPI (see release documentation)
    environment:
      name: testpypi
      url: https://test.pypi.org/project/gridtools-cpp
    permissions:
      id-token: write
    steps:
    - name: download wheel
      uses: actions/download-artifact@v4
      with:
        name: gridtools-cpp-wheel
        path: .python_package/.nox/.cache/dist
    - name: Publish distribution to PyPI
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        packages-dir: .python_package/.nox/.cache/dist   
        repository-url: https://test.pypi.org/legacy/

  test-wheel:
    needs: build
    runs-on: ubuntu-latest
    container: ghcr.io/gridtools/gridtools-base:${{ matrix.compiler }}
    strategy:
      matrix:
        python-version: ["3.8", "3.9", "3.11"]
        compiler: [gcc-10]
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
    - name: install nox
      run: |
        python -m pip install --upgrade pip setuptools
        python -m pip install nox
    - name: download wheel
      uses: actions/download-artifact@v4
      with:
        name: gridtools-cpp-wheel
        path: .python_package/.nox/.cache/dist
    - name: test wheel
      working-directory: ./.python_package
      run: nox -s test_wheel_with_python-${{ matrix.python-version }}
