From ec738f556a1948343cc6184eccf87e32e439887b Mon Sep 17 00:00:00 2001 From: Riyad Khan <65298773+rr3khan@users.noreply.github.com> Date: Thu, 3 Dec 2026 16:26:01 -0500 Subject: [PATCH 1/2] Publish to PyPI with trusted publishing from GitHub Actions Add a publish-pypi.yml workflow that builds the wheels and sdist and publishes them to PyPI over OIDC, so releases no longer need a PyPI token on a maintainer's machine. Publishing runs in the `pypi` environment, which gates it behind a reviewer approval, and a dry run is the default. The build job checks that every expected wheel exists with the right platform tag and native library, since build-wheels.sh doesn't stop when a single build fails. Pull requests that touch the build run that job as a check. build-wheels.sh gains a USE_PYENV=false mode so CI can call pythonX.Y directly, and the Makefile exposes PYTHON_VERSIONS so CI and local releases build the same versions. The local `make release` path stays as a fallback. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/publish-pypi.yml | 272 ++++++++++++++++++++++++++++ Makefile | 4 + src/release/README.md | 72 +++++++- src/release/scripts/build-wheels.sh | 31 +++- src/release/scripts/release.sh | 6 +- 5 files changed, 373 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/publish-pypi.yml diff --git a/.github/workflows/publish-pypi.yml b/.github/workflows/publish-pypi.yml new file mode 100644 index 00000000..1bc64f82 --- /dev/null +++ b/.github/workflows/publish-pypi.yml @@ -0,0 +1,272 @@ +name: Publish to PyPI (trusted publishing) + +# Builds the onepassword-sdk wheels and sdist and publishes them to PyPI with PyPI trusted +# publishing (OIDC), so no PyPI token is involved. See src/release/README.md. +# +# The trusted publisher for onepassword-sdk on pypi.org is bound to this file +# (publish-pypi.yml) and the `pypi` environment. Renaming either breaks publishing. +# +# PyPI doesn't check the branch, and workflow_dispatch can run this workflow from any branch +# (using that branch's copy of this file). The `pypi` environment's protection rules are what +# limit publishing: deployment branches `main`, `beta` and `release/*`, and required reviewers. +# +# Pull requests that change the build run only the build job, as a check. They never reach +# the publish job. +# +# The workflow doesn't bump versions or commit. Those come from the merged release PR +# (make prep-release). After a real (non-dry-run) publish, the `release` job creates the +# vX.Y.Z tag and the GitHub release on the published commit. + +on: + workflow_dispatch: + inputs: + channel: + description: Release channel + type: choice + options: + - beta + - stable + required: true + dry_run: + description: Dry run (builds and checks the distributions; nothing is published) + type: boolean + default: true + pull_request: + paths: + - .github/workflows/publish-pypi.yml + - src/release/scripts/build-wheels.sh + - setup.py + - Makefile + +permissions: + contents: read + +concurrency: + group: ${{ github.event_name == 'pull_request' && format('publish-pypi-pr-{0}', github.ref) || 'publish-pypi' }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + build: + name: Build${{ github.event_name == 'workflow_dispatch' && format(' ({0}{1})', inputs.channel, inputs.dry_run && ', dry run' || '') || '' }} + runs-on: ubuntu-latest + permissions: + contents: read + outputs: + version: ${{ steps.version.outputs.version }} + env: + EVENT: ${{ github.event_name }} + CHANNEL: ${{ inputs.channel }} + DRY_RUN: ${{ inputs.dry_run }} + steps: + - name: Harden the runner (Audit all outbound calls) + uses: step-security/harden-runner@ccd8616d44fd3846e67624a50d5aad6d37bf2d25 # v2.22.1 + with: + egress-policy: audit + + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + # The Makefile's PYTHON_VERSIONS is the single list of Python versions we ship wheels for. + - name: Read Python versions + id: pythons + run: | + versions=$(make -s print-python-versions) + echo "Python versions: $versions" + echo "versions=$versions" >> "$GITHUB_OUTPUT" + { + echo "list<> "$GITHUB_OUTPUT" + + - name: Set up Python + uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 + with: + python-version: ${{ steps.pythons.outputs.list }} + + # version.py is the source of truth. This workflow doesn't bump versions itself. + - name: Resolve and check version + id: version + run: | + version=$(awk -F "['\"]" '/SDK_VERSION =/{print $2}' version.py) + echo "version.py: SDK_VERSION=$version" + [ -n "$version" ] || { echo "::error::Couldn't read SDK_VERSION from version.py"; exit 1; } + echo "version=$version" >> "$GITHUB_OUTPUT" + + # Pull requests only check that the build works. + [ "$EVENT" = "workflow_dispatch" ] || exit 0 + + fail=0 + case "$CHANNEL" in + beta) + [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+b[0-9]+$ ]] \ + || { echo "::error::channel 'beta' needs a beta version like 0.4.0b1, got $version"; fail=1; } ;; + stable) + [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] \ + || { echo "::error::channel 'stable' needs a version like 0.4.0, got $version"; fail=1; } ;; + esac + + # PyPI never accepts the same file twice, so catch a version that was already released. + status=$(curl -s -o /dev/null -w '%{http_code}' "https://pypi.org/pypi/onepassword-sdk/$version/json") + if [ "$status" = "200" ]; then + if [ "$DRY_RUN" = "true" ]; then + echo "::warning::onepassword-sdk $version is already on PyPI. A real publish of this version would fail." + else + echo "::error::onepassword-sdk $version is already on PyPI. Bump the version in the release PR first." + fail=1 + fi + fi + exit "$fail" + + - name: Install build tools + env: + PYTHONS: ${{ steps.pythons.outputs.versions }} + run: | + for v in $PYTHONS; do + "python$v" -m pip install --disable-pip-version-check --upgrade build + done + python -m pip install --disable-pip-version-check --upgrade twine + + - name: Build wheels and sdist + env: + USE_PYENV: "false" + run: make build-wheels + + # build-wheels.sh doesn't stop on a failed build, so check that every expected file + # exists, with the right platform tag and native library. + - name: Check distributions + env: + PYTHONS: ${{ steps.pythons.outputs.versions }} + VERSION: ${{ steps.version.outputs.version }} + run: | + expected=() + for v in $PYTHONS; do + tag="cp${v/./}" + mac_x86="macosx_10_9_x86_64" + [ "$v" = "3.13" ] && mac_x86="macosx_10_13_x86_64" + for plat in "$mac_x86" macosx_11_0_arm64 manylinux_2_32_x86_64 manylinux_2_32_aarch64 win_amd64; do + expected+=("onepassword_sdk-${VERSION}-${tag}-${tag}-${plat}.whl") + done + done + expected+=("onepassword_sdk-${VERSION}.tar.gz") + + if ! diff <(printf '%s\n' "${expected[@]}" | sort) <(find dist -maxdepth 1 -type f -printf '%f\n' | sort); then + echo "::error::dist/ doesn't match the expected files (< expected, > built)" + exit 1 + fi + + fail=0 + for whl in dist/*.whl; do + case "$whl" in + *macosx*x86_64.whl) lib=onepassword/lib/x86_64/libop_uniffi_core.dylib ;; + *macosx*arm64.whl) lib=onepassword/lib/aarch64/libop_uniffi_core.dylib ;; + *manylinux*x86_64.whl) lib=onepassword/lib/x86_64/libop_uniffi_core.so ;; + *manylinux*aarch64.whl) lib=onepassword/lib/aarch64/libop_uniffi_core.so ;; + *win_amd64.whl) lib=onepassword/lib/x86_64/op_uniffi_core.dll ;; + *) echo "::error::unexpected wheel $whl"; fail=1; continue ;; + esac + # Platform wheels keep the package under onepassword_sdk-.data/purelib/. + unzip -Z1 "$whl" | grep -qE "(^|/)${lib}\$" || { echo "::error::$whl is missing $lib"; fail=1; } + done + [ "$fail" = 0 ] || exit 1 + + twine check --strict dist/* + + { + echo "### onepassword-sdk $VERSION" + echo + echo "Built ${#expected[@]} files:" + echo + # shellcheck disable=SC2016 # the backticks are Markdown + printf -- '- `%s`\n' "${expected[@]}" + } >> "$GITHUB_STEP_SUMMARY" + + - name: Upload distributions + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: dist + path: dist/ + if-no-files-found: error + + # Only this job gets an OIDC token, and it runs nothing from the repo: it uploads the + # files the build job made. + publish: + name: Publish to PyPI + needs: build + if: ${{ github.event_name == 'workflow_dispatch' && !inputs.dry_run }} + runs-on: ubuntu-latest + # Must match the environment on the PyPI trusted publisher config. + environment: + name: pypi + url: https://pypi.org/project/onepassword-sdk/${{ needs.build.outputs.version }}/ + permissions: + id-token: write # PyPI trusted publishing (OIDC) + steps: + - name: Harden the runner (Audit all outbound calls) + uses: step-security/harden-runner@ccd8616d44fd3846e67624a50d5aad6d37bf2d25 # v2.22.1 + with: + egress-policy: audit + + - name: Download distributions + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: dist + path: dist/ + + # Uploads dist/* and attaches PEP 740 attestations (on by default with trusted publishing). + - name: Publish to PyPI + uses: step-security/gh-action-pypi-publish@eccc9148d1fa355476d98c01819ec34b75a86f24 # v1.14.2 + + # Creates the vX.Y.Z tag and GitHub release on the commit that was just published. + # Kept in its own job so only this job can write to the repo, and the publish job + # (which holds the PyPI OIDC token) stays read-only. + # + # The tag is a lightweight tag created by github-actions[bot], so it isn't signed + # itself. It points at the release PR's merge commit, which GitHub signs when the PR + # is merged in the web UI. + release: + name: GitHub release + needs: [build, publish] + runs-on: ubuntu-latest + permissions: + contents: write # create the tag and GitHub release + env: + VERSION: ${{ needs.build.outputs.version }} + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + steps: + - name: Harden the runner (Audit all outbound calls) + uses: step-security/harden-runner@ccd8616d44fd3846e67624a50d5aad6d37bf2d25 # v2.22.1 + with: + egress-policy: audit + + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + # Safe to re-run: skips if the release already exists, and refuses to reuse a tag + # that points at a different commit. + - name: Create tag and GitHub release + run: | + tag="v$VERSION" + if gh release view "$tag" >/dev/null 2>&1; then + echo "::notice::GitHub release $tag already exists, skipping" + exit 0 + fi + + # Commit the tag points at, if it exists (peeled, for annotated tags). + tagged=$(git ls-remote origin "refs/tags/$tag" "refs/tags/$tag^{}" | sort -k2 | tail -n1 | cut -f1) + if [ -n "$tagged" ] && [ "$tagged" != "$GITHUB_SHA" ]; then + echo "::error::tag $tag already exists on $tagged, not on the published commit $GITHUB_SHA" + exit 1 + fi + + args=(--target "$GITHUB_SHA" --title "Release $VERSION" --notes-file src/release/RELEASE-NOTES) + if [[ "$VERSION" == *b* ]]; then + args+=(--prerelease) + fi + gh release create "$tag" "${args[@]}" + echo "Created GitHub release $tag on $GITHUB_SHA" >> "$GITHUB_STEP_SUMMARY" diff --git a/Makefile b/Makefile index 17592d1d..8ed2c72e 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,10 @@ prep-release: build-wheels: src/release/scripts/build-wheels.sh $(PYTHON_VERSIONS) +# Used by .github/workflows/publish-pypi.yml so CI builds the same Python versions as a local release. +print-python-versions: + @echo $(PYTHON_VERSIONS) + release/install-dependencies: # Install latest version of pyenv if not already installed brew install pyenv diff --git a/src/release/README.md b/src/release/README.md index ab88ca00..a1fb37a6 100644 --- a/src/release/README.md +++ b/src/release/README.md @@ -1,6 +1,6 @@ ## How to Prepare a Release for the Python SDK -Before running this script, the user must make sure that they have the write permissions to the Python SDK repository. +Before running these scripts, make sure that you have write permissions to the Python SDK repository. Run this make command to install all dependencies required for the Python SDK release process. ``` @@ -18,17 +18,77 @@ Follow the scripts instructions and the release has now been prepped. Step 3. Ensure that the correct files have been updated - i.e. version/build files, release-notes has been updated. Check the latest commit on the branch to see your changes. -Step 4. To build the wheels and source distribution for PyPi, run in the root of the repo: +Step 4. Open a PR for the release branch and merge it. Merge into `beta` for a beta release, or `main` for a stable release. + +## How to Publish a Release + +The SDK is published to PyPI by the [Publish to PyPI (trusted publishing)](../../.github/workflows/publish-pypi.yml) workflow. It authenticates to PyPI with OIDC, so no PyPI token is needed, and PyPI attaches an attestation to each file. + +### Before you start + +- Merge the release PR first (Step 4 above). +- Run the workflow from the branch you merged into, never from the `rc/...` or `sdk-core/...` branch. The `pypi` environment only allows `main`, `beta` and `release/*`. +- The workflow publishes the version already in `version.py`. It fails if that version doesn't match the channel (`0.4.0b1` for beta, `0.4.0` for stable) or is already on PyPI. It doesn't bump versions or commit. + +### Inputs + +| Input | Beta release | Stable release | +| --- | --- | --- | +| Use workflow from | `beta` | `main` | +| Release channel | `beta` | `stable` | +| Dry run | Checked for the first run, unchecked for the real publish | Checked for the first run, unchecked for the real publish | + +### Option 1: Run it from the GitHub UI + +1. Go to [Actions → Publish to PyPI (trusted publishing)](https://github.com/1Password/onepassword-sdk-python/actions/workflows/publish-pypi.yml) → **Run workflow**, pick the branch and set the inputs above. **Dry run** is checked by default. +2. **Check the dry run.** The **Build** job builds the wheels and sdist, checks that every expected file is there with the right platform tag and native library, and runs `twine check`. Its summary lists the files. The built files are attached to the run as the `dist` artifact. Nothing is published, and the publish and GitHub release jobs are skipped. +3. **Run it again with Dry run unchecked.** +4. **Get the run approved.** The **Publish to PyPI** job waits for approval on the `pypi` environment. Someone in `1Password/open-source` other than you approves it with **Review deployments** on the run page. You can't approve your own run. +5. **The GitHub release job runs on its own** after the publish. It creates the `v` tag and the GitHub release from `src/release/RELEASE-NOTES`. Beta versions are marked as pre-releases. + +### Option 2: Run it from the CLI + +Same workflow and inputs. The approval still has to be done by a reviewer in GitHub. + +```bash +# Dry run of a beta release (set --ref main -f channel=stable for a stable release) +gh workflow run publish-pypi.yml --repo 1Password/onepassword-sdk-python --ref beta \ + -f channel=beta -f dry_run=true + +# Find the run and follow it +gh run list --repo 1Password/onepassword-sdk-python --workflow publish-pypi.yml --limit 1 +gh run watch --repo 1Password/onepassword-sdk-python + +# Real publish, after the dry run looks right +gh workflow run publish-pypi.yml --repo 1Password/onepassword-sdk-python --ref beta \ + -f channel=beta -f dry_run=false +``` + +### Confirm the release + +- The [PyPI release history](https://pypi.org/project/onepassword-sdk/#history) shows the new version, with 26 files (25 wheels and the sdist). +- [GitHub releases](https://github.com/1Password/onepassword-sdk-python/releases) has the new `v` release on the merged commit. + +### How publishing is set up + +- **PyPI:** the `onepassword-sdk` project has a trusted publisher (Manage → Publishing) for owner `1Password`, repository `onepassword-sdk-python`, workflow `publish-pypi.yml` and environment `pypi`. Renaming the workflow file or the environment breaks publishing until the trusted publisher is updated to match. +- **GitHub:** the `pypi` environment allows deployments from `main`, `beta` and `release/*` only, requires a review from `1Password/open-source`, and has "Prevent self-review" on. It has no secrets. + +## Fallback: Publish from Your Machine + +Use this only if the workflow can't be used. It needs the PyPI publishing token and will be removed once publishing through GitHub Actions has been proven. + +Step 1. To build the wheels and source distribution for PyPI, run in the root of the repo: ``` make build-wheels ``` -Step 5. Ensure your GITHUB_TOKEN environment variable is set as this will allow you to create the tags/release and push it. +Step 2. Ensure your GITHUB_TOKEN environment variable is set as this will allow you to create the tags/release and push it. -Step 6. Ensure you have the PyPi credentials to login when uploading the source and wheels to PyPi. +Step 3. Ensure you have the PyPI credentials to log in when uploading the source and wheels to PyPI. -Step 7. If everything looks good, at the root of the repo, run: +Step 4. If everything looks good, at the root of the repo, run: ``` make release ``` -Step 8. Congratulations, you have released the newest Python SDK! \ No newline at end of file +Congratulations, you have released the newest Python SDK! diff --git a/src/release/scripts/build-wheels.sh b/src/release/scripts/build-wheels.sh index 484875d3..3d8af41d 100755 --- a/src/release/scripts/build-wheels.sh +++ b/src/release/scripts/build-wheels.sh @@ -7,6 +7,26 @@ output_version_file="version.py" # The list of python verisons the SDKs release for python_versions=("$@") +# Locally, the script switches between Python versions with pyenv. In CI (USE_PYENV=false), +# it calls pythonX.Y from PATH instead, as installed by actions/setup-python. +use_pyenv="${USE_PYENV:-true}" +current_python="" + +select_python() { + current_python=$1 + if [[ "$use_pyenv" == "true" ]]; then + pyenv local "$current_python" + fi +} + +run_python() { + if [[ "$use_pyenv" == "true" ]]; then + pyenv exec python3 "$@" + else + "python${current_python}" "$@" + fi +} + # Minimum glibc version we support glibc_version=2-32 @@ -48,10 +68,11 @@ build_wheels() { Darwin) macos_version= # Min MacOS version for Python 3.13+ is 10.13 - python_version=$(pyenv exec python3 --version 2>&1) + local interpreter_version + interpreter_version=$(run_python --version 2>&1) if [[ "$machine_platform" == "x86_64" ]]; then - if [[ "$python_version" == "Python 3.13"* ]]; then + if [[ "$interpreter_version" == "Python 3.13"* ]]; then macos_version="10.13" else macos_version=$macOS_version_x86_64 @@ -74,7 +95,7 @@ build_wheels() { ;; esac - pyenv exec python3 -m build --wheel + run_python -m build --wheel rm -rf build } @@ -83,7 +104,7 @@ enforce_latest_code # Acquire the wheels for different OS for python_version in "${python_versions[@]}"; do - pyenv local $python_version + select_python "$python_version" build_wheels Darwin x86_64 build_wheels Darwin arm64 build_wheels Linux x86_64 @@ -92,4 +113,4 @@ for python_version in "${python_versions[@]}"; do done # Build Source as well incase wheels fails, pypi can install this as backup (standard practice) -pyenv exec python3 -m build --sdist \ No newline at end of file +run_python -m build --sdist \ No newline at end of file diff --git a/src/release/scripts/release.sh b/src/release/scripts/release.sh index 050c1489..600ad45d 100755 --- a/src/release/scripts/release.sh +++ b/src/release/scripts/release.sh @@ -1,6 +1,10 @@ #!/bin/bash -# Helper script to release the Python SDK +# Helper script to release the Python SDK from a maintainer's machine. +# +# This is the fallback path. Releases are normally published by the "Publish to PyPI +# (trusted publishing)" workflow (.github/workflows/publish-pypi.yml), which also creates +# the tag and GitHub release. Don't run this script for a release that workflow published. set -e From 45af78a4e8ea33f9931a0274754c2763918547c8 Mon Sep 17 00:00:00 2001 From: Riyad Khan <65298773+rr3khan@users.noreply.github.com> Date: Thu, 3 Dec 2026 16:30:19 -0500 Subject: [PATCH 2/2] Pin the PyPI build job to Ubuntu 24.04 ubuntu-latest now resolves to Ubuntu 26.04, which actions/setup-python has no Python 3.9 build for, so the build job failed while installing Python. Ubuntu 24.04 has builds for every version we ship wheels for. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/publish-pypi.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/publish-pypi.yml b/.github/workflows/publish-pypi.yml index 1bc64f82..fe7b6dec 100644 --- a/.github/workflows/publish-pypi.yml +++ b/.github/workflows/publish-pypi.yml @@ -48,7 +48,9 @@ concurrency: jobs: build: name: Build${{ github.event_name == 'workflow_dispatch' && format(' ({0}{1})', inputs.channel, inputs.dry_run && ', dry run' || '') || '' }} - runs-on: ubuntu-latest + # Pinned rather than ubuntu-latest: actions/setup-python has no Python 3.9 build for + # Ubuntu 26.04, and we still ship 3.9 wheels. + runs-on: ubuntu-24.04 permissions: contents: read outputs: