|
| 1 | +name: Publish to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - 'v*.*.*' # Triggers on version tags like v0.0.21, v1.0.0, etc. |
| 7 | + |
| 8 | +jobs: |
| 9 | + publish: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + contents: read |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Checkout code |
| 16 | + uses: actions/checkout@v4 |
| 17 | + |
| 18 | + - name: Set up Python |
| 19 | + uses: actions/setup-python@v4 |
| 20 | + with: |
| 21 | + python-version: '3.12' |
| 22 | + |
| 23 | + - name: Install build dependencies |
| 24 | + run: | |
| 25 | + python -m pip install --upgrade pip |
| 26 | + pip install build twine |
| 27 | + |
| 28 | + - name: Extract version from tag |
| 29 | + id: get_version |
| 30 | + run: | |
| 31 | + # Get the tag name (e.g., v0.0.21) |
| 32 | + TAG=${GITHUB_REF#refs/tags/v} |
| 33 | + echo "VERSION=$TAG" >> $GITHUB_OUTPUT |
| 34 | + echo "Publishing version: $TAG" |
| 35 | + |
| 36 | + - name: Verify version matches setup.py |
| 37 | + run: | |
| 38 | + SETUP_VERSION=$(python -c "import re; content=open('setup.py').read(); print(re.search(r'version=\"([^\"]+)\"', content).group(1))") |
| 39 | + TAG_VERSION="${{ steps.get_version.outputs.VERSION }}" |
| 40 | + echo "setup.py version: $SETUP_VERSION" |
| 41 | + echo "Git tag version: $TAG_VERSION" |
| 42 | + if [ "$SETUP_VERSION" != "$TAG_VERSION" ]; then |
| 43 | + echo "ERROR: Version mismatch!" |
| 44 | + echo "setup.py has version $SETUP_VERSION but git tag is v$TAG_VERSION" |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | + echo "✅ Versions match!" |
| 48 | + |
| 49 | + - name: Build package |
| 50 | + run: python -m build |
| 51 | + |
| 52 | + - name: Check distribution files |
| 53 | + run: | |
| 54 | + ls -lh dist/ |
| 55 | + twine check dist/* |
| 56 | + |
| 57 | + - name: Publish to PyPI |
| 58 | + env: |
| 59 | + TWINE_USERNAME: __token__ |
| 60 | + TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} |
| 61 | + run: | |
| 62 | + twine upload dist/* |
| 63 | + |
| 64 | + - name: Create GitHub Release |
| 65 | + uses: softprops/action-gh-release@v1 |
| 66 | + with: |
| 67 | + generate_release_notes: true |
| 68 | + files: | |
| 69 | + dist/*.tar.gz |
| 70 | + dist/*.whl |
0 commit comments