From 1c4994b54515dfe5e97f907db35dea35970ec4f6 Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Thu, 16 May 2024 17:18:43 +0200 Subject: [PATCH] ci: Add GitHub artifact attestations to package distribution (#2473) * Add generation of GitHub artifact attestations to built sdist and wheel before upload. c.f.: - https://github.blog/2024-05-02-introducing-artifact-attestations-now-in-public-beta/ - https://docs.github.com/en/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds * Add verification of artifact attestation before publishing to PyPI using the 'gh attestation verify' CLI API, added in v2.49.0. - c.f. https://github.com/cli/cli/releases/tag/v2.49.0 --- .github/workflows/publish-package.yml | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/.github/workflows/publish-package.yml b/.github/workflows/publish-package.yml index 38b455e8b5..edd27998dd 100644 --- a/.github/workflows/publish-package.yml +++ b/.github/workflows/publish-package.yml @@ -31,6 +31,11 @@ jobs: build: name: Build Python distribution runs-on: ubuntu-latest + permissions: + id-token: write + attestations: write + contents: read + steps: - uses: actions/checkout@v4 with: @@ -98,6 +103,16 @@ jobs: - name: List contents of wheel run: python -m zipfile --list dist/pyhf-*.whl + - name: Generate artifact attestation for sdist and wheel + # If publishing to TestPyPI or PyPI + if: >- + (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') && github.repository == 'scikit-hep/pyhf') + || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true' && github.repository == 'scikit-hep/pyhf') + || (github.event_name == 'release' && github.event.action == 'published' && github.repository == 'scikit-hep/pyhf') + uses: actions/attest-build-provenance@897ed5eab6ed058a474202017ada7f40bfa52940 # v1.0.0 + with: + subject-path: "dist/pyhf-*" + - name: Upload distribution artifact uses: actions/upload-artifact@v4 with: @@ -127,6 +142,26 @@ jobs: - name: List all files run: ls -lh dist + - name: Verify sdist artifact attestation + # If publishing to TestPyPI or PyPI + if: >- + (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') && github.repository == 'scikit-hep/pyhf') + || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true' && github.repository == 'scikit-hep/pyhf') + || (github.event_name == 'release' && github.event.action == 'published' && github.repository == 'scikit-hep/pyhf') + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: gh attestation verify dist/pyhf-*.tar.gz --repo ${{ github.repository }} + + - name: Verify wheel artifact attestation + # If publishing to TestPyPI or PyPI + if: >- + (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') && github.repository == 'scikit-hep/pyhf') + || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true' && github.repository == 'scikit-hep/pyhf') + || (github.event_name == 'release' && github.event.action == 'published' && github.repository == 'scikit-hep/pyhf') + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: gh attestation verify dist/pyhf-*.whl --repo ${{ github.repository }} + - name: Publish distribution 📦 to Test PyPI # Publish to TestPyPI on tag events of if manually triggered # Compare to 'true' string as booleans get turned into strings in the console