Skip to content
97 changes: 91 additions & 6 deletions .github/workflows/conda-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ jobs:
url: https://pypi.org/p/ml-metadata/
permissions:
contents: read
id-token: write
steps:
- name: Retrieve wheels
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
Expand All @@ -115,10 +116,94 @@ jobs:
run: |
ls -lAs wheels/

- name: Upload to PyPI
# zizmor: ignore[use-trusted-publishing]
uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # release/v1
- name: Set up Python
uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6
with:
packages_dir: wheels/
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
python-version: '3.11'

- name: Authenticate to Google Cloud (OSS Exit Gate)
uses: google-github-actions/auth@7c6bc770dae815cd3e89ee6cdf493a5fab2cc093 # v3.0.0
with:
workload_identity_provider: 'projects/305452601764/locations/global/workloadIdentityPools/builders/providers/github'

- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@aa5489c8933f4cc7a4f7d45035b3b1440c9c10db # v3.0.1

- name: Clean up failed staging attempt for this exact version (if any)
run: |
python3 - <<'PY'
import email, glob, re, subprocess, tarfile, zipfile
seen = set()
for f in sorted(glob.glob("wheels/**/*.whl", recursive=True) + glob.glob("wheels/**/*.tar.gz", recursive=True)):
try:
if f.endswith(".whl"):
with zipfile.ZipFile(f) as zf:
meta = [n for n in zf.namelist() if n.endswith(".dist-info/METADATA")][0]
msg = email.message_from_bytes(zf.read(meta))
else:
with tarfile.open(f) as tf:
info = [m for m in tf.getmembers() if m.name.endswith("/PKG-INFO") or m.name == "PKG-INFO"][0]
msg = email.message_from_bytes(tf.extractfile(info).read())
pkg = re.sub(r"[-_.]+", "-", msg["Name"].strip()).lower()
ver = msg["Version"].strip()
except Exception:
continue
if (pkg, ver) in seen:
continue
seen.add((pkg, ver))
if subprocess.run([
"gcloud", "artifacts", "versions", "describe", ver,
f"--package={pkg}", "--project=oss-exit-gate-prod", "--location=us", "--repository=ml-metadata--pypi"
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode == 0:
subprocess.run([
"gcloud", "artifacts", "versions", "delete", ver,
f"--package={pkg}", "--project=oss-exit-gate-prod", "--location=us", "--repository=ml-metadata--pypi", "--quiet"
], check=False)
PY

- name: Upload to PyPI via OSS Exit Gate
run: |
python -m pip install --upgrade pip twine
TWINE_PASSWORD="$(gcloud auth print-access-token)"
echo "::add-mask::${TWINE_PASSWORD}"
python -m twine upload \
--repository-url "https://us-python.pkg.dev/oss-exit-gate-prod/ml-metadata--pypi/" \
-u oauth2accesstoken \
-p "${TWINE_PASSWORD}" \
wheels/*
python3 - wheels/* <<'PY'
import email, json, re, sys, tarfile, zipfile
pairs = set()
for path in sys.argv[1:]:
if path.endswith(".whl"):
with zipfile.ZipFile(path) as zf:
for name in zf.namelist():
if name.endswith(".dist-info/METADATA"):
msg = email.message_from_bytes(zf.read(name))
pkg = re.sub(r"[-_.]+", "-", msg["Name"].strip()).lower()
pairs.add((pkg, msg["Version"].strip()))
break
elif path.endswith(".tar.gz"):
with tarfile.open(path, "r:gz") as tf:
for member in tf.getmembers():
if member.name.endswith("/PKG-INFO") and member.name.count("/") == 1:
f = tf.extractfile(member)
if f is not None:
msg = email.message_from_bytes(f.read())
pkg = re.sub(r"[-_.]+", "-", msg["Name"].strip()).lower()
pairs.add((pkg, msg["Version"].strip()))
break
if not pairs:
raise SystemExit("ERROR: No .whl/.tar.gz package metadata found for manifest.json")
manifest = {
"publish_all": False,
"publishing_groups": [{
"packages": [{"name": pkg, "version": ver} for pkg, ver in sorted(pairs)]
}],
}
with open("manifest.json", "w", encoding="utf-8") as fh:
json.dump(manifest, fh, indent=2)
fh.write("\n")
PY
gcloud storage cp manifest.json \
"gs://oss-exit-gate-prod-projects-bucket/ml-metadata/pypi/manifests/manifest-${{ github.run_id }}-${{ github.run_attempt }}.json"
Loading