ci: 新增 GitHub Actions 工作流,发布改用 PyPI Trusted Publishing

- .github/workflows/ci.yml:push/PR 触发,Python 3.11 + 3.13 矩阵跑 pytest,
  另做一次构建与 twine check 校验
- .github/workflows/release.yml:推送 v* 标签触发,测试 → 构建 → 发布到 PyPI
  - 含 git tag 与 pyproject 版本一致性校验,防止标签与版本号错位
  - publish 作业使用 environment: pypi + id-token: write,走 OIDC 免密钥发布
- uv.lock:同步 pytdxdata 版本 0.3.2 → 0.4.0(此前 lockfile 落后于 pyproject.toml)
This commit is contained in:
openbot-coder committed 2026-09-15 12:00:02 +08:00
1 parent 9183c3294f
commit 92a03951c7
3 files changed
+241 -76

No files matched your search

+60
View File
@@ -0,0 +1,60 @@
name: CI
# 每次 push / PR 都跑,用于在打发布标签之前提前暴露问题
# (本机 Windows 上无法创建 asyncio 事件循环,所以 Linux 侧的首轮验证只能靠这里)
on:
push:
branches:
- main
- develop
pull_request:
workflow_dispatch:
permissions:
contents: read
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
name: Test (Python ${{ matrix.python-version }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.11", "3.13"]
steps:
- name: Checkout
uses: actions/checkout@v7
- name: Install uv
uses: astral-sh/setup-uv@v10
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: uv sync --locked
- name: Run tests
run: uv run pytest -q
build:
name: Build check
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v7
- name: Install uv
uses: astral-sh/setup-uv@v10
with:
python-version: "3.13"
- name: Build sdist and wheel
run: uv build
- name: Check distribution metadata
run: uvx twine check dist/*
+105
View File
@@ -0,0 +1,105 @@
name: Release
# 触发方式:
# 1) 推送 v* 标签(如 v0.4.0)→ 测试 → 构建 → 发布到 PyPI
# 2) 手动 workflow_dispatch → 只跑测试 + 构建,不发布(除非从 tag 上触发)
#
# PyPI Trusted Publisher 配置(项目 → Manage → Publishing)需填写:
# Owner : openbot-coder
# Repository name : pytdxdata
# Workflow name : release.yml <-- 必须与「本文件名」完全一致
# Environment name : pypi <-- 必须与下方 environment.name 一致
on:
push:
tags:
- "v*"
workflow_dispatch:
permissions:
contents: read
concurrency:
group: release-${{ github.ref }}
cancel-in-progress: false
jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v7
- name: Install uv
uses: astral-sh/setup-uv@v10
with:
python-version: "3.13"
- name: Install dependencies
run: uv sync --locked
- name: Run tests
run: uv run pytest -q
build:
name: Build distributions
needs: test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v7
- name: Install uv
uses: astral-sh/setup-uv@v10
with:
python-version: "3.13"
# 防止「标签叫 v0.5.0、pyproject 里还是 0.4.0」这种低级事故
- name: Verify tag matches package version
if: startsWith(github.ref, 'refs/tags/v')
run: |
set -euo pipefail
tag_version="${GITHUB_REF_NAME#v}"
pkg_version="$(uv version --short)"
echo "git tag : v${tag_version}"
echo "pyproject : ${pkg_version}"
if [ "${tag_version}" != "${pkg_version}" ]; then
echo "::error::git tag v${tag_version} 与 pyproject.toml 的版本 ${pkg_version} 不一致,已中止发布"
exit 1
fi
- name: Build sdist and wheel
run: uv build
- name: Check distribution metadata
run: uvx twine check dist/*
- name: Upload distributions
uses: actions/upload-artifact@v7
with:
name: dist
path: dist/
if-no-files-found: error
publish:
name: Publish to PyPI
needs: build
# 只在 v* 标签上发布;手动 dispatch 到分支时只跑测试与构建
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/p/pytdxdata
permissions:
# Trusted Publishing 必需:允许向 PyPI 换取 OIDC 短期令牌(无需任何 token 密钥)
id-token: write
steps:
- name: Retrieve distributions
uses: actions/download-artifact@v8
with:
name: dist
path: dist/
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1