|
| 1 | +name: Build Python distributions, publish on PyPI, and create a GitHub release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + push: |
| 6 | + tags: |
| 7 | + - "v*.*.*" |
| 8 | + |
| 9 | +env: |
| 10 | + PYPI_PROJECT_URL: "https://pypi.org/p/packageurl-python" |
| 11 | + |
| 12 | +jobs: |
| 13 | + build-python-dist: |
| 14 | + name: Build Python distributions |
| 15 | + runs-on: ubuntu-24.04 |
| 16 | + permissions: |
| 17 | + contents: read |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Checkout repository |
| 21 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 22 | + with: |
| 23 | + persist-credentials: false # do not keep the token around |
| 24 | + |
| 25 | + - name: Set up Python |
| 26 | + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 |
| 27 | + with: |
| 28 | + python-version: 3.14 |
| 29 | + |
| 30 | + - name: Install pypa/build |
| 31 | + run: python -m pip install build==1.4.0 --user |
| 32 | + |
| 33 | + - name: Build a binary wheel and a source tarball |
| 34 | + run: python -m build --sdist --wheel --outdir dist/ |
| 35 | + |
| 36 | + - name: Upload package distributions as GitHub workflow artifacts |
| 37 | + uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0 |
| 38 | + with: |
| 39 | + name: python-package-distributions |
| 40 | + path: dist/ |
| 41 | + |
| 42 | + # Only set the id-token: write permission in the job that does publishing, not globally. |
| 43 | + # Also, separate building from publishing, this makes sure that any scripts |
| 44 | + # maliciously injected into the build or test environment won't be able to elevate |
| 45 | + # privileges while flying under the radar. |
| 46 | + pypi-publish: |
| 47 | + name: Upload package distributions to PyPI |
| 48 | + if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes |
| 49 | + needs: |
| 50 | + - build-python-dist |
| 51 | + runs-on: ubuntu-24.04 |
| 52 | + environment: |
| 53 | + name: pypi |
| 54 | + url: ${{ env.PYPI_PROJECT_URL }} |
| 55 | + permissions: |
| 56 | + id-token: write # IMPORTANT: this permission is mandatory for trusted publishing |
| 57 | + |
| 58 | + steps: |
| 59 | + - name: Download package distributions |
| 60 | + uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0 |
| 61 | + with: |
| 62 | + name: python-package-distributions |
| 63 | + path: dist/ |
| 64 | + |
| 65 | + - name: Publish to PyPI |
| 66 | + uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # v1.13.0 |
| 67 | + |
| 68 | + create-gh-release: |
| 69 | + name: Create GitHub release |
| 70 | + needs: |
| 71 | + - build-python-dist |
| 72 | + runs-on: ubuntu-24.04 |
| 73 | + permissions: |
| 74 | + contents: write |
| 75 | + |
| 76 | + steps: |
| 77 | + - name: Checkout repository |
| 78 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 79 | + with: |
| 80 | + persist-credentials: false |
| 81 | + |
| 82 | + - name: Download package distributions |
| 83 | + uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0 |
| 84 | + with: |
| 85 | + name: python-package-distributions |
| 86 | + path: dist/ |
| 87 | + |
| 88 | + - name: Create GitHub release |
| 89 | + run: gh release create "$GITHUB_REF_NAME" dist/* --generate-notes |
| 90 | + env: |
| 91 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments