Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 121 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,132 @@ jobs:
name: pure-wheel-${{ matrix.os }}-py${{ matrix.python }}
path: dist-pure

# ------------------------------------------------------------------
# PR regression for the release PyPI checks — NO publishing, NO
# environment/OIDC, read-only permissions, and NO PyPI registry
# uploads and NO PyPI JSON API requests from these checks themselves
# (the tooling install steps and the consumer probe may still contact
# the package index for packages, and GitHub artifacts are uploaded
# for the round-trip job — only PyPI uploads/requests are absent):
#
# 1. Unit tests exercise the EXACT preflight / post-upload functions
# the release workflow calls (scripts/pypi_preflight.py) against
# mocked HTTP responses (404 / partial / full same-hash repeat /
# conflicting digest / duplicate filenames / malformed payload /
# post-upload match & mismatch) with fake local files.
# 2. The sdist is built with the same provenance gate as the release
# sdist job, then installed from the EXACT built archive into a
# fresh venv OUTSIDE the checkout via the shared consumer probe.
# ------------------------------------------------------------------
pypi-publish-regression:
name: PyPI preflight regression (no PyPI registry requests, no PyPI uploads)
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: "3.12"
- uses: dtolnay/rust-toolchain@stable
- name: Install pytest (package-index access for tooling only)
run: python -m pip install --upgrade pip pytest

# Unit tests over the shared implementation — mocked openers only.
# PYTHONPATH exposes the pure package so tests/conftest.py can import
# ktav without building the native extension.
- name: Pytest — preflight/post-upload regression (mocked HTTP)
env:
PYTHONPATH: python
run: python -m pytest tests/ci/test_pypi_preflight.py -v

# Same provenance gate + sdist build as the release sdist job.
- name: Generate Cargo.lock (no --locked)
run: cargo generate-lockfile
- name: Check Rust provenance
run: python scripts/check_rust_provenance.py
- name: Build sdist
uses: PyO3/maturin-action@v1
with:
command: sdist
args: --out dist
- name: Assert the artifact is exactly one sdist and record its sha256
shell: bash
run: |
set -euo pipefail
count="$(ls dist | wc -l)"
test "$count" -eq 1 || { echo "expected exactly one file in dist:"; ls dist; exit 1; }
test -f dist/ktav-0.9.0.tar.gz || { echo "missing dist/ktav-0.9.0.tar.gz:"; ls dist; exit 1; }
sha256sum dist/ktav-0.9.0.tar.gz
sha256sum dist/ktav-0.9.0.tar.gz | cut -d' ' -f1 > dist/sdist-sha256.txt

# Hand the built sdist + its recorded sha256 sidecar to the
# round-trip job, which downloads them as an Actions artifact and
# re-verifies checksum equality against the DOWNLOADED copy (until
# now equality was only checked in the release job, skipped here by
# dry-run). No release, no publish, read-only permissions.
- name: Upload sdist + sha256 sidecar for the round-trip job
uses: actions/upload-artifact@v7
with:
name: pr-sdist
path: dist
retention-days: 1

# Safe artifact round-trip: download the sdist + sidecar uploaded by
# pypi-publish-regression into a DIFFERENT directory, compare the
# downloaded archive's sha256 against the sidecar (checksum coverage
# previously existed only in the release job), then run the shared
# consumer probe against exactly that DOWNLOADED file. No release, no
# publish, no extra privileges.
sdist-checksum-roundtrip:
name: Sdist artifact round-trip checksum (no release, no publish)
runs-on: ubuntu-latest
needs: [pypi-publish-regression]
permissions:
contents: read
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: "3.12"
- uses: actions/download-artifact@v8
with:
name: pr-sdist
path: downloaded-dist
- name: Compare downloaded sdist sha256 against the sidecar
shell: bash
run: |
set -euo pipefail
recorded="$(tr -d '[:space:]' < downloaded-dist/sdist-sha256.txt)"
if [[ ! "$recorded" =~ ^[0-9a-f]{64}$ ]]; then
echo "sidecar is not a 64-hex sha256: $recorded"; exit 1
fi
test -f downloaded-dist/ktav-0.9.0.tar.gz \
|| { echo "downloaded archive ktav-0.9.0.tar.gz is missing"; exit 1; }
actual="$(sha256sum downloaded-dist/ktav-0.9.0.tar.gz | cut -d' ' -f1)"
echo "recorded=$recorded"
echo "actual=$actual"
test "$actual" = "$recorded" \
|| { echo "downloaded sdist sha256 differs from sidecar"; exit 1; }
- uses: dtolnay/rust-toolchain@stable
- name: Consumer probe — install the DOWNLOADED sdist outside the checkout
shell: bash
run: |
set -euo pipefail
sdist="$PWD/downloaded-dist/ktav-0.9.0.tar.gz"
test -f "$sdist"
scratch="$(mktemp -d)"
cd "$scratch"
python "$GITHUB_WORKSPACE/scripts/pypi_preflight.py" consumer-probe \
--sdist "$sdist" \
--version "0.9.0"

# Guarantees the three OS × Python matrix all pass before merging; useful
# as a required check in branch protection without listing every cell.
test-summary:
name: Test summary
runs-on: ubuntu-latest
needs: [test, pure-mode]
needs: [test, pure-mode, pypi-publish-regression, sdist-checksum-roundtrip]
if: always()
steps:
- name: Fail if any matrix job failed
Expand Down
109 changes: 107 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,43 @@ jobs:
command: sdist
args: --out dist

