|
1 | | -name: Build Python distributions and publish on PyPI |
| 1 | +name: Build Python distributions, publish on PyPI, and create a GH release |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | workflow_dispatch: |
5 | 5 | push: |
6 | 6 | tags: |
7 | 7 | - "v*.*.*" |
8 | 8 |
|
| 9 | +env: |
| 10 | + PYPI_PROJECT_URL: "https://pypi.org/p/scancodeio" |
| 11 | + |
9 | 12 | jobs: |
10 | | - build-and-publish: |
11 | | - name: Build and publish library to PyPI |
| 13 | + build-python-dist: |
| 14 | + name: Build Python distributions |
12 | 15 | runs-on: ubuntu-24.04 |
| 16 | + permissions: |
| 17 | + contents: read |
13 | 18 |
|
14 | 19 | steps: |
15 | | - - uses: actions/checkout@v4 |
| 20 | + - name: Checkout repository |
| 21 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 22 | + with: |
| 23 | + persist-credentials: false # do not keep the token around |
16 | 24 |
|
17 | 25 | - name: Set up Python |
18 | | - uses: actions/setup-python@v5 |
| 26 | + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 |
19 | 27 | with: |
20 | 28 | python-version: 3.14 |
21 | 29 |
|
22 | 30 | - name: Install pypa/build |
23 | 31 | run: python -m pip install build --user |
24 | 32 |
|
25 | 33 | - name: Build a binary wheel and a source tarball |
26 | | - run: python -m build --sdist --wheel --outdir dist/ . |
| 34 | + run: python -m build --sdist --wheel --outdir dist/ |
27 | 35 |
|
28 | | - - name: Publish to PyPI |
29 | | - if: startsWith(github.ref, 'refs/tags') |
30 | | - uses: pypa/gh-action-pypi-publish@release/v1 |
| 36 | + - name: Upload package distributions as GitHub workflow artifacts |
| 37 | + uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0 |
31 | 38 | with: |
32 | | - password: ${{ secrets.PYPI_API_TOKEN }} |
| 39 | + name: python-package-distributions |
| 40 | + path: dist/ |
33 | 41 |
|
34 | | - - name: Upload built archives |
35 | | - uses: actions/upload-artifact@v4 |
| 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 |
36 | 61 | with: |
37 | | - name: pypi_archives |
38 | | - path: dist/* |
| 62 | + name: python-package-distributions |
| 63 | + path: dist/ |
39 | 64 |
|
40 | | - - name: Create a GitHub release |
41 | | - uses: softprops/action-gh-release@v2 |
| 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: Download package distributions |
| 78 | + uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0 |
42 | 79 | with: |
43 | | - generate_release_notes: true |
44 | | - draft: false |
45 | | - files: dist/* |
| 80 | + name: python-package-distributions |
| 81 | + path: dist/ |
| 82 | + |
| 83 | + - name: Create GitHub release |
| 84 | + run: gh release create "$GITHUB_REF_NAME" dist/* --generate-notes |
| 85 | + env: |
| 86 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments