Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

ci: bootstrap new PyPI modules with refreshed OIDC credentials

Bingxi Zhao (Frank)6fab5deb8979
diff --git a/.github/workflows/pypi-release.yml b/.github/workflows/pypi-release.yml
index 1be3bd165..21bcb238e 100644
--- a/.github/workflows/pypi-release.yml
+++ b/.github/workflows/pypi-release.yml
@@ -24,6 +24,10 @@ on:
description: Skip files already uploaded by a partial release
default: false
type: boolean
+ bootstrap:
+ description: Wait for first-time module publishers to be registered in PyPI
+ default: false
+ type: boolean
env:
RELEASE_TAG: ${{ inputs.tag || github.event.release.tag_name }}
@@ -178,8 +182,49 @@ jobs:
PY
- name: Publish module distributions to PyPI
+ if: ${{ !inputs.bootstrap }}
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: dist/deeptutor/
print-hash: true
skip-existing: ${{ inputs.resume || false }}
+
+ - name: Bootstrap new module projects with Trusted Publishing
+ if: ${{ inputs.bootstrap }}
+ timeout-minutes: 35
+ env:
+ TWINE_NON_INTERACTIVE: "1"
+ run: |
+ python -m pip install --upgrade pypi-attestations "twine>=6.1"
+ python -m pypi_attestations sign dist/deeptutor/*.whl
+ python - <<'PY'
+ from pathlib import Path
+ import re
+ import subprocess
+ import sys
+ import time
+
+ # PyPI permits one pending project per publisher configuration.
+ # Each new Twine process exchanges a fresh OIDC token, converting
+ # the next pending publisher without needing a long-lived API key.
+ files = sorted(str(path) for path in Path("dist/deeptutor").glob("*"))
+ deadline = time.monotonic() + 1800
+ while True:
+ result = subprocess.run(
+ [sys.executable, "-m", "twine", "upload", "--non-interactive",
+ "--verbose", "--skip-existing", "--attestations", *files],
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
+ )
+ print(result.stdout, flush=True)
+ if result.returncode == 0:
+ break
+ output = " ".join(re.sub(r"\x1b\[[0-9;]*m", "", result.stdout).split())
+ waiting_for_publisher = (
+ "Non-user identities cannot create new projects" in output
+ or "isn't allowed to upload to project" in output
+ )
+ if not waiting_for_publisher or time.monotonic() >= deadline:
+ raise SystemExit(result.returncode)
+ print("Waiting for the next PyPI pending publisher; retrying in 10s.", flush=True)
+ time.sleep(10)
+ PY