|
| 1 | +name: Publish to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + version: |
| 9 | + description: 'Version to publish (e.g., 1.0.0)' |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + |
| 13 | +jobs: |
| 14 | + publish: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + # Strongly recommended: use a GitHub environment for additional security |
| 17 | + environment: pypi |
| 18 | + permissions: |
| 19 | + contents: write |
| 20 | + # IMPORTANT: this permission is mandatory for Trusted Publishing |
| 21 | + id-token: write |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Checkout code |
| 25 | + uses: actions/checkout@v4 |
| 26 | + with: |
| 27 | + fetch-depth: 0 |
| 28 | + |
| 29 | + - name: Set up Python |
| 30 | + uses: actions/setup-python@v5 |
| 31 | + with: |
| 32 | + python-version: '3.11' |
| 33 | + |
| 34 | + - name: Install build tools |
| 35 | + run: | |
| 36 | + python -m pip install --upgrade pip |
| 37 | + pip install build toml |
| 38 | + |
| 39 | + - name: Update version (if manual trigger) |
| 40 | + if: github.event_name == 'workflow_dispatch' |
| 41 | + run: | |
| 42 | + python << 'EOF' |
| 43 | + import toml |
| 44 | + with open('pyproject.toml', 'r') as f: |
| 45 | + data = toml.load(f) |
| 46 | + data['project']['version'] = '${{ inputs.version }}' |
| 47 | + with open('pyproject.toml', 'w') as f: |
| 48 | + toml.dump(data, f) |
| 49 | + EOF |
| 50 | + git config user.name "GitHub Actions" |
| 51 | + git config user.email "actions@github.com" |
| 52 | + git add pyproject.toml |
| 53 | + git commit -m "chore: bump version to ${{ inputs.version }}" |
| 54 | + git tag "v${{ inputs.version }}" |
| 55 | + git push origin main --tags |
| 56 | + |
| 57 | + - name: Build package |
| 58 | + run: python -m build |
| 59 | + |
| 60 | + - name: Publish to PyPI (Trusted Publishing) |
| 61 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 62 | + |
| 63 | + - name: Create GitHub Release (if manual) |
| 64 | + if: github.event_name == 'workflow_dispatch' |
| 65 | + uses: softprops/action-gh-release@v1 |
| 66 | + with: |
| 67 | + tag_name: v${{ inputs.version }} |
| 68 | + name: Release v${{ inputs.version }} |
| 69 | + draft: false |
| 70 | + prerelease: false |
| 71 | + generate_release_notes: true |
0 commit comments