- name: Assert the artifact is exactly one sdist and record its sha256
shell: bash
run: |
set -euo pipefail
# Exactly one distribution file must come out of the sdist build:
# the ktav 0.9.0 tar.gz. Anything else means the build swept in
# unexpected output and the artifact cannot be bound to a single
# checksum.
count="$(ls dist | wc -l)"
test "$count" -eq 1 || { echo "expected exactly one file in dist:"; ls dist; exit 1; }
test -f dist/ktav-0.9.0.tar.gz || { echo "missing dist/ktav-0.9.0.tar.gz:"; ls dist; exit 1; }
sha256sum dist/ktav-0.9.0.tar.gz
# SHA256 sidecar rides INSIDE dist, so the wheels-sdist artifact
# (uploaded with path: dist) carries archive + checksum together
# and the release job can prove it publishes the EXACT tested file.
sha256sum dist/ktav-0.9.0.tar.gz | cut -d' ' -f1 > dist/sdist-sha256.txt

- name: Install the EXACT built sdist in a clean venv outside the checkout
shell: bash
run: |
set -euo pipefail
sdist="$PWD/dist/ktav-0.9.0.tar.gz"
test -f "$sdist"
# The whole check runs from a scratch directory OUTSIDE the
# checkout tree; the venv gets ktav ONLY from the sdist file path —
# never from the checkout as a package source. (--no-index is not
# used on purpose: building the sdist needs maturin from the index,
# but the ktav package itself can only come from the sdist file.)
scratch="$(mktemp -d)"
cd "$scratch"
# The probe (venv creation, install of the EXACT sdist file path,
# installed-package assertions) lives in scripts/pypi_preflight.py
# and is shared verbatim with the PR CI job.
python "$GITHUB_WORKSPACE/scripts/pypi_preflight.py" consumer-probe \
--sdist "$sdist" \
--version "0.9.0"

- uses: actions/upload-artifact@v7
with:
name: wheels-sdist
Expand Down Expand Up @@ -317,11 +354,67 @@ jobs:
# Not a distribution file — keep it out of the PyPI upload.
rm dist/pure-wheel-sha256.txt

- name: Re-verify the downloaded sdist against the tested sha256
shell: bash
run: |
set -euo pipefail
sdist="dist/ktav-0.9.0.tar.gz"
test -f "$sdist" || { ls dist; exit 1; }
# Cross-job checksum equality rides on artifact integrity
# (upload/download of the `wheels-sdist` artifact); assert the
# published sdist is the EXACT file that was built, size-checked
# and install-tested in the sdist job.
echo "sdist sha256 (downloaded): $(sha256sum "$sdist")"
cat dist/sdist-sha256.txt
recorded="$(cut -d' ' -f1 dist/sdist-sha256.txt)"
actual="$(sha256sum "$sdist" | cut -d' ' -f1)"
test "$recorded" = "$actual" || {
echo "downloaded sdist does not match the tested sha256"; exit 1;
}
# Not a distribution file — keep it out of the PyPI upload.
rm dist/sdist-sha256.txt

# Single-source implementation: scripts/pypi_preflight.py is shared
# verbatim with the PR CI regression tests
# (tests/ci/test_pypi_preflight.py) — keep no logic inline here.
- name: PyPI pre-flight — hash-checked idempotency (fail closed)
id: pypi-preflight
shell: bash
run: |
set -euo pipefail
python scripts/pypi_preflight.py preflight \
--dist dist \
--project ktav \
--version "${GITHUB_REF_NAME#v}" \
--output "$GITHUB_OUTPUT"

- name: Publish to PyPI
if: steps.pypi-preflight.outputs.skip != 'true'
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: dist
skip-existing: true
# Upload ONLY the files the pre-flight proved absent from PyPI;
# an empty upload-dir here is impossible (skip=true would have
# short-circuited this step).
packages-dir: ${{ steps.pypi-preflight.outputs.upload-dir }}
# Deliberately NOT skip-existing: idempotent re-runs are decided
# by the sha256 pre-flight above, so an existing file with a
# different digest can never be silently accepted — and a race
# (file appearing after pre-flight) fails the upload, which is
# the safe outcome.
skip-existing: false
verify-metadata: true

- name: PyPI post-upload verification of every published digest
if: steps.pypi-preflight.outputs.skip != 'true'
shell: bash
run: |
set -euo pipefail
# Same single-source implementation as the pre-flight above;
# unit-tested in tests/ci/test_pypi_preflight.py.
python scripts/pypi_preflight.py post-upload \
--dist dist \
--project ktav \
--version "${GITHUB_REF_NAME#v}"

# ------------------------------------------------------------------
# GitHub Release — pulls notes from the matching `## [vX.Y.Z]`
Expand Down Expand Up @@ -361,6 +454,18 @@ jobs:
echo "__END_NOTES__"
} >> "$GITHUB_OUTPUT"

# Drop ONLY the sdist checksum sidecar from the release assets;
# pure-wheel-sha256.txt is an intentionally published checksum
# asset and must survive (a bare `rm -f dist/*.txt` deleted it).
- name: Drop the sdist checksum sidecar from the release assets
shell: bash
run: |
set -euo pipefail
rm -f dist/sdist-sha256.txt
test -f dist/pure-wheel-sha256.txt || {
echo "pure-wheel-sha256.txt missing; refusing to publish without it"; ls dist; exit 1;
}

- uses: softprops/action-gh-release@v3
with:
tag_name: ${{ github.ref_name }}
Expand Down
Loading
Loading