Skip to content

Commit 9ad9f63

Browse files
shixishclaude
andcommitted
ci: publish to PyPI via trusted publishing on v* tags
Add .github/workflows/publish-pypi.yml: verify (tag == pyproject version == diffbot.__version__, unit tests, pyright), build (uv build sdist + wheel, twine check --strict, artifact), and publish (tags only, environment "pypi", id-token: write, pypa/gh-action-pypi-publish@release/v1; no API token). workflow_dispatch is a dry run that never uploads. Concurrency never cancels an in-flight publish. release.yml (binaries) is unchanged. Document the release path (merge to main, push tag vX.Y.Z) in the README and mark `make release` as a manual fallback. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 40e98ba commit 9ad9f63

3 files changed

Lines changed: 154 additions & 1 deletion

File tree

‎.github/workflows/publish-pypi.yml‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
name: Publish to PyPI
2+
3+
# Publishes the `diffbot` sdist + wheel to PyPI via trusted publishing (OIDC):
4+
# no API tokens are stored anywhere. Triggered by pushing a version tag
5+
# (e.g. v3.4.0). workflow_dispatch runs verify + build + twine check only,
6+
# as a dry run; it never uploads.
7+
#
8+
# One-time setup (maintainer): on pypi.org > diffbot > Manage > Publishing, add
9+
# a GitHub publisher with owner `diffbot`, repository `diffbot-python`,
10+
# workflow `publish-pypi.yml`, environment `pypi`; and create a GitHub
11+
# environment named `pypi` in the repo settings (protection rules optional).
12+
#
13+
# The standalone `db` binaries are published separately by release.yml, which
14+
# fires on the same tag.
15+
on:
16+
push:
17+
tags: ["v*"]
18+
workflow_dispatch:
19+
20+
permissions:
21+
contents: read
22+
23+
# Never cancel an in-flight run: a cancelled publish could leave PyPI with only
24+
# some of the files for a version, and PyPI doesn't allow re-uploading them.
25+
concurrency:
26+
group: publish-pypi-${{ github.ref }}
27+
cancel-in-progress: false
28+
29+
jobs:
30+
verify:
31+
name: Verify version, tests, and types
32+
runs-on: ubuntu-latest
33+
timeout-minutes: 20
34+
steps:
35+
- uses: actions/checkout@v4
36+
37+
- name: Install uv
38+
uses: astral-sh/setup-uv@v5
39+
with:
40+
python-version: "3.12"
41+
42+
- name: Install dependencies (locked)
43+
run: uv sync --frozen --extra dev
44+
45+
# __version__ is read from the installed distribution metadata, so this
46+
# checks that pyproject.toml's version is what the package reports.
47+
- name: Check tag matches package version
48+
run: |
49+
set -euo pipefail
50+
pkg=$(grep -m1 '^version' pyproject.toml | cut -d'"' -f2)
51+
runtime=$(uv run --frozen python -c 'import diffbot; print(diffbot.__version__)')
52+
if [ "$pkg" != "$runtime" ]; then
53+
echo "::error::pyproject.toml version ${pkg} does not match diffbot.__version__ ${runtime}."
54+
exit 1
55+
fi
56+
if [ "${GITHUB_REF_TYPE}" = "tag" ]; then
57+
tag=${GITHUB_REF_NAME#v}
58+
if [ "$pkg" != "$tag" ]; then
59+
echo "::error::Tag v${tag} does not match pyproject.toml version ${pkg}. Bump pyproject.toml (e.g. 'make bump-patch') and re-tag."
60+
exit 1
61+
fi
62+
fi
63+
echo "Package version: ${pkg}"
64+
65+
# Unit tests only: pyproject's addopts deselect `-m live` by default.
66+
- name: Run tests
67+
run: uv run --frozen pytest
68+
69+
- name: Run pyright
70+
run: uv run --frozen pyright
71+
72+
build:
73+
name: Build sdist and wheel
74+
needs: verify
75+
runs-on: ubuntu-latest
76+
timeout-minutes: 15
77+
steps:
78+
- uses: actions/checkout@v4
79+
80+
- name: Install uv
81+
uses: astral-sh/setup-uv@v5
82+
with:
83+
python-version: "3.12"
84+
85+
- name: Build
86+
run: uv build --sdist --wheel --out-dir dist
87+
88+
- name: Check distributions
89+
run: uvx twine check --strict dist/*
90+
91+
- name: Check built version matches pyproject.toml
92+
run: |
93+
set -euo pipefail
94+
pkg=$(grep -m1 '^version' pyproject.toml | cut -d'"' -f2)
95+
ls dist
96+
test -f "dist/diffbot-${pkg}.tar.gz"
97+
test -f "dist/diffbot-${pkg}-py3-none-any.whl"
98+
99+
- name: Upload artifact
100+
uses: actions/upload-artifact@v4
101+
with:
102+
name: python-dist
103+
path: dist/
104+
if-no-files-found: error
105+
106+
publish:
107+
name: Publish to PyPI
108+
needs: build
109+
# Tag pushes only; workflow_dispatch stops after build.
110+
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
111+
runs-on: ubuntu-latest
112+
timeout-minutes: 15
113+
environment:
114+
name: pypi
115+
url: https://pypi.org/project/diffbot/
116+
permissions:
117+
id-token: write
118+
steps:
119+
- name: Download artifact
120+
uses: actions/download-artifact@v4
121+
with:
122+
name: python-dist
123+
path: dist/
124+
125+
- name: Publish
126+
uses: pypa/gh-action-pypi-publish@release/v1

‎Makefile‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,16 @@ _release-test:
6161
fi && \
6262
UV_PUBLISH_TOKEN="$$TOKEN" uv publish --publish-url https://test.pypi.org/legacy/
6363

64-
# Publish to real PyPI. Confirmation gate before upload (PyPI does not allow re-uploads).
64+
# MANUAL FALLBACK ONLY. The normal release path is CI: merge to main, then push
65+
# a tag vX.Y.Z matching pyproject.toml; .github/workflows/publish-pypi.yml
66+
# publishes to PyPI via trusted publishing (no token). Use this target only if
67+
# that workflow is unavailable. It uploads with a token from macOS Keychain
68+
# (service: pypi-token-pypi) after a confirmation gate (PyPI does not allow
69+
# re-uploads).
6570
release: build _release
6671

6772
_release:
73+
@echo "NOTE: 'make release' is the manual fallback; normally push tag vX.Y.Z and let publish-pypi.yml publish."
6874
@VERSION=$$(grep '^version' $(PYPROJECT) | head -1 | cut -d'"' -f2) && \
6975
STATUS=$$(curl -s -o /dev/null -w "%{http_code}" "https://pypi.org/pypi/$(PKG)/$$VERSION/json") && \
7076
if [ "$$STATUS" = "200" ]; then \

‎README.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,27 @@ environment variable or `~/.diffbot/credentials`:
525525
DIFFBOT_API_TOKEN=your_token python -m pytest -m live
526526
```
527527

528+
## Releasing
529+
530+
Releases are published by CI; nothing is uploaded from a laptop.
531+
532+
1. Bump the version in `pyproject.toml` (`make bump-patch`, `bump-minor`, or `bump-major`), update `CHANGELOG.md`, and merge to `main`.
533+
2. Tag the merge commit and push the tag:
534+
```bash
535+
git tag vX.Y.Z && git push origin vX.Y.Z
536+
```
537+
538+
The tag must match the `pyproject.toml` version. Pushing it runs two workflows:
539+
[`publish-pypi.yml`](.github/workflows/publish-pypi.yml) checks the version, runs the
540+
unit tests and pyright, builds the sdist and wheel, and publishes them to PyPI via
541+
[trusted publishing](https://docs.pypi.org/trusted-publishers/) (no API token), and
542+
[`release.yml`](.github/workflows/release.yml) builds the standalone `db` binaries
543+
and attaches them to a GitHub Release. Running `publish-pypi.yml` manually
544+
(workflow_dispatch) is a dry run: it builds and checks but never uploads.
545+
546+
`make release` (token from macOS Keychain) remains only as a manual fallback for
547+
when the workflow can't be used.
548+
528549
## Acknowledgements
529550

530551
Thanks to [@attilaolah](https://github.com/attilaolah), who published the original `diffbot` package in 2012 and looked after the name for 14 years.

0 commit comments

Comments
 (0)