|
| 1 | +name: Publish Python Package to PyPI |
| 2 | + |
| 3 | +# This workflow uses GitHub's OIDC trusted publishing to authenticate with PyPI. |
| 4 | +# For this to work, you must configure trusted publishers on PyPI/TestPyPI: |
| 5 | +# 1. Go to https://test.pypi.org/ or https://pypi.org/ |
| 6 | +# 2. Navigate to your project settings → Publishing |
| 7 | +# 3. Add a new publisher with these details: |
| 8 | +# - Owner: alexlib |
| 9 | +# - Repository: openptv-python |
| 10 | +# - Workflow: publish-to-pypi.yml |
| 11 | +# - Environment: testpypi (for TestPyPI) or pypi (for PyPI) |
| 12 | +# |
| 13 | +# TROUBLESHOOTING: |
| 14 | +# If you see "invalid-publisher" error, it means the trusted publisher is not configured |
| 15 | +# correctly on PyPI/TestPyPI. The configuration MUST match these exact values: |
| 16 | +# - Repository owner: alexlib |
| 17 | +# - Repository name: openptv-python |
| 18 | +# - Workflow filename: publish-to-pypi.yml (must be exact match) |
| 19 | +# - Environment name: testpypi or pypi (must match exactly) |
| 20 | +# |
| 21 | +# For detailed setup instructions, see DEPLOYMENT.md |
| 22 | + |
| 23 | +on: |
| 24 | + release: |
| 25 | + types: [published] |
| 26 | + workflow_dispatch: |
| 27 | + inputs: |
| 28 | + deploy_target: |
| 29 | + description: 'Deploy to PyPI or TestPyPI' |
| 30 | + required: true |
| 31 | + default: 'testpypi' |
| 32 | + type: choice |
| 33 | + options: |
| 34 | + - pypi |
| 35 | + - testpypi |
| 36 | + |
| 37 | +jobs: |
| 38 | + build: |
| 39 | + name: Build distribution |
| 40 | + runs-on: ubuntu-latest |
| 41 | + |
| 42 | + steps: |
| 43 | + - uses: actions/checkout@v4 |
| 44 | + |
| 45 | + - name: Set up Python |
| 46 | + uses: actions/setup-python@v5 |
| 47 | + with: |
| 48 | + python-version: '3.11' |
| 49 | + |
| 50 | + - name: Install build dependencies |
| 51 | + run: | |
| 52 | + python -m pip install --upgrade pip |
| 53 | + pip install build twine |
| 54 | +
|
| 55 | + - name: Build package |
| 56 | + run: python -m build |
| 57 | + |
| 58 | + - name: Check distribution |
| 59 | + run: twine check dist/* |
| 60 | + |
| 61 | + - name: Upload artifacts |
| 62 | + uses: actions/upload-artifact@v4 |
| 63 | + with: |
| 64 | + name: python-package-distributions |
| 65 | + path: dist/ |
| 66 | + |
| 67 | + publish-to-pypi: |
| 68 | + name: Publish to PyPI |
| 69 | + if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.deploy_target == 'pypi') |
| 70 | + needs: [build] |
| 71 | + runs-on: ubuntu-latest |
| 72 | + environment: |
| 73 | + name: pypi # This must match the environment name in PyPI's trusted publisher config |
| 74 | + url: https://pypi.org/p/openptv-python |
| 75 | + permissions: |
| 76 | + id-token: write # Required for trusted publishing via OIDC |
| 77 | + |
| 78 | + steps: |
| 79 | + - name: Download distributions |
| 80 | + uses: actions/download-artifact@v4 |
| 81 | + with: |
| 82 | + name: python-package-distributions |
| 83 | + path: dist/ |
| 84 | + |
| 85 | + - name: Publish to PyPI |
| 86 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 87 | + |
| 88 | + publish-to-testpypi: |
| 89 | + name: Publish to TestPyPI |
| 90 | + if: github.event_name == 'workflow_dispatch' && github.event.inputs.deploy_target == 'testpypi' |
| 91 | + needs: [build] |
| 92 | + runs-on: ubuntu-latest |
| 93 | + environment: |
| 94 | + name: testpypi # This must match the environment name in TestPyPI's trusted publisher config |
| 95 | + url: https://test.pypi.org/p/openptv-python |
| 96 | + permissions: |
| 97 | + id-token: write # Required for trusted publishing via OIDC |
| 98 | + |
| 99 | + steps: |
| 100 | + - name: Download distributions |
| 101 | + uses: actions/download-artifact@v4 |
| 102 | + with: |
| 103 | + name: python-package-distributions |
| 104 | + path: dist/ |
| 105 | + |
| 106 | + - name: Publish to TestPyPI |
| 107 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 108 | + with: |
| 109 | + repository-url: https://test.pypi.org/legacy/ |
| 110 | + |
0 commit comments