name: Build+Upload Linux Python Wheels
on:
  push:
    branches:
      - master
    tags:
      - '*'
  pull_request:
    branches:
      - master

jobs:
  build_linux_wheels:
    name: Build manylinux2014 Wheels
    runs-on: ubuntu-latest
    outputs:
      perform_pypi_upload: ${{ steps.perform_pypi_upload_check.outputs.perform_pypi_upload }}
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3
        with:
          submodules: 'recursive'
      - name: Build Wheel Builder Container
        shell: bash
        run: |
          set -x
          cd .github/manylinux2014_wheel_builder
          docker build --tag zmat_wheel_builder --force-rm .
      - name: Build Manylinux2014 Wheels
        shell: bash
        run: |
          docker run -v ${GITHUB_WORKSPACE}:/src/ zmat_wheel_builder
          sudo mv ${GITHUB_WORKSPACE}/python/dist/ ${GITHUB_WORKSPACE}/dist/
      - name: Check If the Build Version Exists on PyPI
        id: perform_pypi_upload_check
        shell: bash
        run: |
          bash $GITHUB_WORKSPACE/.github/check-pypi-upload.sh
      - name: Prepare Wheels for Upload
        uses: actions/upload-artifact@v4
        with:
          name: linux-wheels
          path: dist/

  upload_linux_wheels:
    needs: build_linux_wheels
    runs-on: ubuntu-latest
    if: ${{ github.repository_owner == 'NeuroJSON' && needs.build_linux_wheels.outputs.perform_pypi_upload == 1 && github.event_name != 'pull_request'}}
    permissions:
      id-token: write
    steps:
      - name: Download Wheels from Build Job
        uses: actions/download-artifact@v4
        with:
          name: linux-wheels
          path: dist/
      - name: Upload packages to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}
          verify_metadata: false
          verbose: true
          skip_existing: true