Skip to content
Open
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
274 changes: 274 additions & 0 deletions .github/workflows/publish-pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
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' || '') || '' }}
# 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:
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<<EOF"
tr ' ' '\n' <<< "$versions"
echo "EOF"
} >> "$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-<version>.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"
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 66 additions & 6 deletions src/release/README.md
Original file line number Diff line number Diff line change
@@ -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.
```
Expand All @@ -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<version>` 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 <run-id> --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<version>` 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!
Congratulations, you have released the newest Python SDK!
Loading
Loading