|
| 1 | +name: Publish to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - "v*" # triggers on v1.12.3, v2.0.0, etc. |
| 7 | + |
| 8 | +# Least privilege by default: every job gets read-only contents and nothing |
| 9 | +# else. The publish job re-declares the extra scopes it actually needs. |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + |
| 13 | +jobs: |
| 14 | + build: |
| 15 | + name: Build distribution |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 |
| 19 | + - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # 6.2.0 |
| 20 | + with: |
| 21 | + python-version: "3.12" |
| 22 | + - name: Install build |
| 23 | + run: pip install build |
| 24 | + - name: Build sdist and wheel |
| 25 | + run: python -m build |
| 26 | + - name: Upload build artifacts |
| 27 | + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # 7.0.1 |
| 28 | + with: |
| 29 | + name: dist |
| 30 | + path: dist/ |
| 31 | + |
| 32 | + sbom: |
| 33 | + name: Generate SBOM |
| 34 | + needs: build |
| 35 | + runs-on: ubuntu-latest |
| 36 | + steps: |
| 37 | + - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 |
| 38 | + - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # 6.2.0 |
| 39 | + with: |
| 40 | + python-version: "3.12" |
| 41 | + - name: Download build artifacts |
| 42 | + uses: actions/download-artifact@ad191675b41f6a5b46da9a048cb6893812da158b # 8.0.1 |
| 43 | + with: |
| 44 | + name: dist |
| 45 | + path: dist/ |
| 46 | + - name: Generate SBOM (Python dependencies) |
| 47 | + run: | |
| 48 | + # Install the freshly built wheel into an ISOLATED venv so the SBOM |
| 49 | + # captures only the package's own resolved dependencies, and not the |
| 50 | + # SBOM tool itself or anything else present on the runner. |
| 51 | + python -m venv .sbom-venv |
| 52 | + .sbom-venv/bin/pip install --upgrade pip |
| 53 | + .sbom-venv/bin/pip install dist/*.whl |
| 54 | +
|
| 55 | + # Install cyclonedx-bom in the runner's default environment and point |
| 56 | + # it at the isolated venv's interpreter. Because the tool lives in a |
| 57 | + # different environment than the one being scanned, it never appears |
| 58 | + # as a component in the generated bill of materials. |
| 59 | + pip install cyclonedx-bom |
| 60 | +
|
| 61 | + TAG="${GITHUB_REF_NAME}" # e.g. v1.12.3 |
| 62 | + PKG="${GITHUB_REPOSITORY##*/}" # e.g. grid2op |
| 63 | + cyclonedx-py environment .sbom-venv/bin/python \ |
| 64 | + --output-format json \ |
| 65 | + --outfile "${PKG}-${TAG}-sbom.cdx.json" |
| 66 | + - name: Upload SBOM artifact |
| 67 | + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # 7.0.1 |
| 68 | + with: |
| 69 | + name: sbom |
| 70 | + path: "*-sbom.cdx.json" |
| 71 | + |
| 72 | + publish: |
| 73 | + name: Publish to PyPI |
| 74 | + needs: [build, sbom] |
| 75 | + runs-on: ubuntu-latest |
| 76 | + environment: pypi # must match the environment name from the Trusted Publisher config |
| 77 | + permissions: |
| 78 | + contents: write # needed to attach assets to the GitHub Release |
| 79 | + id-token: write # needed for OIDC trusted publishing + Sigstore |
| 80 | + steps: |
| 81 | + - name: Download build artifacts |
| 82 | + uses: actions/download-artifact@ad191675b41f6a5b46da9a048cb6893812da158b # 8.0.1 |
| 83 | + with: |
| 84 | + name: dist |
| 85 | + path: dist/ |
| 86 | + - name: Download SBOM |
| 87 | + uses: actions/download-artifact@ad191675b41f6a5b46da9a048cb6893812da158b # 8.0.1 |
| 88 | + with: |
| 89 | + name: sbom |
| 90 | + path: sbom/ |
| 91 | + - name: Publish to PyPI (with Sigstore attestations) |
| 92 | + uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b |
| 93 | + with: |
| 94 | + attestations: true # this is what triggers Sigstore signing |
| 95 | + - name: Attach SBOM to GitHub Release |
| 96 | + uses: softprops/action-gh-release@b4309332981a82ec1c5618f44dd2e27cc8bfbfda |
| 97 | + with: |
| 98 | + files: sbom/*-sbom.cdx.json |
0 commit comments