本文档说明如何使用自动化工作流将 deep-diff 发布到 PyPI。
该项目配置了 GitHub Actions 工作流,在推送 Git tag 时自动发布到 PyPI。
.github/workflows/publish.yml- 自动 PyPI 发布工作流
- ✅ 监听 tag 推送事件
- ✅ 验证版本号是否与
pyproject.toml一致 - ✅ 构建 wheel 和 source distribution
- ✅ 上传到 PyPI
- ✅ 创建 GitHub Release
编辑 pyproject.toml 并更新版本号:
[project]
name = "deep-diff"
version = "0.2.0" # 更新版本号在 pyproject.toml 中的 Changelog 部分或在 README.md 中记录变更。
git add pyproject.toml
git commit -m "chore: bump version to 0.2.0"
git push origin master等待 GitHub Actions CI 工作流完成,确保所有测试都通过。
# 创建带注释的 tag
git tag -a v0.2.0 -m "Release version 0.2.0"
# 推送 tag 到远程仓库
git push origin v0.2.0- 访问 GitHub Releases
- 点击 "Create a new release"
- 填写 tag 名称(例如
v0.2.0) - 添加发布说明
- 点击 "Publish release"
# 1. 更新版本
vim pyproject.toml # 改成 0.2.0
# 2. 提交更新
git add pyproject.toml
git commit -m "chore: bump version to 0.2.0"
# 3. 创建 tag
git tag -a v0.2.0 -m "Release version 0.2.0"
# 4. 推送到 GitHub(自动触发发布工作流)
git push origin master
git push origin v0.2.0
# 5. 监控工作流进度
# - 访问 GitHub Actions 页面查看发布状态
# - 等待工作流完成
# 6. 验证发布
# - 访问 https://pypi.org/project/deep-diff/0.2.0/
# - 或运行: pip install deep-diff==0.2.0工作流在以下情况下触发:
v*- 所有以v开头的 tag(例如v0.2.0)[0-9]+.[0-9]+.[0-9]+- 语义化版本(例如0.2.0)
工作流会验证 Git tag 版本与 pyproject.toml 中的版本是否一致:
Git tag: v0.2.0
pyproject.toml: version = "0.2.0"
✅ 匹配 → 继续发布
❌ 不匹配 → 发布失败
本项目使用 Trusted Publisher 方式连接到 PyPI,无需存储密钥:
- ✨ 更安全:不需要在 GitHub Secrets 中存储 PyPI token
- ✨ 自动更新:PyPI 自动信任来自 GitHub 的发布
- ✨ 临时权限:每次发布都是独立授权
在 PyPI 项目设置中,需要配置 Trusted Publisher:
- 访问 PyPI deep-diff 设置
- 进入 "Publishing" → "Trusted publishers"
- 添加新的 Trusted Publisher:
- Workflow name:
publish.yml - Environment name: (留空)
- Owner:
ider-zh - Repository name:
diff - Ref type:
tag - Ref:
refs/tags/*
- Workflow name:
- 访问项目的 Actions 标签页
- 找到 "Publish to PyPI" 工作流
- 点击最近的运行查看详细日志
工作流包含以下步骤:
1. Checkout code
↓
2. Set up Python
↓
3. Extract version from tag
↓
4. Verify version matches pyproject.toml
↓
5. Install dependencies
↓
6. Build distribution
↓
7. Publish to PyPI
↓
8. Create GitHub Release
↓
9. Summary
发布完成后,验证:
# 方法 1: 访问 PyPI
curl -s https://pypi.org/pypi/deep-diff/json | python -m json.tool | grep -A 5 releases
# 方法 2: 尝试安装
pip install --upgrade deep-diff
# 方法 3: 检查版本
python -c "import deep_diff; print(deep_diff.__version__)"症状: 工作流在 "Verify version" 步骤失败
解决方案:
# 确认 tag 和 pyproject.toml 版本一致
grep version pyproject.toml
git tag -l | tail -5症状: 发布步骤返回认证错误
解决方案:
- 在 PyPI 项目设置中添加 Trusted Publisher
- 参考上方 "PyPI 配置要求" 部分
症状: 工作流进行到 "Create GitHub Release" 时失败
解决方案:
- 这通常不影响 PyPI 发布
- 可以手动在 GitHub 上创建 Release
git tag -a v0.2.0 -m "Release version 0.2.0
- Feature 1: 描述
- Feature 2: 描述
- Bug fix: 修复的问题"发布说明将自动在 GitHub Release 中创建,包含:
- 版本号
- PyPI 链接
- 安装说明
-
语义化版本: 遵循 Semantic Versioning
MAJOR.MINOR.PATCH(例如0.2.0)
-
提前验证:
# 创建 tag 后本地测试 python -m build twine check dist/*
-
一致性检查:
# 确保版本号一致 grep "version" pyproject.toml grep "__version__" deep_diff/__init__.py # 如有的话
-
及时更新文档:
- 更新 README 中的安装版本
- 更新 CHANGELOG 或发布说明
-
测试发布:
# 使用 --dry-run 模式测试(如果支持) twine upload --repository testpypi dist/*
本项目使用 Trusted Publisher,不需要 在 GitHub Secrets 中存储任何密钥。
工作流只能在以下条件下发布到 PyPI:
- 推送的是 git tag
- Tag 版本与
pyproject.toml一致 - GitHub Actions 工作流来自官方仓库
# 1. 在开发分支上完成功能
git checkout -b feature/new-feature
# ... 开发代码 ...
git add .
git commit -m "feat: add new feature"
# 2. 合并到主分支
git checkout master
git merge --no-ff feature/new-feature
# 3. 更新版本
vim pyproject.toml # 0.1.0 → 0.2.0
git add pyproject.toml
git commit -m "chore: bump version to 0.2.0"
# 4. 创建 tag 并推送
git tag -a v0.2.0 -m "Release version 0.2.0"
git push origin master
git push origin v0.2.0
# 5. 监控发布(GitHub Actions 自动执行)
# 访问 https://github.com/ider-zh/diff/actions
# 6. 验证发布完成
pip install --upgrade deep-diff==0.2.0
python -c "import deep_diff; print('Successfully installed!')"需要帮助? 创建一个 GitHub Issue 或查看项目的 CONTRIBUTING.md 文件。