|
| 1 | +name: Publish to PyPI |
| 2 | + |
| 3 | +# Trigger the workflow when: |
| 4 | +# 1. A version tag is pushed (e.g., v0.1.0, v1.2.3) |
| 5 | +# 2. Manually triggered from GitHub Actions UI |
| 6 | +on: |
| 7 | + push: |
| 8 | + tags: |
| 9 | + - v* |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +jobs: |
| 13 | + # Job 1: Build the package distributions (wheel and sdist) |
| 14 | + build: |
| 15 | + name: Build distribution |
| 16 | + runs-on: ubuntu-latest |
| 17 | + |
| 18 | + steps: |
| 19 | + # Check out the repository code |
| 20 | + - name: Check out repository |
| 21 | + uses: actions/checkout@v4 |
| 22 | + |
| 23 | + # Set up Python 3.12 (matches your project requirement) |
| 24 | + - name: Set up Python |
| 25 | + uses: actions/setup-python@v5 |
| 26 | + with: |
| 27 | + python-version: "3.12" |
| 28 | + |
| 29 | + # Install the modern Python build tool |
| 30 | + - name: Install build tool |
| 31 | + run: pip install build |
| 32 | + |
| 33 | + # Build both wheel (.whl) and source distribution (.tar.gz) |
| 34 | + # This is equivalent to npm's build step |
| 35 | + - name: Build package |
| 36 | + run: python -m build |
| 37 | + |
| 38 | + # Upload the built distributions as artifacts |
| 39 | + # Other jobs will download these artifacts to publish |
| 40 | + - name: Upload distributions |
| 41 | + uses: actions/upload-artifact@v4 |
| 42 | + with: |
| 43 | + name: python-package-distributions |
| 44 | + path: dist/ |
| 45 | + |
| 46 | + # Job 2: Publish to TestPyPI (staging environment) |
| 47 | + # This is like publishing to a test registry - always runs to validate the package |
| 48 | + publish-to-testpypi: |
| 49 | + name: Publish to TestPyPI |
| 50 | + needs: [build] |
| 51 | + runs-on: ubuntu-latest |
| 52 | + |
| 53 | + # Required permission for OIDC (Trusted Publisher authentication) |
| 54 | + # This allows GitHub to prove its identity to PyPI without API tokens |
| 55 | + permissions: |
| 56 | + id-token: write |
| 57 | + |
| 58 | + steps: |
| 59 | + # Download the built distributions from the build job |
| 60 | + - name: Download distributions |
| 61 | + uses: actions/download-artifact@v4 |
| 62 | + with: |
| 63 | + name: python-package-distributions |
| 64 | + path: dist/ |
| 65 | + |
| 66 | + # Publish to TestPyPI using Trusted Publisher (OIDC) |
| 67 | + # No username/password needed - authentication happens via OIDC |
| 68 | + - name: Publish to TestPyPI |
| 69 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 70 | + with: |
| 71 | + repository-url: https://test.pypi.org/legacy/ |
| 72 | + |
| 73 | + # Job 3: Publish to production PyPI |
| 74 | + # Only runs when TestPyPI publish succeeds |
| 75 | + publish-to-pypi: |
| 76 | + name: Publish to PyPI |
| 77 | + needs: [publish-to-testpypi] |
| 78 | + runs-on: ubuntu-latest |
| 79 | + |
| 80 | + # Use a GitHub environment for additional protection |
| 81 | + # You can configure this environment in GitHub to require manual approval |
| 82 | + environment: |
| 83 | + name: pypi |
| 84 | + url: https://pypi.org/p/django-telescope |
| 85 | + |
| 86 | + # Required permission for OIDC (Trusted Publisher authentication) |
| 87 | + permissions: |
| 88 | + id-token: write |
| 89 | + |
| 90 | + steps: |
| 91 | + # Download the built distributions from the build job |
| 92 | + - name: Download distributions |
| 93 | + uses: actions/download-artifact@v4 |
| 94 | + with: |
| 95 | + name: python-package-distributions |
| 96 | + path: dist/ |
| 97 | + |
| 98 | + # Publish to production PyPI using Trusted Publisher (OIDC) |
| 99 | + # Automatically generates PEP 740 attestations (like npm provenance) |
| 100 | + - name: Publish to PyPI |
| 101 | + uses: pypa/gh-action-pypi-publish@release/v1 |
0 commit comments