name: publish-pypi-deploy

on:
  release:
    types: [ published ]

jobs:

  deploy:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        python-version: [ '3.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: Build package
      run: |
        python -m pip install --upgrade pip
        python -m pip install wheel
        python setup.py sdist bdist_wheel

    - name: Upload artifacts
      if: ${{ success() }}
      uses: actions/upload-artifact@v2
      with:
        name: wheels
        path: dist/*.whl

    - name: Set up venv
      run: |
        python -m venv ~/myenv
        source ~/myenv/bin/activate
        echo "Virtual env: $VIRTUAL_ENV"

    - name: Install pytest and our package
      run: |
        python -m pip install -U pytest
        cd ..  # avoid pip seeing 'pydicom' package dir below
        python -m pip install -f pydicom/dist pydicom

    - name: Test with pytest (no numpy)
      run: |
        python -c "import pytest; pytest.main(['--pyargs', 'pydicom.tests'])"

    - name: Install numpy and test
      run: |
        python -m pip install -U numpy
        python -c "import pytest; pytest.main(['--pyargs', 'pydicom.tests'])"
        cd pydicom  # back to root repo dir

    - name: Publish package to PyPi
      uses: pypa/gh-action-pypi-publish@master
      with:
        user: __token__
        password: ${{ secrets.PYPI_PASSWORD }}
